Python Forum

Full Version: Most used words in a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I'm trying to solve this exercise: https://open.kattis.com/problems/conversationlog .

I've managed to get a list with the words sorted by the number of occurrence but I'm still having troubles because I don't know what the exercise means with "most used words" and I don't understand it from the example.
This is what I've done so far:

#!/usr/bin/python3
import collections

m = int(input())
chat_list = [input().split() for _ in range(m)] 
[i.pop(0) for i in chat_list]   #remove the first word, which is the user name, from every list
words = [i for sublist in chat_list for i in sublist]   #create a list with every words
counter = collections.Counter(words)
a = counter.most_common()
print(a)
For future reference, some sample input, along with what your output is and the expected output, would help us know what we're looking at and how to help.

That said, the problem does mention that it's words used by *every* user, not JUST the most common words. So I think the first thing to do, would be to create a dict of sets of words, and then intersect the sets to get words used by every user. Something like:
>>> chat_list = '''Jepson no no no no nobody never
... Ashley why ever not
... Marcus no not never nobody
... Bazza no never know nobody
... Hatty why no nobody
... Hatty nobody never know why nobody
... Jepson never no nobody
... Ashley never never nobody no'''.split("\n")
>>> chat_list
['Jepson no no no no nobody never', 'Ashley why ever not', 'Marcus no not never nobody', 'Bazza no never know nobody', 'Hatty why no nobody', 'Hatty nobody never know why nobody', 'Jepson never no nobody', 'Ashley never never nobody no']
>>> user_chats = {}
>>> for message in chat_list:
...     words = message.split()
...     user = words[0]
...     if user not in user_chats:
...         user_chats[user] = set()
...     user_chats[user].update(set(words[1:]))
...
>>> user_chats
{'Jepson': {'never', 'no', 'nobody'}, 'Ashley': {'not', 'never', 'ever', 'no', 'nobody', 'why'}, 'Marcus': {'not', 'never', 'no', 'nobody'}, 'Bazza': {'know', 'never', 'no', 'nobody'}, 'Hatty': {'know', 'never', 'no', 'nobody', 'why'}}
>>> users = list(user_chats.values())
>>> all_chats = users[0]
>>> for user in users:
...     all_chats &= user
...
>>> all_chats
{'never', 'no', 'nobody'}
And from there, do a counter to sort those three words.
Whoops, this is why I was getting some false positive in the tests... Thank you, I'll change approach following your hint