我是Python的新手,我想知道是否有一种简洁的方法来测试值,以确定它是否是列表中的值之一,类似于SQL WHERE子句。如果这是一个基本的问题,很抱歉。
MsUpdate.UpdateClassificationTitle in (
'Critical Updates',
'Feature Packs',
'Security Updates',
'Tools',
'Update Rollups',
'Updates',
)也就是说,我想写:
if MsUpdate.UpdateClassificationTitle in (
'Critical Updates',
'Feature Packs',
'Security Updates',
'Tools',
'Update Rollups',
'Updates'
):
then_do_something()发布于 2011-10-10 21:48:17
看起来很简洁,但是如果你多次使用它,你应该给这个元组命名:
titles = ('Critical Updates',
'Feature Packs',
'Security Updates',
'Tools',
'Update Rollups',
'Updates')
if MsUpdate.UpdateClassificationTitle in titles:
do_something_with_update(MsUpdate)元组使用括号。如果你想要一个列表,把它改成方括号。或者使用具有更快查找速度的集合。
发布于 2011-10-10 21:50:06
这很简单:
sample = ['one', 'two', 'three', 'four']
if 'four' in sample:
print True发布于 2016-06-01 05:27:50
例如,请确保您在not中使用
username = ["Bob", "Kyle"]
name = "Kyle"
if name in username:
print("step 1")
login = 1
else:
print("Invalid User")https://stackoverflow.com/questions/7713700
复制相似问题