List of lists of lists of lists...

James Stroud jstroud at ucla.edu
Tue May 9 21:10:44 EDT 2006


Ángel Gutiérrez Rodríguez wrote:
> I would like to have a list of lists N times deep, and my solution is (in
> pseudocode):
> 
> def deep(x):
>   a=[x]
>   return a
> 
> mylist=[]
> for N: mylist=deep(mylist)
> 
> Is there a more elegant way to do it?
> 
> The maine idea is: from a list having the numbre of steps along N
> dimensions, generate a list with an item at each possible point.
> 
> Example 1: N=2  list=[2,3]  result=[[1,2],[1,2],[1,2]]
> Example 2: N=3  list=[3,1,2]  result=[[[1,2,3]],[[1,2,3]]]

Numarray does this sort of thing, but you have to familiarize yourself 
with its indexing conventions:

py> import numarray
py> numarray.ones((3,2))
array([[1, 1],
        [1, 1],
        [1, 1]])
py> numarray.ones((1,2,3))
array([[[1, 1, 1],
         [1, 1, 1]]])

James

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list