Write a program that reads a list of words. Then, the program outputs those words and their frequencies (case insensitive)

Question:

if the code input is:

hey Hi Mark hi mark

the program wants the lower case number count AND for the list of words to remain upper case if they were upper case. So the correct output would be:

hey 1
Hi 2
Mark 2
hi 2
mark 2

I’ve created the correct number count and list using this code:

line = input()
norm = line.split()
low = line.lower().split()
for chr in low:
    freq = low.count(chr)
    print(freq)
for x in norm:
    print(x)

the output of this is:

1
2
2
2
2
hey
Hi
Mark
hi
mark

I thought I could use print(x,freq) in my last for loop to give the correct out put but it creates an additional hey for some ungodly reason and this becomes the output:

hey 2
Hi 2
Mark 2
hi 2
mark 2

I have no idea where it is getting the extra hey.
is there anyway to combine the print results from 2 for loops to create a correct output? Or do you know where the extra ‘hey’ is coming from?

Asked By: P0nder

||

Answers:

You can iterate on both original and lower versions at once using zip

for word, word_lower in zip(norm, low):
    print(word, low.count(word_lower))
Answered By: azro
wordInput = input()

myList = wordInput.split(" ")

for i in myList:

   print(i,myList.count(i))
Answered By: Jaeson Keller

Here is a very simple approach using what you have learned so far:

words = input().split()

for word in words:
    print(word, words.count(word))

In a nutshell, words is split for the for loop so you can iterate through each element from input(). From there, you use an output statement that makes use of the count() feature in Python to earn full points.

Answered By: John B.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.