[Tutor] initialising all elements of a matrix
Sivaram Neelakantan
nsivaram.net at gmail.com
Tue Feb 28 15:40:10 CET 2012
I was wondering whether there is a faster/better/cleaner way of
element wise operations of arbitrarily nested list. I wrote something
like this for 1 level nested lists and am wondering whether there are
any good idioms in python. I did this after a ridiculous amount of
bad thinking/missteps in python for the simplest of cases.
def init_p (arr):
# input is always 2D matrix; init to uniform probability dist.
q = []
row = len(arr)
col = len(arr[0])
uni_dist = 1.0/(row *col)
q = [ [uni_dist] * col for i in range(row)]
return q
Of course, without using external packages like numpy or any other
scientific packages.
sivaram
--
More information about the Tutor
mailing list