【问题标题】:Subtract list of boolean减去布尔值列表
【发布时间】:2021-03-15 23:42:10
【问题描述】:

我有两个布尔值列表 buy_machinebroken_machine。我想创建第三个列表working_machines,将购买的机器数量相加并减去损坏机器的数量。

我尝试了以下代码,结果列表中充满了'nothing' 我希望至少能看到'plus 1''minus 1',这只是为了尝试一些东西,我不知道如何实际制作@ 987654327@ 列表求和或减去布尔值

years = list(range(1, 21))
buy_machine = []
broken_machine = []
working_machines = []
for year in years:
    if year <= 5:
        buy_machine.append(True)
    else:
        buy_machine.append(False)
    if 10 % year == 10 and year <= 15:
        broken_machine.append(True)
    else:
        broken_machine.append(False)
    if buy_machine == True and broken_machine == False:
        working_machines.append('plus 1')
    elif buy_machine == False and broken_machine == True:
        working_machines.append('minus 1')
    else:
        working_machines.append('nothing')
buy_machine
[True,
 True,
 True,
 True,
 True,
 False,
 False,
 False,
 False,
 False,
 False,
 False,
 False,
 False,
 False,
 False,
 False,
 False,
 False,
 False]
broken_machine
[False,
 False,
 False,
 False,
 False,
 False,
 False,
 False,
 False,
 False,
 True,
 True,
 True,
 True,
 True,
 False,
 False,
 False,
 False,
 False]
working_machines
['nothing',
 'nothing',
 'nothing',
 'nothing',
 'nothing',
 'nothing',
 'nothing',
 'nothing',
 'nothing',
 'nothing',
 'nothing',
 'nothing',
 'nothing',
 'nothing',
 'nothing',
 'nothing',
 'nothing',
 'nothing',
 'nothing',
 'nothing']

想要的输出

working_machines
[1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0]

【问题讨论】:

    标签: python list boolean


    【解决方案1】:

    您一无所获的原因是您在询问整个列表是否正确,即buy_machine == True。不幸的是,列表不能等于布尔值,所以这就是为什么你总是使用 else 语句的原因。试试这个:

    years = list(range(1, 21))
    buy_machine = []
    broken_machine = []
    working_machines = []
    for year in years:
        if year <= 5:
            buy_machine.append(True)
        else:
            buy_machine.append(False)
        if 10 % year == 10 and year <= 15:
            broken_machine.append(True)
        else:
            broken_machine.append(False)
        if buy_machine[-1] == True and broken_machine[-1] == False:
            working_machines.append('plus 1')
        elif buy_machine[-1] == False and broken_machine[-1] == True:
            working_machines.append('minus 1')
        else:
            working_machines.append('nothing')
            
    print(working_machines)
    # Prints ['plus 1', 'plus 1', 'plus 1', 'plus 1', 'plus 1', 'nothing', 'nothing', 'nothing', 'nothing', 'nothing', 'minus 1', 'minus 1', 'minus 1', 'minus 1', 'minus 1', 'nothing', 'nothing', 'nothing', 'nothing', 'nothing']
    

    这使用buy_machine[-1] 来获取列表的最后一个元素,即您刚刚添加的元素。

    编辑:您可以将 'plus 1' 更改为 1 并将 'minus 1' 更改为 -1,然后使用 Chepner 的答案中描述的 itertools.accumulate 函数来获得您想要的。

    【讨论】:

    • 我认为只需使用循环 for year in years: 就可以获取每个元素,感谢您在列表中使用 [-1] 的提示,效果很好。
    【解决方案2】:

    working_machines 只是以下列表中值的累积和:

    >>> x = [a - b for a,b in zip(buy_machine, broken_machine)]
    >>> x
    [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0]
    

    (这是因为TrueFalse 分别等于10boolint 的子类。)

    您可以使用itertools.accumulate 来获取。

    >>> list(accumulate(x))
    [1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0]
    

    working_sums[i] = sum(x[:i]) 对应每个索引i

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 2012-10-11
      • 2015-01-21
      • 1970-01-01
      相关资源
      最近更新 更多