Python Sort Collections.DefaultDict 按降序排列

标签 python

我有这段代码:

    visits = defaultdict(int)
    for t in tweetsSQL:
         visits[t.user.from_user] += 1

我在网上看了一些使用 sorted 方法的例子:

已排序(visits.iteritems,key=operator.itemgetter(1),reverse=True)

但它给了我:

“TypeError:‘builtin_function_or_method’对象不可迭代”

我不知道为什么。

最佳答案

iteritems 是一种方法。您需要括号来调用它:visits.iteritems()

就目前而言,您正在将 iteritems 方法本身传递给 sorted,这就是它提示无法迭代函数或方法的原因。

关于Python Sort Collections.DefaultDict 按降序排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/758792/

相关文章:

python 信息函数 : where is it?

python - 我从哪里获得 Flask 的 SECRET_KEY?

python - 如何在 Python 中反转字典(其值是列表)?

python - 如何将cpp结构中的字符串转换为python结构

python - 如何使用轴位置作为引用在 matplotlib 上跨多个轴绘制框

python - 从 .xlsx 文件打印唯一值

c# - 连接到 Com5 : System. NullReferenceException 未处理

python - 嵌套列表中的 curs.fetchall() python

python - 变量在函数调用之间不清除

python - 在 python 中从 BackgroundSubtractorMOG2 获取背景模型