Python:使用另一个列表中的索引汇总列表中的数据

时间:2016-05-20 21:58:32

标签: python list

我有两个清单:

L1 = ['A','B','A','C','A']
L2 = [1,   4,  6,  1,  3]

我想创建一个具有以下输出的字典:

DictOutSum = {'A':10, 'B':4, 'C':1}
DictOutCount = {'A':3, 'B':1, 'C':1}

即。列表L1和L2都具有相同数量的元素,并且它们中的元素一一对应。我希望在L1中为L1中的每个唯一元素找到所有数字的总和,并从中创建一个字典(DictOutSum)。我还想创建另一个字典来存储L1(DictOutCount)的唯一元素数量。

除了使用for循环之外,我甚至不知道从哪里开始。

5 个答案:

答案 0 :(得分:2)

纯python实现:

>>> dict_sum = dict.fromkeys(L1, 0)
>>> dict_count = dict.fromkeys(L1, 0)
>>> for k,n in zip(L1, L2):
...    dict_sum[k] += n
...    dict_count[k] += 1
...     
>>> dict_sum
{'A': 10, 'B': 4, 'C': 1}
>>> dict_count
{'A': 3, 'B': 1, 'C': 1}

花哨的单线实施:

>>> from collections import Counter
>>> Counter(L1)  # dict_count
Counter({'A': 3, 'B': 1, 'C': 1})
>>> sum((Counter({k:v}) for k,v in zip(L1, L2)), Counter())  # dict_sum
Counter({'A': 10, 'B': 4, 'C': 1})

答案 1 :(得分:1)

您应该使用zip内置功能

import collections

DictOutSum = collections.defaultdict(int)
DictOutCount = collections.defaultdict(int)

for l1, l2 in zip(L1, L2):
    DictOutSum[l1] += l2
    DictOutCount[l1] += 1

答案 2 :(得分:1)

>>> L1 = ['A','B','A','C','A']
>>> L2 = [1,   4,  6,  1,  3]
>>>
>>> DictOutCount = {v:0 for v in L1}
>>> DictOutSum = {v:0 for v in L1}
>>> for v1,v2 in zip(L1,L2):
...   DictOutCount[v1] += 1
...   DictOutSum[v1] += v2
...
>>>
>>> DictOutCount
{'A': 3, 'C': 1, 'B': 1}
>>> DictOutSum
{'A': 10, 'C': 1, 'B': 4}
>>>

答案 3 :(得分:1)

超级基本方式

L1 = ['A','B','A','C','A']
L2 = [1,   4,  6,  1,  3]

# Carries the information
myDict = {}

# Build the dictionary
for x in range(0,len(L1)):

    # Initialize the dictionary IF the key doesn't exist
    if L1[x] not in myDict:
        myDict[L1[x]] = {}
        myDict[L1[x]]['sum'] = 0
        myDict[L1[x]]['count'] = 0

    # Collect the information you need
    myDict[L1[x]][x] = L2[x]
    myDict[L1[x]]['sum'] += L2[x]
    myDict[L1[x]]['count'] += 1

# Build the other two dictionaries
DictOutSum = {}
DictOutCount = {}

# Literally feed the data
for element in myDict:
    DictOutSum[element] = myDict[element]['sum']
    DictOutCount[element] = myDict[element]['count']

print DictOutSum
# {'A': 10, 'C': 1, 'B': 4}

print DictOutCount
# {'A': 3, 'C': 1, 'B': 1}

旁注:您的用户名是波斯语吗?

答案 4 :(得分:0)

DictOutCount ,使用collections.Counter

import collections
DictOutCount = collections.Counter(L1)

print(DictOutCount)
Counter({'A': 3, 'C': 1, 'B': 1})

DictOutSum

DictOutSum = dict()
for k, v in zip(L1, L2):
    DictOutSum[k] = DictOutSum.get(k, 0) + v

print(DictOutSum)
# Output
{'A': 10, 'C': 1, 'B': 4}

以前的答案,DictOutSum,

import itertools
import operator
import functools

DictOutSum  = dict()
for name, group in itertools.groupby(sorted(itertools.izip(L1, L2)), operator.itemgetter(0)):
    DictOutSum[name] = functools.reduce(operator.add, map(operator.itemgetter(1), group))

print(DictOutSum)
{'A': 10, 'C': 1, 'B': 4}

主要步骤是:

  • 使用itertools.izip创建一个迭代器,用于聚合L1L2
  • 中每个元素的元素
  • 使用itertools.groupby创建一个迭代器,从迭代中返回连续的键和组(在此之前排序)
  • 使用functools.reduce进行累计添加