67

假设我有一个单词列表,并且我想找到每个单词在该列表中出现的次数。

一个明显的方法是:

words = "apple banana apple strawberry banana lemon"
uniques = set(words.split())
freqs = [(item, words.split().count(item)) for item in uniques]
print(freqs)

但我觉得这段代码不是很好,因为程序运行了两次单词列表,一次是构建集合,第二次是计算出现次数。

当然,我可以编写一个函数来遍历列表并进行计数,但这不会像 Python 那样。那么,有没有更高效和 Pythonic 的方式呢?

4

13 回答 13

146

模块中的Countercollections是专门为解决此类问题而构建的:

from collections import Counter
words = "apple banana apple strawberry banana lemon"
Counter(words.split())
# Counter({'apple': 2, 'banana': 2, 'strawberry': 1, 'lemon': 1})
于 2009-05-21T15:16:59.447 回答
95

defaultdict来救援!

from collections import defaultdict

words = "apple banana apple strawberry banana lemon"

d = defaultdict(int)
for word in words.split():
    d[word] += 1

这在 O(n) 中运行。

于 2009-05-21T15:10:59.647 回答
12
freqs = {}
for word in words:
    freqs[word] = freqs.get(word, 0) + 1 # fetch and increment OR initialize

我认为这与 Triptych 的解决方案相同,但没有导入集合。也有点像 Selinap 的解决方案,但更易读恕我直言。几乎与 Thomas Weigel 的解决方案相同,但没有使用异常。

但是,这可能比使用集合库中的 defaultdict() 慢。由于该值被获取,递增然后再次分配。而不是仅仅增加。但是使用 += 可能在内部做同样的事情。

于 2009-06-11T20:21:44.627 回答
11

标准方法:

from collections import defaultdict

words = "apple banana apple strawberry banana lemon"
words = words.split()
result = defaultdict(int)
for word in words:
    result[word] += 1

print result

Groupby oneliner:

from itertools import groupby

words = "apple banana apple strawberry banana lemon"
words = words.split()

result = dict((key, len(list(group))) for key, group in groupby(sorted(words)))
print result
于 2009-05-21T15:11:47.257 回答
7

如果您不想使用标准字典方法(循环通过列表递增正确的 dict.key),您可以试试这个:

>>> from itertools import groupby
>>> myList = words.split() # ['apple', 'banana', 'apple', 'strawberry', 'banana', 'lemon']
>>> [(k, len(list(g))) for k, g in groupby(sorted(myList))]
[('apple', 2), ('banana', 2), ('lemon', 1), ('strawberry', 1)]

它在 O(n log n) 时间内运行。

于 2009-05-21T15:09:57.370 回答
3

没有默认字典:

words = "apple banana apple strawberry banana lemon"
my_count = {}
for word in words.split():
    try: my_count[word] += 1
    except KeyError: my_count[word] = 1
于 2009-05-21T15:59:30.047 回答
2
user_input = list(input().split(' '))

for word in user_input:

    print('{} {}'.format(word, user_input.count(word)))
于 2021-08-07T07:10:09.713 回答
1
words = "apple banana apple strawberry banana lemon"
w=words.split()
e=list(set(w))       
word_freqs = {}
for i in e:
    word_freqs[i]=w.count(i)
print(word_freqs)   

希望这可以帮助!

于 2017-11-12T16:17:28.383 回答
0

你不能只使用计数吗?

words = 'the quick brown fox jumps over the lazy gray dog'
words.count('z')
#output: 1
于 2011-04-07T05:36:08.643 回答
0

我碰巧做了一些 Spark 练习,这是我的解决方案。

tokens = ['quick', 'brown', 'fox', 'jumps', 'lazy', 'dog']

print {n: float(tokens.count(n))/float(len(tokens)) for n in tokens}

**#以上输出**

{'brown': 0.16666666666666666, 'lazy': 0.16666666666666666, 'jumps': 0.16666666666666666, 'fox': 0.16666666666666666, 'dog': 0.16666666666666666, 'quick': 0.16666666666666666}
于 2015-06-26T06:02:07.303 回答
0

使用 reduce() 将列表转换为单个字典。

from functools import reduce

words = "apple banana apple strawberry banana lemon"
reduce( lambda d, c: d.update([(c, d.get(c,0)+1)]) or d, words.split(), {})

返回

{'strawberry': 1, 'lemon': 1, 'apple': 2, 'banana': 2}
于 2016-02-23T18:03:45.900 回答
0
list = input()  # Providing user input passes multiple tests
text = list.split()

for word in text:
    freq = text.count(word) 
    print(word, freq)
于 2021-10-11T01:11:02.803 回答
-1

下面的答案需要一些额外的周期,但它是另一种方法

def func(tup):
    return tup[-1]


def print_words(filename):
    f = open("small.txt",'r')
    whole_content = (f.read()).lower()
    print whole_content
    list_content = whole_content.split()
    dict = {}
    for one_word in list_content:
        dict[one_word] = 0
    for one_word in list_content:
        dict[one_word] += 1
    print dict.items()
    print sorted(dict.items(),key=func)
于 2013-02-27T02:17:20.163 回答