如果共享公共密钥,如何聚合一组子列表中的值?

时间:2014-03-21 02:45:30

标签: python list sublist

如果子列表与索引1处的另一个子列表具有相同的密钥,您将如何聚合以下列表中的第三个索引?

 lst = [['aaa','key1','abc',4],['aaa','key2','abc',4],['ddd','key3','abc',4],['eas','key1','abc',4],['aaa','key1','abc',2],['aaa','key2','abc',10]]

我想仅在具有相同索引的子列表中聚合第三个索引。例如,上面的列表在三个子列表中的索引1处具有key1。我想将4,4和2加在一起。

Desired_List = [['aaa','key1','abc',10],['aaa','key2','abc',14],['ddd','key3','abc',4]]

列表中的其他项目无关紧要。

4 个答案:

答案 0 :(得分:2)

嗯,这听起来并不太可读 - 但这是itertools.groupbyreduce的一种非常紧凑的方式:

from itertools import groupby
from operator import itemgetter as ig

[reduce(lambda x,y: x[:-1] + [x[-1] + y[-1]], g) for k,g in groupby(sorted(lst, key=ig(1)), ig(1))]
Out[26]: 
[['aaa', 'key1', 'abc', 10],
 ['aaa', 'key2', 'abc', 14],
 ['ddd', 'key3', 'abc', 4]]

如果你将lambda拉成一个辅助函数,事情会变得更好:

def helper(agg,x):
    agg[-1] += x[-1]
    return agg

[reduce(helper,g) for k,g in groupby(sorted(lst, key=ig(1)), ig(1))]
Out[30]: 
[['aaa', 'key1', 'abc', 10],
 ['aaa', 'key2', 'abc', 14],
 ['ddd', 'key3', 'abc', 4]]

请注意,您需要在python 3中执行from functools import reduce,因为它已从内置版(悲伤的脸)中消失。

答案 1 :(得分:1)

如果您想要哪个键或索引值变化,我创建了一个变量,因此很容易更改此代码:

lst = [['aaa','key1','abc',4],['aaa','key2','abc',4],['ddd','key3','abc',4],['eas','key1','abc',4],['aaa','key1','abc',2],['aaa','key2','abc',10]]
# Index of the key value to sort on
key_index = 1
# Index of the value to aggregate
value_index = 3

list_dict = {}

# Iterate each list and uniquely identify it by its key value.
for sublist in lst:
    if sublist[1] not in list_dict:
        list_dict[sublist[key_index]] = sublist
    # Add the value of the list to the unique entry in the dict
    list_dict[sublist[key_index]][value_index] += sublist[value_index]

# Now turn it into a list. This is not needed but I was trying to match the output
desired_list = [ sublist for _, sublist in list_dict.iteritems()]

输出:

[['ddd', 'key3', 'abc', 8], ['aaa', 'key2', 'abc', 18], ['aaa', 'key1', 'abc', 14]]

答案 2 :(得分:1)

非常难看,但这很有效:

lst = [['aaa','key1','abc',4],['aaa','key2','abc',4],['ddd','key3','abc',4],['eas','key1','abc',4],['aaa','key1','abc',2],['aaa','key2','abc',10]]
newlst = []
searched = []
for i, sublist1 in enumerate(lst[0:len(lst)-1]):
    if sublist1[1] not in searched:
        searched.append(sublist1[1])
        total = 0
        for sublist2 in lst[i+1:]:
            if sublist1[1] == sublist2[1]:
                total += int(sublist2[3])
        newlst.append([sublist1[0], sublist1[1], sublist1[2], total + sublist1[3]])

print newlst

给出:

[['aaa', 'key1', 'abc', 10], ['aaa', 'key2', 'abc', 14], ['ddd', 'key3', 'abc', 4]]

答案 3 :(得分:0)

lst = [['aaa','key1','abc',4],['aaa','key2','abc',4],['ddd','key3','abc',4],['eas','key1','abc',4],['aaa','key1','abc',2],['aaa','key2','abc',10]]
result = []
for item in lst:
    t = tuple(item[:3])
    d = {t:item[-1]}
    result.append(d)
print result