如何验证元组中是否不存在变量?
How can i verify if a variable does not exist in a tuple?
我有以下元组:
mytuple = (("azerty", "1"), ("qwerty", "2"))
我正在尝试检查变量是否不在其中:
n = 'azerty'
if n not in mytuple:
print('not in the list')
> not in the list
我正在尝试将字符串“azerty”与元组中的每个元组匹配,所以我尝试在元组中再添加一层:
for mylist in mytuple:
if n not in mylist:
print('not in the list')
> not in the list
现在这里的问题是 mytuple[1]
匹配我的 if,让我打印。
你知道如何检查我的变量是否不在元组中吗?
编辑:这个问题被标记为重复。但是,answered post 指的是一个稍微不同的问题。原来的 post 通过索引检查是否存在,这道题想检查元组中任何元素是否存在。
或者你可以这样做:
general_list = [("azerty", "1"), ("qwerty","2")]
n = "azerty"
in_tuple = False
for single_tuple in general_list:
if n in single_tuple:
in_tuple = True
if not in_tuple:
print("not in the list")
我有以下元组:
mytuple = (("azerty", "1"), ("qwerty", "2"))
我正在尝试检查变量是否不在其中:
n = 'azerty'
if n not in mytuple:
print('not in the list')
> not in the list
我正在尝试将字符串“azerty”与元组中的每个元组匹配,所以我尝试在元组中再添加一层:
for mylist in mytuple:
if n not in mylist:
print('not in the list')
> not in the list
现在这里的问题是 mytuple[1]
匹配我的 if,让我打印。
你知道如何检查我的变量是否不在元组中吗?
编辑:这个问题被标记为重复。但是,answered post 指的是一个稍微不同的问题。原来的 post 通过索引检查是否存在,这道题想检查元组中任何元素是否存在。
或者你可以这样做:
general_list = [("azerty", "1"), ("qwerty","2")]
n = "azerty"
in_tuple = False
for single_tuple in general_list:
if n in single_tuple:
in_tuple = True
if not in_tuple:
print("not in the list")