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

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

我正在尝试 return 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

是否可以对所需的 list/output 组合应用相同的逻辑? 我还是一个新手 Python 用户,所以我不确定如何继续。

Split each string into words and flatten.

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

代码:

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']
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 的回答。