use*_*180 32 python list frequency
鉴于以下列表
['Jellicle', 'Cats', 'are', 'black', 'and', 'white,', 'Jellicle', 'Cats',
'are', 'rather', 'small;', 'Jellicle', 'Cats', 'are', 'merry', 'and',
'bright,', 'And', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.',
'Jellicle', 'Cats', 'have', 'cheerful', 'faces,', 'Jellicle', 'Cats',
'have', 'bright', 'black', 'eyes;', 'They', 'like', 'to', 'practise',
'their', 'airs', 'and', 'graces', 'And', 'wait', 'for', 'the', 'Jellicle',
'Moon', 'to', 'rise.', '']
Run Code Online (Sandbox Code Playgroud)
我试图计算每个单词出现的次数并显示前3个.
但是我只想找到第一个字母大写的前三个,并忽略所有没有首字母大写的单词.
我相信有比这更好的方法,但我的想法是做以下事情:
Mar*_*ers 64
在Python 2.7及更高版本中,有一个名为Counter的类可以帮助您:
from collections import Counter
words_to_count = (word for word in word_list if word[:1].isupper())
c = Counter(words_to_count)
print c.most_common(3)
Run Code Online (Sandbox Code Playgroud)
结果:
[('Jellicle', 6), ('Cats', 5), ('And', 2)]
Run Code Online (Sandbox Code Playgroud)
我对编程很陌生,所以请尝试以最准确的方式进行编程.
您可以使用字典来执行此操作,其中键是单词,值是该单词的计数.如果它们不存在,首先迭代将它们添加到字典中的单词,否则如果它存在则增加该单词的计数.然后,要找到前三个,您可以使用简单的O(n*log(n))排序算法并从结果中获取前三个元素,或者您可以使用O(n)扫描列表的算法一次只记住前三个元素.
初学者的一个重要观察是,通过使用专为此目的而设计的内置类,您可以节省大量工作和/或获得更好的性能.熟悉标准库及其提供的功能是很好的.
Joh*_*web 17
如果您使用的是早期版本的Python,或者您有充分的理由推出自己的单词计数器(我想听听它!),您可以尝试使用以下方法dict.
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> word_list = ['Jellicle', 'Cats', 'are', 'black', 'and', 'white,', 'Jellicle', 'Cats', 'are', 'rather', 'small;', 'Jellicle', 'Cats', 'are', 'merry', 'and', 'bright,', 'And', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.', 'Jellicle', 'Cats', 'have', 'cheerful', 'faces,', 'Jellicle', 'Cats', 'have', 'bright', 'black', 'eyes;', 'They', 'like', 'to', 'practise', 'their', 'airs', 'and', 'graces', 'And', 'wait', 'for', 'the', 'Jellicle', 'Moon', 'to', 'rise.', '']
>>> word_counter = {}
>>> for word in word_list:
... if word in word_counter:
... word_counter[word] += 1
... else:
... word_counter[word] = 1
...
>>> popular_words = sorted(word_counter, key = word_counter.get, reverse = True)
>>>
>>> top_3 = popular_words[:3]
>>>
>>> top_3
['Jellicle', 'Cats', 'and']
Run Code Online (Sandbox Code Playgroud)
热门提示:只要您想使用这样的算法,交互式Python解释器就是您的朋友.只需键入它并观察它,沿途检查元素.
unl*_*kme 14
要返回包含最常用单词的列表:
from collections import Counter
words=["i", "love", "you", "i", "you", "a", "are", "you", "you", "fine", "green"]
most_common_words= [word for word, word_count in Counter(words).most_common(3)]
print most_common_words
Run Code Online (Sandbox Code Playgroud)
这打印:
['you', 'i', 'a']
Run Code Online (Sandbox Code Playgroud)
" most_common(3)"中的3 表示要打印的项目数.
Counter(words).most_common()返回一个元组列表,每个元组将该单词作为第一个成员,频率作为第二个成员.元组按单词的频率排序.
`most_common = [item for item in Counter(words).most_common()]
print(str(most_common))
[('you', 4), ('i', 2), ('a', 1), ('are', 1), ('green', 1), ('love',1), ('fine', 1)]`
Run Code Online (Sandbox Code Playgroud)
"the word for word, word_counter in",只提取元组的第一个成员.
这不是......
word_list=['Jellicle', 'Cats', 'are', 'black', 'and', 'white,', 'Jellicle', 'Cats',
'are', 'rather', 'small;', 'Jellicle', 'Cats', 'are', 'merry', 'and',
'bright,', 'And', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.',
'Jellicle', 'Cats', 'have', 'cheerful', 'faces,', 'Jellicle', 'Cats',
'have', 'bright', 'black', 'eyes;', 'They', 'like', 'to', 'practise',
'their', 'airs', 'and', 'graces', 'And', 'wait', 'for', 'the', 'Jellicle',
'Moon', 'to', 'rise.', '']
from collections import Counter
c = Counter(word_list)
c.most_common(3)
Run Code Online (Sandbox Code Playgroud)
哪个应该输出
[('Jellicle', 6), ('Cats', 5), ('are', 3)]
有两种标准库方法可以查找列表中最常见的值:
from statistics import mode
most_common = mode([3, 2, 2, 2, 1, 1]) # 2
most_common = mode([3, 2]) # StatisticsError: no unique mode
Run Code Online (Sandbox Code Playgroud)
collections.Counter.most_common:
from collections import Counter
most_common, count = Counter([3, 2, 2, 2, 1, 1]).most_common(1)[0] # 2, 3
(most_common_1, count_1), (most_common_2, count_2) = Counter([3, 2, 2]).most_common(2) # (2, 2), (3, 1)
Run Code Online (Sandbox Code Playgroud)
所以就这个问题而言,第二个是正确的选择。顺便说一句,两者在性能方面是相同的。
一个简单的两行解决方案,不需要任何额外的模块,如下代码:
lst = ['Jellicle', 'Cats', 'are', 'black', 'and','white,',
'Jellicle', 'Cats','are', 'rather', 'small;', 'Jellicle',
'Cats', 'are', 'merry', 'and','bright,', 'And', 'pleasant',
'to','hear', 'when', 'they', 'caterwaul.','Jellicle',
'Cats', 'have','cheerful', 'faces,', 'Jellicle',
'Cats','have', 'bright', 'black','eyes;', 'They', 'like',
'to', 'practise','their', 'airs', 'and', 'graces', 'And',
'wait', 'for', 'the', 'Jellicle','Moon', 'to', 'rise.', '']
lst_sorted=sorted([ss for ss in set(lst) if len(ss)>0 and ss.istitle()],
key=lst.count,
reverse=True)
print lst_sorted[0:3]
Run Code Online (Sandbox Code Playgroud)
输出:
['Jellicle', 'Cats', 'And']
Run Code Online (Sandbox Code Playgroud)
方括号中的术语返回列表中所有不为空且以大写字母开头的唯一字符串。sorted()然后,该函数按照它们在列表中出现的频率(通过使用lst.count键)以相反的顺序对它们进行排序。
nltk很方便很多语言处理.它有内置频率分配的方法.例如:
import nltk
fdist = nltk.FreqDist(your_list) # creates a frequency distribution from a list
most_common = fdist.max() # returns a single element
top_three = fdist.keys()[:3] # returns a list
Run Code Online (Sandbox Code Playgroud)