[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-list
Subject:    Accessing multidimensional lists with an index list
From:       gabriel.birke () gmail ! com (Gabriel Birke)
Date:       2005-04-17 19:50:58
Message-ID: 32903d4e.0504171150.73b7feb0 () posting ! google ! com
[Download RAW message or body]

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