Python的作业,希望用简单的语言编写,tiaoؠl菜鸟难了看不懂,谢谢

2025-06-22 04:27:56
推荐回答(2个)
回答1:

from collections import Counter

data = None
#Get data from datafile
with open("datafile", "r") as f:
    data = f.read().strip().split()

#delete special characters
data = [_.strip("?,\"") for _ in data]

#Count words number
result = Counter(data)
print result

#Get user choice:
input_msg = raw_input("Enter your choice: ")
try:
    input_msg = int(input_msg)
except Exception,e:
    print "Should input number and less than %d"%len(result)
    print str(e)

#Print result
print "You inputed: %d"%input_msg
r = {}
r = {k:result.get(k) for k in result if result.get(k) == input_msg}
print "Result: ", r.keys()


自己新建一个叫datafile 的文件,写进字符串就好了

回答2:

先mark一下