繁体   English   中英

我想进入清单,检查是否有多个元素等于我想要的元素

[英]I want to go thought list with if to check if multiple elements are equal to that I want

我想做这样的事情:

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

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

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

如果您的元素是可哈希的,请使用set

st = set(choice)

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

使用列表理解所有

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM