Python 条件以检查列表中是否包含除所需字符串以外的字符串
我想写一个条件,检查列表中是否有另一个字符串不是Python 条件以检查列表中是否包含除所需字符串以外的字符串,python,python-3.x,if-statement,Python,Python 3.x,If Statement,我想写一个条件,检查列表中是否有另一个字符串不是'1'或'2' 例如: list = ['1', '2'] -> is True list = ['1', '3'] -> is false 我试过类似的方法,但没有成功: if list[0] is not '1' or list[0] is not '2' and list[1] is not '1' or list[1] is not '2': return False else: return True 帮助
'1'
或'2'
例如:
list = ['1', '2'] -> is True
list = ['1', '3'] -> is false
我试过类似的方法,但没有成功:
if list[0] is not '1' or list[0] is not '2' and list[1] is not '1' or list[1] is not '2':
return False
else:
return True
帮助某人???试试这样的方法
l1=['1','2']
l2=['1','3']
def check(l1):
if '1' in l1 and '2' in l1:
print('True')
else:
print('False')
check(l1)
check(l2)
输出:
True
False
试试这样的
l1=['1','2']
l2=['1','3']
def check(l1):
if '1' in l1 and '2' in l1:
print('True')
else:
print('False')
check(l1)
check(l2)
输出:
True
False
在生成器中使用
all
:
def check_value(lst):
return all(x in ('1', '2') for x in lst)
用法:
>>> check_value(['1', '2'])
True
>>> check_value(['1', '3'])
False
在生成器中使用
all
:
def check_value(lst):
return all(x in ('1', '2') for x in lst)
用法:
>>> check_value(['1', '2'])
True
>>> check_value(['1', '3'])
False
您可以使用any()
进行此操作,只需pythonic:
def检查列表(l):
不返回任何(对于l中的x,x不在('1','2'))
您可以使用any()
进行此操作,只需pythonic:
def检查列表(l):
不返回任何(对于l中的x,x不在('1','2'))
不要将列表
用作变量,因为它是一个内置函数
checking_elements = ['1', '2']
ls = ['1', '3']
for ele in ls:
if ele not in checking_elements:
return False
return True
如果你的ls
只有两个元素,那么
checking_elements = ['1', '2']
ls = ['1', '3']
return (ls[0] in checking_elements and ls[1] in checking_elements)
不要将
list
用作变量,因为它是一个内置函数
checking_elements = ['1', '2']
ls = ['1', '3']
for ele in ls:
if ele not in checking_elements:
return False
return True
如果你的ls
只有两个元素,那么
checking_elements = ['1', '2']
ls = ['1', '3']
return (ls[0] in checking_elements and ls[1] in checking_elements)
你好!!
你能做的第一件事就是制作一个iter对象
s=iter("12")
这将创建一个带有“1”和“2”的iter对象。你一定在想为什么要使用iter而不是列表。
答案在于iter对象的大小、内存小于列表
import sys
l=["1","2"]
s=iter("12")
sys.getsizeof(l) #return 72
sys.getsizeof(s) #return 48
所以我做的是,我得到了l和s的内存大小。
如果您想了解有关此函数的更多信息,请转到
现在回到你的问题,检查下面的代码
for element in l:
if element in s:
print("69 is not just a number!!!") #You can replace this part by anything u want
输出:
69 is not just a number!!!
69 is not just a number!!!
False
这样做的好处是,您可以将其用于更多字符串!!!。而且记忆会非常少
你也可以按照奥斯汀的方式来做
继续学习 你好!!
你能做的第一件事就是制作一个iter对象
s=iter("12")
这将创建一个带有“1”和“2”的iter对象。你一定在想为什么要使用iter而不是列表。
答案在于iter对象的大小、内存小于列表
import sys
l=["1","2"]
s=iter("12")
sys.getsizeof(l) #return 72
sys.getsizeof(s) #return 48
所以我做的是,我得到了l和s的内存大小。
如果您想了解有关此函数的更多信息,请转到
现在回到你的问题,检查下面的代码
for element in l:
if element in s:
print("69 is not just a number!!!") #You can replace this part by anything u want
输出:
69 is not just a number!!!
69 is not just a number!!!
False
这样做的好处是,您可以将其用于更多字符串!!!。而且记忆会非常少
你也可以按照奥斯汀的方式来做
继续学习 对于初学者,欢迎!:)
我们可以尝试使用all()函数使用if语句检查列表中的所有元素(使其更易于阅读):
例如:
list1 = ['1', '3']
if all(elem in ('1', '2') for elem in list1): #if all elements in the list have '1' or '2'
True #return true
else: #if not
False #return false
输出:
69 is not just a number!!!
69 is not just a number!!!
False
希望这就是您想要的。对于初学者,欢迎!:)
我们可以尝试使用all()函数使用if语句检查列表中的所有元素(使其更易于阅读):
例如:
list1 = ['1', '3']
if all(elem in ('1', '2') for elem in list1): #if all elements in the list have '1' or '2'
True #return true
else: #if not
False #return false
输出:
69 is not just a number!!!
69 is not just a number!!!
False
希望这就是您要查找的内容。但是l=['1','2']也应该是真的。列表应该包括'1'或'2',其他字符都是假的。如果仍然是错的,请在
['1','2','3']
上运行代码,但是l=['1',2']也应该是真的。列表应该包括'1'或'2',每隔一个字符都是错误的。如果仍然是错误的,请在['1','2','3']上运行代码。