鱼C论坛

 找回密码
 立即注册
查看: 4053|回复: 4

[已解决]报错原因

[复制链接]
发表于 2023-1-30 17:45:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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)
请教报错原因??
最佳答案
2023-1-30 17:53:31
本帖最后由 isdkz 于 2023-1-30 17:55 编辑

dict的用法错了,dict 最多只能接收一个位置参数,而且这个位置参数需要能迭代出两个元素的序列

所以我用 zip 将你的两个列表打包成 zip 对象,这样应该能符合你的需求

  1. wordstring = """it was the best of times it was the worst of times.
  2. it was the age of wisdom it was the age of foolishness"""
  3. wordstring = wordstring.replace(".","")
  4. # print(wordstring)
  5. wordlist = wordstring.split()
  6. # print(wordlist)
  7. wordfreq = []
  8. for w in wordlist:
  9.     wordfreq.append(wordlist.count(w))
  10. # print(wordfreq)
  11. d = dict(zip(wordlist,wordfreq))                         # 改了这一行
  12. print(d)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-4 02:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表