1

I am trying to return the most reoccurring word from a list of strings in Python.

For example, here is a list that I'm working with:

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

desired_output = north

I am unsure as to how I can approach this.

For a more simpler list, I am able to use mode (such as below example):

simple_list = [north, north, south, east]

simple_output = mode(simple_list)

simple_output = north

Is it possible to apply this same logic for the desired list/output combo? I'm still a pretty novice Python user so I'm unsure how to proceed.

ek2014
  • 65
  • 1
  • 1
  • 6
  • BTW, [avoid using `list` as a variable name](https://stackoverflow.com/q/31087111/4518341). I'm guessing you meant that as pseudocode, but I mention it just in case. – wjandrea Nov 21 '20 at 04:52

3 Answers3

2

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
wjandrea
  • 28,235
  • 9
  • 60
  • 81
1
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))

But if you are allowed use statistics library, I like wjandrea's answer.

syohey
  • 156
  • 1
  • 1
  • 6
0

Code:

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']
Aaj Kaal
  • 1,205
  • 1
  • 9
  • 8