我想用if来检查是否有多个元素与我想要的相等

时间:2015-12-16 21:41:54

标签: python

我想做那样的事情:

if board[0:2] == choice:
    #the code here

所以我想要的是通过从0到2的元素并检查它们是否等于'选择',但我不知道该怎么做。   请帮忙!

3 个答案:

答案 0 :(得分:1)

for item in board[0:2]:
    if item==choice:
        #the code here

答案 1 :(得分:1)

如果您的元素是可清除的,请使用set

st = set(choice)

if st.issuperset(board[0:2]):

答案 2 :(得分:0)

使用list comprehensionall

if all([b == choice for b in board[0:2]]):
   ...

这会比较数组中的每个项目并创建一个新的True / False值数组。然后all()仅当新数组中的所有项都为True(即它们与您的choice匹配)时才返回True。