简体   繁体   English

如何在python中计算两个单词序列

[英]How to count two word sequences in python

I am trying to find a way to count two word sequence in a list of words using python. 我试图找到一种方法来使用python计算单词列表中的两个单词序列。 I converted the one word list into a list of two words.I then want to try to count the frequency of all the similar two word lists. 我将一个单词列表转换为两个单词的列表,然后我想尝试计算所有相似的两个单词列表的频率。 I tried the counter function but it gives me an unhashable type: 'list'. 我尝试了计数器功能,但它给了我一个无法散列的类型:“列表”。 An example of my code can be seen below: 我的代码示例如下所示:

s = ['one', 'two','three','four','five','six','one','two']

print s
print
p=[]
i=0
for i in range(0,len(s)/2):
    p.append(s[i*2:i*2+2])

print p
wordcounter = Counter(p)

I am trying to have the output so that the most occuring two words are displayed at the top with the number of occurances next to them. 我试图提供输出,以便将最常出现的两个单词显示在顶部,并在其旁边显示出现次数。 Thank you in advance. 先感谢您。

Requirement - "I am trying to have the output so that the most occuring two words are displayed at the top with the number of occurances next to them." 要求-“我正在尝试获得输出,以便将最常出现的两个单词显示在顶部,并在其旁边显示出现的次数。”

You should try Counter.most_common method and tell us if it works for you. 您应该尝试使用Counter.most_common方法,并告诉我们它是否适合您。

Counter works only with hashable items. 计数器仅适用于可哈希项。 Items of p are lists, so they are not hashable. p项目是列表,因此它们不可哈希。 One way to go around it, is to joint the inner list of strings, eg 解决它的一种方法是联合内部字符串列表,例如

wordcounter = collections.Counter(" ".join(v) for v in p)
print(wordcounter)
# gives
# Counter({'one two': 2, 'three four': 1, 'five six': 1})

I thought I could do this is a nice one-liner list comp. 我以为我可以做到这一点是一个不错的单行列表。 Turns out its not so nice: 事实证明它不是很好:

newlist=[ii for n,ii in enumerate([[x,[lst[i:i+2] for i in range(0,len(lst),2)].count(x)] for x in [lst[i:i+2] for i in range(0,len(lst),2)]]) if ii not in [[x,[lst[i:i+2] for i in range(0,len(lst),2)].count(x)] for x in [lst[i:i+2] for i in range(0,len(lst),2)]][:n]]

please have mercy on my soul 请怜悯我的灵魂

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM