在元组列表中找到精确的元组匹配并返回其索引 [英] Find an Exact Tuple Match in a List of Tuples and Return Its Index

查看:50
本文介绍了在元组列表中找到精确的元组匹配并返回其索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何确定元组列表中元组是否完全匹配,如果是,则返回匹配元组的索引.例如,如果我有:

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')]

我希望能够接受任何元组('XXXX','YYYY'),看看它在TupList中是否具有完全匹配的内容,如果有,则其索引是什么.因此,例如,如果元组('XXXX','YYYY')=(u'EFG H',u'462')准确,则该代码将返回 1 .

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.

我也不想允许像('EFG','462')这样的元组(基本上是任何一个tuple元素的子字符串)匹配.

I also don't want to allow tuples like ('EFG', '462') (basically any substring of either tuple element) to match.

推荐答案

使用 list.index :

>>> TupList = [('ABC D','235'),('EFG H','462')]
>>> TupList.index((u'EFG H',u'462'))
1

这篇关于在元组列表中找到精确的元组匹配并返回其索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆