Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Accessing multidimensional lists with an index list

Reply
Thread Tools

Accessing multidimensional lists with an index list

 
 
Gabriel Birke
Guest
Posts: n/a
 
      04-17-2005
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[i] #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?
 
Reply With Quote
 
 
 
 
Kent Johnson
Guest
Posts: n/a
 
      04-17-2005
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[i] #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[i] #In future versions, put error checking here
return l

Kent
 
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
locate items in matrix (index of lists of lists) Alexzive Python 6 03-20-2009 06:14 PM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
List Util insert_at(index, value), delete_at(index) ngoc Perl Misc 5 05-11-2006 12:31 PM
multidimensional lists =?iso-8859-1?q?Orla=20O'Sullivan?= Python 1 10-07-2003 04:35 PM



Advertisments