Accessing multidimensional lists with an index list

Discussion in 'Python' started by Gabriel Birke, Apr 17, 2005.

  1. Given the multidimensional list l:
    l = [ {'v1': 1, 'v2': 2},
    [ {'v1':4, 'v2': 7},
    {'v1': 9, 'v2': 86},
    [ {'v1': 77, 'v2': 88}]
    ]
    ]

    I want to access specific items the indices of which are stored in
    another list. For now, I created a function to do this:

    def getNestedValue(l, indices):
    while len(indices) > 0:
    i = indices.pop(0)
    l = l #In future versions, put error checking here
    return l

    print getNestedValue(l, [1, 2, 0])
    print getNestedValue(l, [1, 1])

    Is there a more elegant or performant language construct to accomplish
    my task?
    Gabriel Birke, Apr 17, 2005
    #1
    1. Advertising

  2. Gabriel Birke

    Kent Johnson Guest

    Gabriel Birke wrote:
    > Given the multidimensional list l:
    > l = [ {'v1': 1, 'v2': 2},
    > [ {'v1':4, 'v2': 7},
    > {'v1': 9, 'v2': 86},
    > [ {'v1': 77, 'v2': 88}]
    > ]
    > ]
    >
    > I want to access specific items the indices of which are stored in
    > another list. For now, I created a function to do this:
    >
    > def getNestedValue(l, indices):
    > while len(indices) > 0:
    > i = indices.pop(0)
    > l = l #In future versions, put error checking here
    > return l
    >
    > Is there a more elegant or performant language construct to accomplish
    > my task?


    def getNestedValue(l, indices):
    for i in indices:
    l = l #In future versions, put error checking here
    return l

    Kent
    Kent Johnson, Apr 17, 2005
    #2
    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. =?iso-8859-1?q?Orla=20O'Sullivan?=

    multidimensional lists

    =?iso-8859-1?q?Orla=20O'Sullivan?=, Oct 7, 2003, in forum: Python
    Replies:
    1
    Views:
    304
    anton muhin
    Oct 7, 2003
  2. Nils Grimsmo
    Replies:
    1
    Views:
    269
    Michael Hoffman
    Oct 12, 2004
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    403
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. Alexzive
    Replies:
    6
    Views:
    602
    Chris Rebert
    Mar 20, 2009
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    283
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page