[英]Summing Elements in Nested Lists with Condition Check
I've a list of lists, and each inner list has three objects. 我有一个列表列表,每个内部列表都有三个对象。 I'd like to sum the 1st index (2nd object) of each sublist, but only if the other two objects in the sublists are the same.
我想对每个子列表的第一个索引(第二个对象)求和,但前提是子列表中的其他两个对象相同。 For example:
例如:
list_in = [['aa', 2, 'bb'], ['aa', 2, 'bb']]
Since 'aa'
and 'bb'
are the same, I'd like to add the 2 and 2 to equal 4, and output this into a new list: 由于
'aa'
和'bb'
相同,因此我想将2和2相加等于4,并将其输出到新列表中:
list_out = ['aa', 4, 'bb']
So I need an if
statement. 所以我需要一个
if
语句。 I tried this with the following: 我尝试了以下方法:
list_out = []
if i[0]==i[0] for i in list_in:
total = [sum(list_in[1]) for i in range(len(list_in))]
list_out.append(i[0], total, i[2])
But python gives me an InvalidSyntax
error. 但是python给了我一个
InvalidSyntax
错误。 I'm unsure how to fix this. 我不确定如何解决此问题。
Then I tried to just construct a new list based on the condition that if the 1st and 3rd objects are the same, sum the 2nd objects: 然后,我尝试根据以下条件构造一个新列表:如果第一个和第三个对象相同,则将第二个对象相加:
list_out, total = [], 0
for i in list_in: # "i" is a list within the "list_in" list.
x, y, z = i[0], i[1], i[2]
if x not in list_out and z not in list_out:
list_out.append([x, y, z])
elif x in list_out and z in list_out:
total_y += y
list_out.append([x, total_y, z])
return list_out
However, this just gives me the same list as I started with. 但是,这给了我与开始时相同的列表。 Apparently the
if
and elif
statements aren't working correctly. 显然,
if
和elif
语句无法正常工作。
You can use itertools.groupby
: 您可以使用
itertools.groupby
:
import itertools
list_in = [['aa', 2, 'bb'], ['aa', 2, 'bb']]
new_list = [(a, list(b)) for a, b in itertools.groupby(sorted(list_in, key=lambda x:(x[0], x[-1])), key=lambda x:(x[0], x[-1]))]
final_data = [[a, sum(c[1] for c in d), b] for (a, b), d in new_list][0]
Output: 输出:
['aa', 4, 'bb']
itertools.groupby
allows for a more generic solution should the length of list_in
exceed two. 如果
list_in
的长度超过两个, itertools.groupby
允许使用更通用的解决方案。
Edit: the solution will work for larger lists: 编辑:该解决方案将适用于较大的列表:
list_in = [['aa', 2, 'bb'], ['aa', 2, 'bb'], ['aa', 15, 'bb']]
new_list = [(a, list(b)) for a, b in itertools.groupby(sorted(list_in, key=lambda x:(x[0], x[-1])), key=lambda x:(x[0], x[-1]))]
final_data = [[a, sum(c[1] for c in d), b] for (a, b), d in new_list][0]
Output: 输出:
['aa', 19, 'bb']
In Python, a function is an object. 在Python中,函数是对象。 A
lambda
function is known as an "anonymous function" because when its object is created, it is not bound to a name. lambda
函数被称为“匿名函数”,因为创建对象时,它没有绑定到名称。 Instead, the function object itself is returned. 而是返回函数对象本身。
I think I am doing manually what Ajax1234 does with groupby, here it comes: 我想我正在手动执行Ajax1234对groupby的操作,它来了:
list_in = [['aa', 2, 'bb'], ['aa', 2, 'bb']]
grp = {}
for inner in list_in:
key = (inner[0],inner[-1]) # tuple is immutable and can be key
grp.setdefault(key,0) # create key if needed with value 0
grp[key]+=inner[1] # add value
list_out = [[k[0],grp[k],k[1]] for k in grp] # reassemble from dictionary
print(list_out)
Output: 输出:
[['aa', 4, 'bb']]
This will also include any sublist that has NO duplicate entry. 这还将包括没有重复项的任何子列表。 Not sure if thats ok.
不确定是否可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.