繁体   English   中英

如何从 Python 中的字符串列表中获取最多重复出现的单词?

[英]How to get most reoccurring word from a list of strings in Python?

我正在尝试从 Python 中的字符串列表中返回最常出现的单词。

例如,这是我正在使用的列表:

list = [north north, north south, north west, north east]

desired_output = north

我不确定如何处理这个问题。

对于更简单的列表,我可以使用模式(如下例所示):

simple_list = [north, north, south, east]

simple_output = mode(simple_list)

simple_output = north

是否可以对所需的列表/输出组合应用相同的逻辑? 我仍然是一个相当新手的 Python 用户,所以我不确定如何继续。

将每个字符串拆分为单词展平

from statistics import mode

strings = ['north north', 'north south', 'north west', 'north east']
words = [word for s in strings for word in s.split()]

print(mode(words))  # -> north
directions = ['north north', 'north south', 'north west', 'north east']
word_count = {}

for d in directions:
    for word in d.split():
        if word in word_count:
            word_count[word] += 1
        else:
            word_count[word] = 1

print(max(word_count, key=word_count.get))

但是如果你被允许使用statistics库,我喜欢 wjandrea 的回答。

代码:

list1 = ['north north', 'north south', 'north west', 'north east']
dict_cnt = dict()
for sentence in list1:
    for word in sentence.split():
        if dict_cnt.get(word):
            dict_cnt[word] += 1
        else:
            dict_cnt[word] = 1
max_val = max(dict_cnt.values())
reoccurring = [k for k,v in dict_cnt.items() if v == max_val ]
print(reoccurring) # ['north']

暂无
暂无

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

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