简体   繁体   English

如何在python中合并多个列表

[英]how to merge multiple lists in python

I have multiple lists like below 我有多个列表,如下所示

[u'a', 11, u'P']
[u'a', 11, u'A']
[u'b', 2, u'P']
[u'c', 1, u'P']
[u'c', 2, u'P']
[u'd', 1, u'P']
[u'e', 3, u'P']
[u'f', 2, u'P']
[u'a', 1, u'P']
[u'a', 2, u'P']
[u'b', 1, u'P']
[u'b', 11, u'P']

how to merge above lists,to loop the list to add up like below 如何合并上面的列表,循环列表添加如下

[u'a', 11, u'P'] + [u'a', 2, u'P'] + [u'a', 11, u'A'] = ['a',('P' : 13) ,('A': 11)]

[u'b', 2, u'P'] + [u'b', 1, u'P'] + [u'b', 11, u'P'] = ['b',14,p]

output should like below : 输出应如下:

['a',('P' : 13) ,('A': 11)]
['b',14,'p']

You could consider using collections.defaultdict and iterating through the list of dicts in the values. 您可以考虑使用collections.defaultdict并迭代值中的dicts列表。

import collections
d = collections.defaultdict(list)
l = [[u'a', 11, u'P'],[u'a', 11, u'A'],[u'a', 3, u'P'],[u'b', 2, u'P'],[u'c', 1, u'P'],[u'c', 2, u'P'],[u'd', 1, u'P'],[u'e', 3, u'P']]
for k1, v, k2 in l:
    if k1 in d:
            d[k1].append({k2:v})
    else: 
        d[k1] = [{k2:v}]

newdict = {}
for key,value in d.items():
    newvalue = {}
    for valuedict in value:
        for key2,value2 in valuedict.items():
            if key2 in newvalue:
                newvalue[key2] += value2
            else:
                newvalue[key2] = value2
    newdict[key] = newvalue

print newdict

And this would give you 这会给你

{u'a': {u'A': 11, u'P': 14}, u'c': {u'P': 3}, u'b': {u'P': 2}, u'e': {u'P': 3}, u'd': {u'P': 1}}

The output that you want looks slightly strange due to the inconsistency between the two cases. 由于两种情况之间的不一致,您想要的输出看起来有点奇怪。 You could trivially change this example to get whatever output you want, however: 您可以通过简单地更改此示例来获得所需的输出,但是:

lists = [
 [u'a', 11, u'P'],
 [u'a', 11, u'A'],
 [u'b', 2, u'P'],
 [u'c', 1, u'P'],
 [u'c', 2, u'P'],
 [u'd', 1, u'P'],
 [u'e', 3, u'P'],
 [u'f', 2, u'P'],
 [u'a', 1, u'P'],
 [u'a', 2, u'P'],
 [u'b', 1, u'P'],
 [u'b', 11, u'P']]

# Each key in this dictionary will be one of the first elements
# from the lists shown above.  The values will be dictionaries
# mapping a letter (one of the third elements in each list) to
# their total count (i.e. the sum of the second elements matching
# the other two columns)
from collections import defaultdict
results = defaultdict(dict)

for main_key, count, subkey in lists:
    d = results[main_key]
    d[subkey] = d.get(subkey,0) + count

for main_key, values in results.items():
    print main_key, "=>", values

The output is: 输出是:

a => {u'A': 11, u'P': 14}
c => {u'P': 3}
b => {u'P': 14}
e => {u'P': 3}
d => {u'P': 1}
f => {u'P': 2}

Update: Thanks to sharjeel for suggesting in the comment below that I remove the setdefault by using defaultdict instead. 更新:感谢sharjeel在下面的评论中建议我使用defaultdict删除setdefault


Update 2: In your further question in the comments below, you indicate that instead you want as output "[a] set of lists like [[u'a', 11, u'P'], [u'a', 11, u'A'] ". 更新2:在下面评论中的进一步问题中,您指出您想要输出“[a]列表,如[[u'a', 11, u'P'], [u'a', 11, u'A'] “。 (I'm assuming for the moment that you mean a list of lists rather than a set, but that's almost as easy.) In order to construct such a list of lists , you could replace the loop that prints the values with the following: (我现在假设你的意思是列表而不是集合,但这几乎一样容易。)为了构建这样的列表列表,你可以用以下代码替换打印值的循环:

lists_output = []

for main_key, values in results.items():
    for subkey, count in values.items():
       lists_output.append([main_key,count,subkey])

print lists_output

... which will give the output: ...将给出输出:

[[u'a', 11, u'A'], [u'a', 14, u'P'], [u'c', 3, u'P'], [u'b', 14, u'P'], [u'e', 3, u'P'],
 [u'd', 1, u'P'], [u'f', 2, u'P']]

Its a one liner solution if you use groupby from itertools. 如果你使用itertools的groupby,它是一个单线解决方案。

Put all the lists in one list say lst. 把所有列表放在一个列表中说lst。

lst = [
    [u'a', 11, u'P']
    [u'a', 11, u'A']
    [u'b', 2, u'P']
    [u'c', 1, u'P']
    [u'c', 2, u'P']
    [u'd', 1, u'P']
    [u'e', 3, u'P']
    [u'f', 2, u'P']
    [u'a', 1, u'P']
    [u'a', 2, u'P']
    [u'b', 1, u'P']
    [u'b', 11, u'P']
]

Now use group by in outer elements to group over a,b,c etc. then within each grouped data group over the third element ie P,A etc. The further grouped data needs to be summed. 现在在外部元素中使用group by来分组a,b,c等,然后在第三个元素上的每个分组数据组内,即P,A等。需要对进一步分组的数据求和。

Here is the solution: 这是解决方案:

from itertools import groupby
result = dict(
                ( k, dict( (k1, sum([i[1] for i in g2])) for k1, g2 in groupby(g, key=lambda y: y[2] ) ) )
                for k, g in groupby(lst, key=lambda x: x[0])
            )

To have a better understanding, I would recommend you to play around with single groupby then jump to nested groupby. 为了更好地理解,我建议你玩单个组然后跳转到嵌套groupby。

Here are a few links: 以下是一些链接:

http://docs.python.org/library/itertools.html#itertools.groupby http://docs.python.org/library/itertools.html#itertools.groupby

http://www.builderau.com.au/program/python/soa/Python-groupby-the-iterator-swiss-army-knife/0,2000064084,339280431,00.htm http://www.builderau.com.au/program/python/soa/Python-groupby-the-iterator-swiss-army-knife/0,2000064084,339280431,00.htm

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM