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.
Initial approach that can be applied is that we can iterate on each tuple and check it's count in list using count() , if greater than one, we can add to list. To remove multiple additions, we can convert the result to set using set() .
Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
Use list.index
:
>>> TupList = [('ABC D','235'),('EFG H','462')]
>>> TupList.index((u'EFG H',u'462'))
1
I think you can do it by this
TupList = [('ABC D','235'),('EFG H','462')]
if ('ABC D','235') in TupList:
print TupList.index(i)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With