Python里,输入一个英文句子,统计并输出单词个数,怎么弄啊?

如题所述


你好,答案如下所示。mydict={}
for i in input("英文句子").split():
    if i in mydict:
        mydict[i]+=1
    else :
        mydict[i]=1
for  key,value in mydict.items():
    print(key,value)

缩进如图所示



希望你能够详细查看。

如果你有不会的,你可以提问

我有时间就会帮你解答。
希望你好好学习。
每一天都过得充实。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-03-11

#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author:Storm_duck
@file  :Read_Engish_Txt.py
@time  :2020/3/11 11:49
"""

"""
输入一个英文句子,统计并输出单词个数
"""


def getword(astr):
temp = astr.split(" ")
num = len(temp)
word = []
for i in range(num):
if temp[i] != ',' and temp[i] != '.' and temp[i] != '"':
word.append((temp[i].lower()))
else:
return word

if __name__ == "__main__":
dicts = {}
sentence = input("Please Enter a sentence:")
words = getword(sentence)

for code in words:
if code not in dicts.keys():
dicts[code] = 1
else:
dicts[code] += 1

for key in dicts.keys():
print("{}:{}".format(key, dicts[key]))


相似回答