亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何找到列表中最常見的元素?

如何找到列表中最常見的元素?

aluckdog 2019-10-19 16:43:45
給出以下列表['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.', '']我正在嘗試計算每個單詞出現多少次并顯示前3位。但是,我只想查找首字母大寫的前三位,而忽略所有首字母大寫的單詞。我敢肯定有比這更好的方法,但是我的想法是做以下事情:將列表中的第一個單詞放入另一個稱為uniquewords的列表中從原始列表中刪除第一個單詞及其所有重復單詞將新的第一個單詞添加到唯一單詞中從原始列表中刪除第一個單詞及其所有重復單詞。等等...直到原始列表為空。計算唯一單詞中每個單詞出現在原始列表中的次數找到前三名并打印
查看完整描述

3 回答

?
慕碼人8056858

TA貢獻1803條經驗 獲得超6個贊

如果您使用的是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']

熱門提示:每當您要使用這樣的算法時,交互式Python解釋器就是您的朋友。只需將其鍵入并觀看即可,并檢查整個過程中的元素。


查看完整回答
反對 回復 2019-10-19
?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

在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)

結果:


[('Jellicle', 6), ('Cats', 5), ('And', 2)]

我對編程很陌生,所以請嘗試以最準系統的方式進行。


您可以改用字典來完成此操作,其中的鍵是一個單詞,值是該單詞的計數。首先遍歷單詞,如果不存在則將其添加到字典中;否則,如果單詞存在,則增加單詞的計數。然后,要找到O(n*log(n))前三個元素,可以使用簡單的排序算法并從結果中獲取前三個元素,也可以使用O(n)僅記住前三個元素即可掃描列表的算法。


對于初學者來說,一個重要的觀察結果是,通過使用為此目的而設計的內置類,您可以節省很多工作和/或獲得更好的性能。熟悉標準庫及其提供的功能是很好的。


查看完整回答
反對 回復 2019-10-19
?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

僅返回包含最常用單詞的列表:


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

打?。?/p>


['you', 'i', 'a']

“ 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)]`

“ the word for word, word_counter in”僅提取元組的第一個成員。


查看完整回答
反對 回復 2019-10-19
  • 3 回答
  • 0 關注
  • 444 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號