简体   繁体   English

python:比较这些列表的快速简便的方法?

[英]python: fast and easy way to compare these lists?

I wrote a function for this, but I think its probably wildly inefficient and over-complicated so I wanted to ask if there was an easy way to do it. 我为此编写了一个函数,但我认为它可能效率极低且过于复杂,因此我想问一问是否有一种简便的方法来实现。

Given two lists of lists... 给定两个列表列表...

foo = [['one', 1], ['two', 1], ['three', 1]]
bar = [['three', 1], ['four', 1], ['five', 1]]

I need a function that will return... 我需要一个返回的函数...

final = [['one', 1], ['two', 1], ['three', 2], ['four', 1], ['five', 1]]

so it checks if there is any overlaps of the first term, adds the second terms together and then returns a final list like above 因此它会检查第一项是否有任何重叠,将第二项相加,然后返回如上的最终列表

EDIT: 编辑:

foo/bar[1:] are guaranteed to be ordered, but they could be like this... foo / bar [1:]可以保证被订购,但是它们可能像这样...

foo = [['the', 100], ['at', 99], ['for', 32]]
bar = [['mitochondria', 20], ['at', 10], ['you', 9]]

In other words, they would be relatively random words paired with descending numbers. 换句话说,它们将是相对随机的单词,并带有降序编号。

>>> foo = [['one', 1], ['two', 1], ['three', 1]]
>>> bar = [['three', 1], ['four', 1], ['five', 1]]
>>> from collections import Counter
>>> Counter(dict(foo)) + Counter(dict(bar))
Counter({'three': 2, 'four': 1, 'five': 1, 'two': 1, 'one': 1})

so 所以

>>> (Counter(dict(foo)) + Counter(dict(bar))).items()
[('four', 1), ('five', 1), ('three', 2), ('two', 1), ('one', 1)]

if the order is important: 如果顺序很重要:

>>> from collections import OrderedDict
>>> counter = (Counter(dict(foo)) + Counter(dict(bar)))
>>> order = OrderedDict(foo + bar).keys()
>>> [[k, counter[k]] for k in order]
[['one', 1], ['two', 1], ['three', 2], ['four', 1], ['five', 1]]

If the items are gathered into a list L 如果将项目收集到列表中L

>>> foo = [['one', 1], ['two', 1], ['three', 1]]
>>> bar = [['three', 1], ['four', 1], ['five', 1]]
>>> from collections import Counter
>>> from collections import OrderedDict
>>> from itertools import chain
>>> L = [foo, bar]
>>> counter = Counter()
>>> for item in L:
...     counter.update(dict(item))
... 
>>> order = OrderedDict(chain.from_iterable(L))
>>> [[k, counter[k]] for k in order]
[['one', 1], ['two', 1], ['three', 2], ['four', 1], ['five', 1]]

Basically, you merely should concatenate the lists, sort the result, and run over it while counting repeated elements. 基本上,您只应串联列表,对结果进行排序并在计算重复元素的同时对其进行遍历。 Maybe itertools.groupby could help: https://docs.python.org/2/library/itertools.html#itertools.groupby 也许itertools.groupby可以帮助您: https : itertools.groupby

f = [('one',2), ('two',3)]

g = [('one',2), ('three',4)]

print set(f) | set(g)

set([('three', 4), ('one', 2), ('two', 3)])
[Finished in 0.2s]

maybe more easier. 也许更容易。 在此处输入图片说明

You could use a default dictionary to do this: 您可以使用默认字典来执行此操作:

from collections import defaultdict

foo = [['one', 1], ['two', 1], ['three', 1]]
bar = [['three', 1], ['four', 1], ['five', 1]]

mydict = defaultdict(int)

for each in foo+bar:
    mydict[each[0]]+=each[1]

foobar = [[x,y] for x,y in mydict.items()]

By using a default dictionary you insure that if the first value is not already in your dictionary you don't get a key error. 通过使用默认字典,可以确保如果字典中尚未包含第一个值,则不会出现键错误。

If your list maintain a predictable structure like you have posted you should be able to reliably index them when applying them to your dictionary. 如果您的清单像您发布的那样保持可预测的结构,则在将它们应用于字典时,您应该能够可靠地对其进行索引。

This list comprehension at the bottom gives you back the structure that you started with 底部的列表理解为您提供了开始时的结构

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

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