gpt4 book ai didi

python - Python 中的简单 if-else 语句

转载 作者:太空狗 更新时间:2023-10-30 00:56:00 25 4
gpt4 key购买 nike

我正在编写一个脚本,如果数组的元素是主数组的子集,则打印 PASS,否则打印 FAIL。

我应该在下面的 if-else 语句中添加什么才能使其有效?

a = [1,2,3,4,5]
b = [1,2]
c = [1,9]

# The Passing Scenario

if (i in a for i in b):
print "PASS"
else:
print "FAIL"

# The Failing Scenario

if (i in a for i in c):
print "PASS"
else:
print "FAIL"

最佳答案

使用全部

# the passing scenario
if all(i in a for i in b):
print 'PASS'
else:
print 'FAIL'

# the failing scenario
if all(i in a for i in c):
print 'PASS'
else:
print 'FAIL'

关于python - Python 中的简单 if-else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22499786/

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