|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
wordstring = """it was the best of times it was the worst of times.
it was the age of wisdom it was the age of foolishness"""
wordstring = wordstring.replace(".","")
# print(wordstring)
wordlist = wordstring.split()
# print(wordlist)
wordfreq = []
for w in wordlist:
wordfreq.append(wordlist.count(w))
# print(wordfreq)
d = dict(wordlist,wordfreq)
print(d)
请教报错原因??
本帖最后由 isdkz 于 2023-1-30 17:55 编辑
dict的用法错了,dict 最多只能接收一个位置参数,而且这个位置参数需要能迭代出两个元素的序列
所以我用 zip 将你的两个列表打包成 zip 对象,这样应该能符合你的需求
- wordstring = """it was the best of times it was the worst of times.
- it was the age of wisdom it was the age of foolishness"""
- wordstring = wordstring.replace(".","")
- # print(wordstring)
- wordlist = wordstring.split()
- # print(wordlist)
- wordfreq = []
- for w in wordlist:
- wordfreq.append(wordlist.count(w))
- # print(wordfreq)
- d = dict(zip(wordlist,wordfreq)) # 改了这一行
- print(d)
复制代码
|
|