【问题标题】:am trying to reduce the score when a condition satisfies当条件满足时,我试图降低分数
【发布时间】:2020-05-21 11:10:27
【问题描述】:

当条件满足时,我正在尝试降低分数。但没有这样做。

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

这是我写的,但是有没有办法让它变得更简单??

【问题讨论】:

  • 请分享输入数据框(几行)以及您的预期输出。请分享数据,而不是图片。
  • 你可以试试any()方法
  • @SowjanyaRBhat 你能详细说明一下吗?
  • @sammywemmy 我想我已经给出了我的数据和预期结果
  • 我的错。你有熊猫作为标签,所以我期待一个数据框。

标签: python pandas loops dataframe


【解决方案1】:
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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 2014-09-02
    • 1970-01-01
    • 2020-01-11
    • 2021-05-03
    • 1970-01-01
    相关资源
    最近更新 更多