保存和打印重复的单词

时间:2015-08-19 02:11:36

标签: python words

所以我试图创建一个程序,从用户那里获取输入,然后请求辅助输入。如果第二句中有重复的单词,则保存单词并打印出来。然后它再次询问,如果有与第一句话相同的单词,它也会保存并打印它们。如果用户输入任何内容,则打印重复单词的数量。到目前为止,我的代码是

word_lst = []
rptd_wds = []
words = input("Words: ")
while words != '':
  word_lst.append(words)
  words = input("Words: ")
  for i in word_lst.split():
    if i == i:
      rptd_wds.append(i)
      print(rptd_wds)

这段代码很粗糙。所以过分的问题是如何保存重复的单词,以便以后可以计算和打印。

当我运行它时,这是输出

Words: he me there here
Words: me here
['he me there here']
he me there here
Words: 
['he me there here', 'me here']
he me there here
me here

0 个答案:

没有答案