计算列表项的出现次数

时间:2017-12-10 22:22:14

标签: python

我们假设我有以下列表 names = ['tan','2','alp','3','tan','4','alp','3','tan','1']

奇数索引元素是一些值,甚至索引元素是它们的出现次数(例如,alp发生3 + 3 = 6次)

我正在尝试制作代码。

在这里输入代码

names = ['tan','2','alp','3','tan','4','alp','3','tan','1']

i = 1;
dd = names[0::2]
nn = names[1::2]
ct = [0 for cc in range(len(dd))];
le = len(dd);

for i in range(0, le):
    ct[i] = int(nn[i])
    for j in range(i+1, le):
        if (j < le) and (dd[i] == dd[j]):
            ct[i] += int(nn[j]);
            del(dd[j])
            # del(nn[j])
            le -= 1

我得到的ct输出是[9,7,4,3,1] 但它应该是[7,6] --- 7为棕褐色,6为alp

如果我取消注释del(nn [j]) ---我将等于le,代码将停止

但是我应该在第一次之后(在ct中加入它之后)删除元素和它发生的次数,这样计数过程就不会重复了

任何想法如何做到这一点?

3 个答案:

答案 0 :(得分:2)

我们可以使用字典来比列表更容易地跟踪计数。下面我使用defaultdict,它是标准库中支持默认值的dict子类。

from collections import defaultdict

names = ['tan','2','alp','3','tan','4','alp','3','tan','1']

d=defaultdict(int)
for name, count in zip(*[iter(names)]*2):
    d[name] += int(count)

答案 1 :(得分:1)

这相对容易 - 从奇数元素构建查找映射,然后对匹配的偶数值求和。您可以使用collections.defaultdict()让您的生活更轻松:

import collections

names = ['tan', '2', 'alp', '3', 'tan', '4', 'alp', '3', 'tan', '1']

lookup_map = collections.defaultdict(int)
for element, value in zip(names[0::2], names[1::2]):
    lookup_map[element] += int(value)

print(dict(lookup_map))  # {'tan': 7, 'alp': 6}

如果您确实只需要值并需要保留订单,则可以添加其他步骤:

result = [lookup_map.pop(element) for element in names[0::2] if element in lookup_map]

print(result)  # [7, 6]

如果您在每个元素计算的出现次数/总出现次数之后,您可以在整个查找映射中应用总和:

sum_total = sum(lookup_map.values())
for name in lookup_map:
    lookup_map[name] /= sum_total

print(dict(lookup_map))  # {'alp': 0.46153846153846156, 'tan': 0.5384615384615384}

答案 2 :(得分:0)

您可以使用un object来存储每个名称的出现次数

names = ['tan','2','alp','3','tan','4','alp','3','tan','1']

# Use an object to store occurences
results = {}

# Iterate through 'names' to get occurences
for i in range(len(names)):
    if i % 2 == 0:
        # Store them in the object
        if names[i] in results: # if 'name' exist in results object
            results[names[i]] += int(names[i+1])
        else:                   # if it doesn't
            results[names[i]] = int(names[i+1])

# Get the value only array
res_arr = [v for k,v in results.items()]
print results
print res_arr

这将输出:

{'tan': 7, 'alp': 6}
[7, 6]