【问题标题】:How to iterate over multiple list and calculate every alternate values in it?如何迭代多个列表并计算其中的每个替代值?
【发布时间】:2022-11-18 19:06:33
【问题描述】:

我有一个列表,其中有多个人的值,我想计算列表列表中的每个替代值,我该如何实现?列表如下所示

for j in persons:
    person_list.append(persons[int(j)][0]+persons[int(j)][2])     
    person_list.append(persons[int(j)][1]+persons[int(j)][3])     
    print("person list", person_list)

上面是我试过的代码,但它不起作用

person [[222, 1, 255, 54], [105, 1, 135, 48], [397, 310, 521, 594]]

我必须计算 222+255 和 1+54 以及类似的其他列表。

【问题讨论】:

    标签: python list deep-learning computer-vision


    【解决方案1】:

    你可以这样做:

    >>> x = [[222, 1, 255, 54], [105, 1, 135, 48], [397, 310, 521, 594]]
    >>> [[sum(i[::2]), sum(i[1::2])] for i in x]
    [[477, 55], [240, 49], [918, 904]]
    

    这将遍历并将偶数索引值加在一起,并与奇数索引值相加。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-25
      相关资源
      最近更新 更多