search - searching an item in a multidimensional array in python -


i have multidimensional array in python.

hello = [(['b', 'y', 'e'], 3), (['h', 'e', 'l', 'l', 'o'], 5), (['w', 'o', 'r', 'l', 'd'], 5)] 

and wanted find index of number 3, tried using hello.index(3) won't work. solutions?

>>> [x[0] x in hello if x[1] == 3][0] ['b', 'y', 'e'] 

if need index of item, try

>>> [i i, x in enumerate(hello) if x[1] == 3][0] 0 

for multiple results remove [0] @ end:

>>> hello.append((list("spam"), 3)) >>> hello [(['b', 'y', 'e'], 3), (['h', 'e', 'l', 'l', 'o'], 5), (['w', 'o', 'r', 'l', 'd'], 5), (['s', 'p', 'a', 'm'], 3)] >>> [x[0] x in hello if x[1] == 3] [['b', 'y', 'e'], ['s', 'p', 'a', 'm']] >>> [i i, x in enumerate(hello) if x[1] == 3] [0, 3] >>>  

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -