Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在元组Python中只检查1个元素_Python_Tuples - Fatal编程技术网

如何在元组Python中只检查1个元素

如何在元组Python中只检查1个元素,python,tuples,Python,Tuples,我有一个元组列表: l = [] 在元组中: a = (0, 1, 1) l.append(a) 我想检查第二个位置上是否存在值“1”,但第三个位置上是否存在值“1” 检查1位于位置2: >>> a = (0, 1, 1) >>> if a[1] == 1: ... print("yes it is") ... yes it is 如果您正在检查以确保1存在于位置2而不是位置3: >>> a = (0, 1, 1) >>&

我有一个元组列表:

l = []
在元组中:

a = (0, 1, 1)
l.append(a)

我想检查第二个位置上是否存在值“1”,但第三个位置上是否存在值“1”

检查1位于位置2:

>>> a = (0, 1, 1)
>>> if a[1] == 1:
...  print("yes it is")
...
yes it is
如果您正在检查以确保1存在于位置2而不是位置3:

>>> a = (0, 1, 1)
>>> if a[1] == 1 and a[2] != 1:
...  print('hello')
...
>>>
如果您有一个元组列表:

a = [(0,1,1), (0,1,1), (0,1,0)]
并希望筛选出那些标准适用于
a[1]==1
a[2]!=1
,然后以如下方式收集它们:

a = [(0,1,1), (0,1,1), (0,1,0)]
res = [v for i, v in enumerate(a) if v[1] == 1 and v[2] != 1]
print(res)
# [(0, 1, 0)]
这将有助于:

if a[1] == 1 and a[2] != 1:
    #do something
试试这个

a = (0, 1, 1)
if a.index(1) == 1:
    #do something

要查找第二个值是否为
1
,第三个值是否为
1
,请执行以下操作:

if a[1] == 1 and a[2] != 1: 
    return True # Or whatever you want to do
如果要检查secn位置,它将连续运行

要检查索引,您可以执行以下操作

thetuple.index("index")

你是说a[1]==1吗?不,对不起,我解释过了wrong@Paul,然后快速编辑你的问题,否则你会失去很多代表。这是一个不写完整问题的例子,获取大量答案,然后更改要求:|…并失去通过努力工作获得的所有声誉。您可能不想使用shell获取答案,因为复制和粘贴代码可能会很困难。回答得不错。@MarkyPython的人不应该只是复制和粘贴提供的答案。这只是我的意见,我不同意,因为如果他们不能复制和粘贴答案,他们无论如何都会重新键入。还不如让它更容易些。对我来说,有帮助的是把代码放到编辑器中,这样我就可以在那里通读,看看代码的作用。这是我的意见。
if element in thetuple:
    //whatever u want
thetuple.index("index")