gpt4 book ai didi

python - Python 中更容易的流控制

转载 作者:太空宇宙 更新时间:2023-11-04 07:23:12 25 4
gpt4 key购买 nike

在下面的代码中,我试图检查变量“new_shape”是否已经存在于“shape_list”中。如果它不存在,我想添加它;如果它确实存在,我只想离开它。到目前为止,我只使用标志实现了这一点。我确信有一种方法可以在没有标志的情况下更有效地完成同样的事情。有什么建议么?感谢您提供的任何帮助!

    flag = 0
for shape in shape_list:
if new_shape == shape:
flag = 1
break
if flag == 0:
shape_list.append(new_shape)

最佳答案

你可以使用

if new_shape not in shape_list:
shape_list.append(new_shape)

关于python - Python 中更容易的流控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11748102/

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