Reloader
Reloader

Reputation: 720

Parsing multiple lines in Python

I'm currently learning Python and I need to write a program which determines the word which appears the most times in a poem. Problem which is troubling me is about parsing a lines of a poem into a SINGLE list containing words of the poem. When I solve it i will have no trouble determining the word which appears the most times.

I can access the lines of the poem by calling input() repeatedly, and the last line contains the three characters ###.

So, i wrote:

while True:
   y = input()
   if y == "###":
     break
   y = y.lower()
   y = y.split()

and for input:

Here is a line like sparkling wine
Line up now behind the cow
###

got result:

['here', 'is', 'a', 'line', 'like', 'sparkling', 'wine']
['line', 'up', 'now', 'behind', 'the', 'cow']

If i try to call y[0], i get:

here
line

How can I concatenate two lists within same variable, or how can I assign every line to different variable?

Any hint is appreciated. Thanks.

Upvotes: 1

Views: 2029

Answers (2)

jamylak
jamylak

Reputation: 133504

words = []

while True:
   y = input()
   if y == "###":
     break
   words.extend(y.lower().split())

from collections import Counter
Counter(words).most_common(1)

This whole code can be compressed into the following one liner:

Counter(y.lower() for x in iter(input, '###') for y in x.split()).most_common(1)

eg.

>>> sys.stdin = StringIO.StringIO("""Here is a line like sparkling wine
Line up now behind the cow
###""")
>>> Counter(y.lower() for x in iter(input, '###') for y in x.split()).most_common(1)
[('line', 2)]

As per request, without any imports

c = {} # stores counts
for line in iter(input, '###'):
    for word in line.lower().split():
        c[word] = c.get(word, 0) + 1 # gets count of word or 0 if doesn't exist

print(max(c, key=c.get)) # gets max key of c, based on val (count)

To not have to make a second pass over the dictionary to find the max word, keep track of it as you go along:

c = {} # stores counts
max_word = None
for line in iter(input, '###'):
    for word in line.lower().split():
        c[word] = c.get(word, 0) + 1 # gets count of word or 0 if doesn't exist
        if max_word is None:
            max_word = word
        else:
            max_word = max(max_word, word, key=c.get) # max based on count       

print(max_word)

Upvotes: 3

Martijn Pieters
Martijn Pieters

Reputation: 1121366

You need to add y to an existing list, using list.extend():

words = []
while True:
   y = input()
   if y == "###":
     break
   y = y.lower()
   y = y.split()
   words.extend(y)

Now words is a list containing all the words of the lines your user entered.

Demo:

>>> words = []
>>> while True:
...    y = input()
...    if y == "###":
...      break
...    y = y.lower()
...    y = y.split()
...    words.extend(y)
... 
Here is a line like sparkling wine
Line up now behind the cow
###
>>> print(words)
['here', 'is', 'a', 'line', 'like', 'sparkling', 'wine', 'line', 'up', 'now', 'behind', 'the', 'cow']

Upvotes: 5

Related Questions