Python – 交叉元组求和分组
有时,在处理Python元组记录时,我们可能会遇到一个问题,即我们需要对元组对的第一个元素与元组的第二个相似元素执行求和分组。这类问题可以在日常编程中有应用。让我们讨论可以执行此任务的某些方式。
Input : test_list = [(1, 5), (7, 4), (9, 6), (11, 6)]
Output : [(7, 4), (1, 5), (20, 6)]
Input : test_list = [(1, 8)]
Output : [(1, 8)]
方法#1:使用循环
这是可以执行此任务的蛮力方式。在此我们检查相似的第二个元素并在此之前执行求和并执行累积分组。
# Python3 code to demonstrate working of
# Cross tuple summation grouping
# Using loop
# initializing list
test_list = [(4, 5), (7, 5), (8, 6), (10, 6), (10, 4), (6, 7), (3, 7)]
# printing original list
print("The original list is : " + str(test_list))
# Concatenate Similar Key values
# Using loop
temp = dict()
for ele1, ele2 in test_list:
temp[ele2] = temp.get(ele2, 0) + ele1
res = [(ele2, ele1) for (ele1, ele2) in temp.items()]
# printing result
print("The grouped records are : " + str(res))
输出 :
The original list is : [(4, 5), (7, 5), (8, 6), (10, 6), (10, 4), (6, 7), (3, 7)]
The grouped records are : [(10, 4), (11, 5), (18, 6), (9, 7)]
方法 #2:使用groupby() + sum() + zip()
+ 列表理解
上述功能的组合可以用来解决这个问题。在此,我们使用 sum() 执行求和的任务。分组的任务由 groupby() 完成。
# Python3 code to demonstrate working of
# Cross tuple summation grouping
# Using groupby() + sum() + zip() + list comprehension
from itertools import groupby
# initializing list
test_list = [(4, 5), (7, 5), (8, 6), (10, 6), (10, 4), (6, 7), (3, 7)]
# printing original list
print("The original list is : " + str(test_list))
# Concatenate Similar Key values
# Using groupby() + sum() + zip() + list comprehension
res = [(sum(next(zip(*ele))), key) for key, ele in groupby(
test_list, key = lambda tup:tup[1])]
# printing result
print("The grouped records are : " + str(res))
输出 :
The original list is : [(4, 5), (7, 5), (8, 6), (10, 6), (10, 4), (6, 7), (3, 7)]
The grouped records are : [(10, 4), (11, 5), (18, 6), (9, 7)]