Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Most used words in a list
#1
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)
Reply
#2
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.
Reply
#3
Whoops, this is why I was getting some false positive in the tests... Thank you, I'll change approach following your hint
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Function to count words in a list up to and including Sam Oldman45 15 6,553 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,792 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Trying to find first 2 letter word in a list of words Oldman45 7 3,725 Aug-11-2020, 08:59 AM
Last Post: Oldman45
  how to check if string contains ALL words from the list? zarize 6 7,209 Jul-22-2020, 07:04 PM
Last Post: zarize
  item from a line to list however when i print the line instead of words i get letters Sutsro 5 2,954 Apr-22-2020, 02:39 PM
Last Post: deanhystad
  How do you replace a word after a match from a list of words in each line of a file? vijju56 1 3,459 Oct-17-2019, 03:04 PM
Last Post: baquerik
  Compare all words in input() to all words in file Trianne 1 2,765 Oct-05-2018, 06:27 PM
Last Post: ichabod801
  Exclude words with specific endings from list Epileptiker 5 4,211 Apr-06-2018, 02:40 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020