简体   繁体   English

如何在列表列表中求和元组的第二个元素?

[英]How do i sum the second element of a tuple within a list of lists?

I have a list of lists named allBins which contains several lists which represent different bins, inside those bins are varying numbers of tuples with the format (iD, volume). 我有一个名为allBins的列表列表,其中包含几个代表不同bin的列表,这些bin中有不同数量的元组,格式为(iD,volume)。 I need to iterate through to sum the volumes of the items in each of the bins by summing the second element of the tuples. 我需要通过对元组的第二个元素求和来迭代来对每个二进制数中的项的总和进行求和。

I've tried many things: sum(bin[1] for bin in allBins) gives me a 'list index out of range' error presumably because some bins have more than one tuple? 我尝试了很多东西: sum(bin[1] for bin in allBins)给出了一个'列表索引超出范围'的错误,大概是因为有些bin有多个元组?

allBins = [[(3,20)],[(1,11),(0,6)],[(4,16),(2,5)]]

I need a line of code that, depending on which bin I choose to sum, gives me the following integers: 我需要一行代码,根据我选择总和的bin,给出以下整数:

1st bin: 20 第一箱: 20

2nd bin: 17 第二箱: 17

3rd bin: 21 第3箱: 21

You can use a list-comprehension: 您可以使用列表理解:

allBins = [[(3,20)],[(1,11),(0,6)],[(4,16),(2,5)]]

print([sum(y[1] for y in x) for x in allBins])
# [20, 17, 21]

Treating your actual requirement: "I need some sort of loop or comprehension that, depending on which bin I choose to sum" : 处理你的实际要求: “我需要某种循环或理解,取决于我选择总和的哪个箱子”

allBins = [[(3,20)],[(1,11),(0,6)],[(4,16),(2,5)]]

bin_number = 2
print(sum(x[1] for x in allBins[bin_number-1]))
# 17

You can specify bin_number and the above finds sum for that particular bin. 您可以指定bin_number ,上面找到该特定bin的总和。

The problem you have is you want to only sum one bin, but you're trying to sum across all bins. 你遇到的问题是你只想把一个箱子加起来,但是你想要在所有箱子里求和。 This means when you access the first bin, with the value [(3,20)] and ask for element with index 1, there is only an element with index 0 and you get the out of bounds error. 这意味着当您使用值[(3,20)]访问第一个bin并请求索引为1的元素时,只有一个索引为0的元素,您将获得越界错误。

You want something like this: 你想要这样的东西:

def sum_bin(bin_num, data):
    my_bin = data[bin_num]
    return sum(t[1] for t in my_bin)

>>> sum_bin(0, allBins)
20
>>> sum_bin(1, allBins)
17
>>> sum_bin(2, allBins)
21

as a "one liner", assuming you have a variable capturing the bin you want 作为“一个班轮”,假设您有一个变量捕获您想要的bin

sum(t[1] for t in allBins[bin_idx])

This is called a generator comprehension , and while it's similar to a list comprehension, there are subtle differences. 这被称为生成器理解 ,虽然它类似于列表理解,但存在细微差别。

You were close :-) Just put the summing fragment you gave inside a list comprehension so that it runs one summation per bin. 你很接近:-)只需将你给出的求和片段放在列表理解中,这样它就会为每个bin运行一个求和。

FWIW, you can also use operator.itemgetter() for a beautiful, functional approach: FWIW,你也可以使用operator.itemgetter()来获得一个漂亮的功能方法:

>>> from operator import itemgetter

>>> allBins = [[(3,20)],[(1,11),(0,6)],[(4,16),(2,5)]]
>>> [sum(map(itemgetter(1), bin)) for bin in allBins]
[20, 17, 21]

Read this as, "make a list of sums for every bin in all bins" where the sums are "sum of item one in each tuple in a bin". 读这个,“为所有箱中的每个箱子制作一个总和列表”,其中总和是“箱子中每个元组中第一项的总和”。

Once iterating over the main list, you can use sum to add up the integers. 迭代主列表后,可以使用sum来加总整数。

allBins = [[(3,20)],[(1,11),(0,6)],[(4,16),(2,5)]]

def sumup(which, allBins):
    return sum(tup[1] for tup in allBins[which])

print(sumup(1, allBins))

Doc: sum built-in Doc: 内置总和

for c,b in enumerate(allBins):
    if c == bin_you_choose_to_sum:
        partial=0
        for t in b:
            partial+=t[1]
        print("Bin {}: {}".format(c, partial))

You can use the following function: 您可以使用以下功能:

from operator import itemgetter

allBins = [[(3,20)],[(1,11),(0,6)],[(4,16),(2,5)]]

def func(bin_num, all_bins):
    bin = itemgetter(bin_num)(all_bins)
    s = sum(map(itemgetter(-1), bin))
    return s

print(func(2, allBins))
# 21

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将元组中的一组列表中的第一个值相加? - How do I sum the first value in a set of lists within a tuple? 如何将列表中的元组(在一个元组内)转换为列表? - How do I convert a tuple within a list (within a tuple) to a list? 如何对元组对列表求和? - How do I sum a list of tuple pairs? 给定一个元组列表,如果第一个元素相同,我如何在每个相似的元组中添加第二个元素 - Given a list of tuples, if the first element is the same, how do i add the second element inside each similar tuple 如何获得列表中所有列表组合的总和? - how do I get the sum of combinations of all lists within lists? Python元组列表中第二个和第三个元素的和,按第一个分组 - Python sum of second and third element of tuple in list of tuples grouped by first 如何将列表中的每个元素与嵌套列表相乘并得到总和? - How do I multiply each element in a list with nested lists and get the sum? 如何总结元组中的数字,即列表中的数字? - How do I sum up the numbers in a tuple, thats in a list? 如何按第二个元组元素对元组列表进行排序? - How to sort a list of tuples, by the second tuple element? 如何将元组分配给 function 参数并打印该元组的第二个元素? - How do I assign tuple to function parameter and print second element of that tuple?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM