Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > List of lists of lists of lists...

Reply
Thread Tools

List of lists of lists of lists...

 
 
=?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?=
Guest
Posts: n/a
 
      05-08-2006
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]]]
--
テ]gel Gutiテゥrrez Rodrテ*guez - http://www.velocityreviews.com/forums/(E-Mail Removed)
Instituto de Ciencia de los Materiales de Madrid - CSIC
SpLine - European Syncrothorn Radiation Facility - Grenoble - France

Postal adress: Departamento de Quテ*mica Fテ*sica y Analテ*tica
Universidad de Oviedo - c/Juliテ。n Claverテ*a 8 33006 - Oviedo
Asturias - Spain
E-mail: (E-Mail Removed) Telf.: +34-985103687
 
Reply With Quote
 
 
 
 
bruno at modulix
Guest
Posts: n/a
 
      05-09-2006
テ]gel 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


Hint : what's exactly the difference between deep(x) and [x] ?

> mylist=[]
> for N: mylist=deep(mylist)
>
> Is there a more elegant way to do it?


for N:
mylist = [mylist]


> 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]]]


I'm afraid I don't understand. Could you forgive my stupidity and
re-explain this a bit more clearly ?

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
 
 
 
James Stroud
Guest
Posts: n/a
 
      05-10-2006
テ]gel 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/
 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      05-10-2006
James Stroud wrote:

> 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]]])


numpy is the successor to numarray, so if you are just starting with arrays,
please get started with numpy. It should also be noted that numpy can create
arrays of objects as well as arrays of numbers.

In [1]: from numpy import *

In [2]: empty((1,2,3), dtype=object)
Out[2]:
array([[[None, None, None],
[None, None, None]]], dtype=object)

http://numeric.scipy.org

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

 
Reply With Quote
 
=?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?=
Guest
Posts: n/a
 
      05-15-2006
bruno at modulix wrote:

> for N:
> mylist = [mylist]
>

Right that!

> I'm afraid I don't understand. Could you forgive my stupidity and
> re-explain this a bit more clearly ?
>

No need to. Former solution worked fine. Thanks!
--
テ]gel Gutiテゥrrez Rodrテ*guez - (E-Mail Removed)
Instituto de Ciencia de los Materiales de Madrid - CSIC
SpLine - European Syncrothorn Radiation Facility - Grenoble - France

Postal adress: Departamento de Quテ*mica Fテ*sica y Analテ*tica
Universidad de Oviedo - c/Juliテ。n Claverテ*a 8 33006 - Oviedo
Asturias - Spain
E-mail: (E-Mail Removed) Telf.: +34-985103687
 
Reply With Quote
 
=?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?=
Guest
Posts: n/a
 
      05-15-2006
Robert Kern wrote:
> James Stroud wrote:
> http://numeric.scipy.org
>

Thanks! That's anotehr solution, yes!
--
テ]gel Gutiテゥrrez Rodrテ*guez - (E-Mail Removed)
Instituto de Ciencia de los Materiales de Madrid - CSIC
SpLine - European Syncrothorn Radiation Facility - Grenoble - France

Postal adress: Departamento de Quテ*mica Fテ*sica y Analテ*tica
Universidad de Oviedo - c/Juliテ。n Claverテ*a 8 33006 - Oviedo
Asturias - Spain
E-mail: (E-Mail Removed) Telf.: +34-985103687
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
common elements between list of lists and lists antar2 Python 2 07-17-2008 09:19 AM
How to have a list of lists (or array of lists) bahoo Python 3 04-03-2007 07:37 PM
Extracting a List from a List of lists Damo Java 4 02-09-2007 01:02 AM
list of lists of lists .... yomgui Python 6 07-31-2006 07:28 PM
Howto: extract a 'column' from a list of lists into a new list? Greg Brunet Python 7 07-02-2003 03:20 AM



Advertisments