如何检查Python列表中是否存在元组? [英] How to check whether a tuple exists in a Python list?
本文介绍了如何检查Python列表中是否存在元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Python的新手,我正在尝试检查列表l=[[a,b],[c,d],[d,e]]
中是否存在一对[a,b]
.我搜索了许多问题,但找不到精确的解决方案.请问有人可以告诉我正确,最短的方法吗?
I am new to Python, and I am trying to check whether a pair [a,b]
exists in a list l=[[a,b],[c,d],[d,e]]
. I searched many questions, but couldn't find precise solution. Please can someone tell me the right and shortest way of doing it?
我跑步时:
a=[['1','2'],['1','3']]
for i in range(3):
for j in range(3):
if [i,j] in a:
print a
输出空白
那如何实现呢?
推荐答案
由于'1' != 1
以及['1','2'] != [1,2]
如果您希望它正常工作,请尝试:
The code does not work because '1' != 1
and, consequently, ['1','2'] != [1,2]
If you want it to work, try:
a=[['1','2'],['1','3']]
for i in range(3):
for j in range(3):
if [str(i), str(j)] in a: # Note str
print a
(但是使用in
或如上所述的集合更好)
(But using in
or sets as already mentioned is better)
这篇关于如何检查Python列表中是否存在元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文