Python: Find item in multidimensional list
I have a list of lists, a snippet of which is below:
x_attrib = []
self.x_attrib.append(["Is_virtual", False, 'virtual', 'flag'])
self.x_attrib.append(["X_pos", None, 'pos/x', 'attrib'])
self.x_attrib.append(["Y_pos", None, 'pos/y', 'attrib'])
I want make a functional that retu开发者_运维百科rns the index of the item at the first position (i.e. I want to pass "X_pos"
to a function, and have it return 1
).
How can I do this?
If I understand correctly, you need to something like this:
def find_it(key):
for index, sublist in enumerate(lists):
if sublist[0] == key:
return index
Having said that your code looks like you are solving the more general problem incorrectly. i.e. that list look like a bad idea. Without a better idea of what you are doing I cannot be certain.
精彩评论