I am trying to figure out how to determine if a tuple has an exact
match in a list of tuples, and if so, return the index of the matching tuple. For
instance if I have:
TupList =
[('ABC D','235'),('EFG
H','462')]
I
would like to be able to take any tuple ('XXXX','YYYY')
and see
if it has an exact match in TupList and if so, what its index is. So for example, if the
tuple ('XXXX','YYYY') = (u'EFG H',u'462')
exactly, then the
code will return 1
.
I
also don't want to allow tuples like ('EFG', '462')
(basically
any substring of either tuple element) to match.
Answer
Use
list.index
:
>>>
TupList = [('ABC D','235'),('EFG H','462')]
>>> TupList.index((u'EFG
H',u'462'))
1
No comments:
Post a Comment