List of lists of lists of lists...

Discussion in 'Python' started by =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006.

  1. 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]]]
    --
    Ãngel Gutiérrez Rodríguez -
    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: Telf.: +34-985103687
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006
    #1
    1. Advertising

  2. Ã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


    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 ''.split('@')])"
    bruno at modulix, May 9, 2006
    #2
    1. Advertising

  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    James Stroud Guest

    Ã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/
    James Stroud, May 10, 2006
    #3
  4. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    Robert Kern Guest

    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
    Robert Kern, May 10, 2006
    #4
  5. 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!
    --
    Ãngel Gutiérrez Rodríguez -
    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: Telf.: +34-985103687
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 15, 2006
    #5
  6. Robert Kern wrote:
    > James Stroud wrote:
    > http://numeric.scipy.org
    >

    Thanks! That's anotehr solution, yes!
    --
    Ãngel Gutiérrez Rodríguez -
    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: Telf.: +34-985103687
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 15, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Brunet
    Replies:
    7
    Views:
    409
    Greg Brunet
    Jul 2, 2003
  2. yomgui

    list of lists of lists ....

    yomgui, Jul 28, 2006, in forum: Python
    Replies:
    6
    Views:
    297
    yomgui
    Jul 31, 2006
  3. Damo
    Replies:
    4
    Views:
    502
  4. bahoo
    Replies:
    3
    Views:
    289
    Bruno Desthuilliers
    Apr 3, 2007
  5. antar2
    Replies:
    2
    Views:
    375
    Bighead
    Jul 17, 2008
Loading...

Share This Page