是否可以使用count方法來計算兩個對象的出現?使用count方法來計數bigrams
例如,如果我有作爲
list = ['apple', 'red', 'potato', 'apple', 'red']
我可以做
word = list[0] + ',' + ' ' + list[1]
list.count(word)
這樣的列表,並使其返回2?
我知道,Python有NTLK,但我不允許使用字典存儲單詞的所有對作爲鍵和多少使用它爲項目
是否可以使用count方法來計算兩個對象的出現?使用count方法來計數bigrams
例如,如果我有作爲
list = ['apple', 'red', 'potato', 'apple', 'red']
我可以做
word = list[0] + ',' + ' ' + list[1]
list.count(word)
這樣的列表,並使其返回2?
我知道,Python有NTLK,但我不允許使用字典存儲單詞的所有對作爲鍵和多少使用它爲項目
你可以自己做它們顯示爲價值的時間。您需要遍歷所有單詞並連接所有對,並查看它們是否已經存在於您的字典中。如果它們將該對增加1,並且它們不在字典中,則將其值設爲1。所以第一對你的字典裏是這樣的:
{"apple, red":1}
在通過循環下一次你會:
{"apple, red":1,
"red, potato":1}
和你的時間完成創建詞典中,你將有:
{"apple, red":2,
"red, potato":1,
"potato, apple":1}
然後你可以找到你想要的一對單詞。
你可以拉開這樣算了算:
list = ['apple', 'red', 'potato', 'apple', 'red']
', '.join(list).count(list[0] + ',' + ' ' + list[1])
它只是創建一個字符串「蘋果,紅,土豆,蘋果,紅」,上,它會指望有多少次「蘋果,紅色' 在裏面。
如果你在一個大的輸入數的東西,最好使用'collections.Counter':https://docs.python.org/3/library/collections.html#collections.Counter –
贏得'因爲'word'是一個字符串:''apple,red「':你會得到0.你的問題不清楚。 –