[prev in list] [next in list] [prev in thread] [next in thread]
List: python-list
Subject: Re: Accessing multidimensional lists with an index list
From: Kent Johnson <kent37 () tds ! net>
Date: 2005-04-17 20:21:07
Message-ID: 4262c100_3 () newspeer2 ! tds ! net
[Download RAW message or body]
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
--
http://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic