在Python 2.7中:我正在测量一个进程,该进程计算从函数返回的字典的键。
显示一个基本示例,其中函数 getList()返回一个字符列表,可能是['a'],['b'],['c']或['d “];大多数列表是单个元素,但有时可能返回两个,例如['广告']。我想数一切都归来了。我想到这样做的方式如下所示:
myDict = {'a':0, 'b':0, 'c':0, 'd':0, 'error':0, 'total':0}
for key in charList:
myDict[key] += 1
myDict['total'] += 1
是否有更多的Pythonic方式,也许字典理解来计算列表中的键(长度不一样)?
import random
def getList():
'''mimics a prcoess that returns a list of chars between a - d
[most lists are single elements, though some are two elements]'''
number = (random.randint(97,101))
if number == 101:
charList = [chr(number-1), chr(random.randint(97,100))]
if charList[0] == charList[1]:
getList()
else:
charList = [chr(number)]
return charList
myDict = {'a':0, 'b':0, 'c':0, 'd':0, 'error':0, 'total':0}
for counter in range(0,5):
charList = getList()
for key in charList:
print charList, '\t', key
try:
myDict[key] += 1
myDict['total'] += 1
except:
myDict['error'] += 1
print "\n",myDict
生成的输出:
答案 0 :(得分:1)
您可以使用内置的collections.Counter
课程:https://docs.python.org/2/library/collections.html#collections.Counter
例如使用您的代码:
import collections
ctr = collections.Counter()
for ii in range(0,5):
charList = getList()
ctr.update(charList)
ctr['total'] = sum(ctr.values())
print ctr
这将打印:
Counter({'total': 7, 'd': 5, 'a': 1, 'c': 1})
答案 1 :(得分:1)
您可以使用collections.Counter
:
# You need to initialize the counter or you won't get the entry with 0 count.
myDict = collections.Counter({'a': 0, 'b': 0, 'c': 0, 'd': 0})
myDict.update(x for _ in range(0, 5) for x in getList())
# Then create the 'total' entry
myDict['total'] = sum(myDict.values())
注意:如果'error'
返回的list
包含新字符getList()
,'e'
可能会向计数器添加新密钥而不设置'f'
条目{1}},...)。
答案 2 :(得分:0)
使用collections.Counter
和双循环生成器表达式将各个元素输入计数器:
>>> lst = [['a'], ['a', 'b'], ['c'], ['c', 'd']]
>>> c = collections.Counter((y for x in lst for y in x))
>>> c
Counter({'a': 2, 'c': 2, 'b': 1, 'd': 1})
>>> c.most_common(2)
[('a', 2), ('c', 2)]
>>> sum(c.values())
6
答案 3 :(得分:0)
我能想到的最简单的方法是使用chain
展开您的列表,然后使用Counter
:让lst
成为列表[['a'], ['b'], ['c'], ['d'], ['a', 'd']]
>>> from itertools import chain
>>> from collections import Counter
>>> c = Counter(chain(*lst))
>>> c['total'] = sum(c.values())
>>> c
Counter({'total': 6, 'd': 2, 'a': 2, 'b': 1, 'c': 1})