Dictionary Word counter

丶灬走出姿态 提交于 2020-01-29 06:26:10

Q:
Here’s something to stop you from getting repetitive when writing essays. Write a program that reads multiple lines of plain text from the user, then prints out each different word in the input with a count of how many times that word occurs. Don’t worry about punctuation or case – the input will just be words, all in lower case. Your output should list the words in alphabetical order.

For example:

Enter line: which witch
Enter line: is which
Enter line: 
is 1
which 2
witch 1

A??:

dic_line1 = {}
dic_line2 = {}
list = []
enter_line = input("Enter line: ").lower()

while enter_line:
  list_line = enter_line.split()
  for n in list_line:
    list.append(n)
    num_line = list.count(n)
    dic_line1[n] = num_line
    dic_line2.update(dic_line1)
  enter_line = input("Enter line: ").lower()

for word in sorted(dic_line2):
  print(word, dic_line2[word]) 
  • lower()
  • list.split()
  • list.append()
  • list.count()
  • 按顺序打印字典for n in sorted(dictionary)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!