如何降低阵列的数据复制

问题描述 投票:2回答:2

什么是减少数据的重复最简单,最有效的方法。

我试图让一个算法,但它开始得到途径复杂。

我有保持在阵列这样的数据:[[data, 'country_code',value],[data, 'country_code',value],[data, 'country_code',value],[data, 'country_code',value]]

比如我有[[2019-01-23, "GER", 200],[2019-01-23,"USA",300],[2019-01-23,"GER", 301]].我需要:

[[2019-01-23,"GER", 501],[2019-01-23,"USA",300]]
python-3.x
2个回答
4
投票

defaultdict积累,并用一个列表理解收集的结果:

>>> from collections import defaultdict
>>> d = defaultdict(int)
>>> for date, code, n in L:
...     d[date, code] += n
...     
>>> [[date, code, n] for [[date, code], n] in d.items()]
[['2019-01-23', 'GER', 501], ['2019-01-23', 'USA', 300]]

1
投票

最惯用的方式做到这一点是使用Counter,从collections库:

>>> from collections import Counter
>>> data = [
...     ['2019-01-23', 'GER', 200],
...     ['2019-01-23', 'USA', 300],
...     ['2019-01-23', 'GER', 301],
... ]
>>> counter = Counter()
>>> for date, country_code, count in data:
...     counter[(date, country_code)] += count
...
>>> counter
Counter({('2019-01-23', 'GER'): 501, ('2019-01-23', 'USA'): 300})
>>> output_data = [[date, country_code, count] for (date, country_code), count in counter.items()]
>>> output_data
[['2019-01-23', 'USA', 300], ['2019-01-23', 'GER', 501]]
© www.soinside.com 2019 - 2024. All rights reserved.