python - 当条件满足时,我试图降低分数
问题描述
当条件满足时,我试图降低分数。但未能如愿。
data = ['A','B']
Score = 10
words = [ 'C', 'D']
for i in data:
if i in words:
do nothing
else:
reduce score by 2
在这里,当 A 和 B 都不存在时,我希望我的分数只减少一次,而不是两次。
Expected output : 8
代码 :
index = []
for i in data:
if i in words:
do nothing
else:
index.append(something)
if len(index) > 1:
reduce score by 2
这就是我写的,但是有没有办法让这变得不那么复杂??
解决方案
data = ['A','B']
score = 10
words = [ 'C', 'D']
data_not_found_list = [False for dt in data if dt not in words]
if not any(data_not_found_list):
score -= 2
print(score)
输出 :
8
我在这里使用了 any() 方法。您可以阅读如何获得一个想法 - https://realpython.com/any-python/
推荐阅读
- excel - Paste contents of range into 3 different workbooks
- javascript - 在 html 上使用 JavaScript 增加字体大小
- html - 如何忽略其中的内容使用 pandas.read_html 时的标签?
- maven - maven jqgrid tag not found
- netsuite - 自定义记录在线表格 - 无效的记录类型响应
- c - Program only works with small numbers
- python - Python list object mutation
- html - 选择禁用选项的发布值
- shell - 基于标题特征提取文件列
- graph - 在 Latex 中绘制有向图