gpt4 book ai didi

python - 如何从python中的范围中排除列表?

转载 作者:行者123 更新时间:2023-11-28 21:50:53 25 4
gpt4 key购买 nike

因此,我根据适合特定条件的设定范围内的值创建了一个列表,现在稍后我想检查同一范围内的所有值,但不包括放入该列表的值..这可能吗?如果没有,还有其他选择吗?

filled_columns = []
for b in range(0,7):
if board[0,b] == 1 or board[0,b] == 2:
filled_columns.append(b)
for b in range(0,7) #but excluding values from "filled_columns" list

最佳答案

如果我对您的问题的理解正确,您可以使用列表推导将项目复制到新列表中,过滤掉不需要的项目。例如:

list1 = range(20)
filterlist = [4,6,8,9,14]

filtered=[item for item in list1 if item not in filterlist]
print filtered

因此,对于您的示例,将最后一行替换为:

checklist = [b for b in range(0,7) if b not in filled_columns]
for b in checklist:
# do stuff with b

关于python - 如何从python中的范围中排除列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31314292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com