如何打印两次相同的迭代?

How to print the same iteration twice?

提示: 编写一个读取单词列表的程序。然后,程序输出这些词和它们的频率。

例如:如果输入是:

hey hi Mark hi mark

输出是:

hey 1



hi 2

Mark 1

hi 2

mark 1

我的代码:

from collections import Counter
user.input = input().split()
count = counter(user_input)

for key, value in count.items():
    print('{} {}'.format(key, value))

我几乎已经解决了这个问题,但出于某种原因,我的输出不会打印出上面问题提示中所示的第二个“hi 2”。

您可以使用给定的句子进行迭代:

from collections import Counter
user_input = input().split()
count = Counter(user_input)

for word in user_input:
    print(f'{word} {count[word]}')

输出:

hey 1
hi 2
Mark 1
hi 2
mark 1