python - 在 Jupyter 笔记本上的 Python 中使用 sorted() 时出现“列表”对象不可调用错误
问题描述
我正在尝试下面的代码,我正在尝试读取文件的内容并识别 3 个最常用的单词。我将单个单词及其频率作为键:python dict中的值。当我尝试在其上使用 sorted() 时,出现“列表”对象不可调用错误。
content=open("Assignment1_Q8.txt").read().split()
freq={}
for item in content:
if item in freq:
freq[item]+=1
else:
freq[item]=1
print(freq)
freq_sort=sorted(freq,key=freq.values())
Output:
{'Hello': 5, 'World': 3, 'Hi': 3, 'Bye': 5}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-220-40658aa1ff31> in <module>
8 print(freq)
9
---> 10 freq_sort=sorted(freq,key=freq.values())
TypeError: 'list' object is not callable
解决方案
问题是我之前使用了“排序”作为变量,后来我试图将它用作函数。我删除了“排序”变量并执行了代码,它可以工作。谢谢。
推荐阅读
- node.js - 使用 mongoose 在 mongoDB 中搜索 url 类型的部分文本
- ruby - FieldPath 字段名称可能不包含“。” 尝试使用 AGGREGATE 时
- python - 用降序值替换重复值
- java - 如何为 N/3 重复编号修复此代码
- c# - 在一个表中映射泛型类型的所有派生类型
- git - 只需一次提交即可合并请求
- java - 从数据库中删除项目
- react-native-android - 当我使用 backHandle(hardwareBackPress)返回此屏幕时,为什么屏幕的 componentwillMount 不工作?
- java - 如何为应用程序在后台显示的通知设置自定义布局?
- mysql - 为 mysql db 中存在的特定列生成 n-gram