小编Sag*_*wda的帖子

Python 3.6.2,使用操作符中的 itemgetter -> TypeError: 'int' object is not subscriptable

我有一个示例字典:

sample = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
Run Code Online (Sandbox Code Playgroud)

客观的:

获取键上的(键,值)对的排序列表。
它应该看起来像这样:

[(0, 0), (1, 2), (2, 1), (3, 4), (4, 3)]
Run Code Online (Sandbox Code Playgroud)

我试过这个:

import operator as o   
lst1 = sorted(sample, key = o.itemgetter(0))
Run Code Online (Sandbox Code Playgroud)

以下是错误

回溯(最近一次调用最后一次):
  文件“”,第 1 行,位于
    lst1 = 排序(样本, key = o.itemgetter(0))
类型错误:“int”对象不可下标

不知道我错过了什么。

python dictionary python-3.x

-1
推荐指数
1
解决办法
3229
查看次数

标签 统计

dictionary ×1

python ×1

python-3.x ×1