python - Find the index of a list of tuples with nondistinct values in first index of tuple -
i'm trying index of list of 2-tuples in python, 0 index of each tuple 'a':
list_of_tuples = [('a', 1), ('a', 2), ('a', 3)] so use following list comprehension try find out index returned, correct?
index_list = [x x, y in enumerate(list_of_tuples) if x[0] == 'a'] which gives following error:
typeerror traceback (most recent call last) <ipython-input-22-5d47328b7d9a> in <module>() ----> 1 index_list = [x x, y in enumerate(list_of_tuples) if x[0] == 'a'] typeerror: 'int' object not subscriptable how find out index associated searching list of tuples value in 0th index being equal 'a'?
use value y compare not index x:
list_of_tuples = [('a', 1), ('a', 2), ('a', 3)] index_list = [x x, y in enumerate(list_of_tuples) if y[0] == 'a'] now index_list [0, 1, 2].
Comments
Post a Comment