[英]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.