Python Using a dictionary to count the items in a list
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3496518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a dictionary to count the items in a list
提问by Sophie
I'm new to Python and I have a simple question, say I have a list of items:
I'm new to Python and I have a simple question, say I have a list of items:
['apple','red','apple','red','red','pear']
Whats the simpliest way to add the list items to a dictionary and count how many times the item appears in the list.
Whats the simpliest way to add the list items to a dictionary and count how many times the item appears in the list.
So for the list above I would like the output to be:
So for the list above I would like the output to be:
{'apple': 2, 'red': 3, 'pear': 1}
回答by mechanical_meat
>>> L = ['apple','red','apple','red','red','pear']
>>> from collections import defaultdict
>>> d = defaultdict(int)
>>> for i in L:
... d[i] += 1
>>> d
defaultdict(<type 'int'>, {'pear': 1, 'apple': 2, 'red': 3})
回答by Nick T
L = ['apple','red','apple','red','red','pear']
d = {}
[d.__setitem__(item,1+d.get(item,0)) for item in L]
print d
Gives {'pear': 1, 'apple': 2, 'red': 3}
Gives {'pear': 1, 'apple': 2, 'red': 3}
回答by Odomontois
回答by Stefano Palazzo
I always thought that for a task that trivial, I wouldn't want to import anything. But i may be wrong, depending on collections.Counter being faster or not.
I always thought that for a task that trivial, I wouldn't want to import anything. But i may be wrong, depending on collections.Counter being faster or not.
items = "Whats the simpliest way to add the list items to a dictionary "
stats = {}
for i in items:
if i in stats:
stats[i] += 1
else:
stats[i] = 1
# bonus
for i in sorted(stats, key=stats.get):
print("%d×'%s'" % (stats[i], i))
I think this may be preferable to using count(), because it will only go over the iterable once, whereas count may search the entire thing on every iteration. I used this method to parse many megabytes of statistical data and it always was reasonably fast.
I think this may be preferable to using count(), because it will only go over the iterable once, whereas count may search the entire thing on every iteration. I used this method to parse many megabytes of statistical data and it always was reasonably fast.
回答by mmmdreg
I like:
I like:
counts = dict()
for i in items:
counts[i] = counts.get(i, 0) + 1
.get allows you to specify a default value if the key does not exist.
.get allows you to specify a default value if the key does not exist.
回答by riviera
How about this:
How about this:
src = [ 'one', 'two', 'three', 'two', 'three', 'three' ]
result_dict = dict( [ (i, src.count(i)) for i in set(src) ] )
This results in
This results in
{'one': 1, 'three': 3, 'two': 2}
{'one': 1, 'three': 3, 'two': 2}
回答by Pradyot
Consider collections.Counter (available from python 2.7 onwards). https://docs.python.org/2/library/collections.html#collections.Counter
Consider collections.Counter (available from python 2.7 onwards). https://docs.python.org/2/library/collections.html#collections.Counter
回答by Ashish Kumar Verma
Simply use list property count\
Simply use list property count\
i = ['apple','red','apple','red','red','pear']
d = {x:i.count(x) for x in i}
print d
output :
output :
{'pear': 1, 'apple': 2, 'red': 3}

