TypeError: ‘int‘ object is not subscriptable

When using python to sort the dictionary, an error is reported in this statement:

# 按照键排序
ll1=sorted(zi,key=lambda kv:(kv[0]))
print(ll1)

TypeError: ‘int’ object is not subscriptable

The reason is that the first parameter of the sorted() function cannot be a dictionary, but the zi.items():items() function returns a list of traversable (key, value) tuple array
subscript variables out of bounds or does not support subscript access This error will be reported under the circumstances. That is, the object that cannot be operated on is operated on or the way of accessing the object is wrong.
Modify it to be sorted correctly:

ll1=sorted(zi.items(),key=lambda kv:(kv[0]))
print(ll1)

Guess you like

Origin blog.csdn.net/liulanba/article/details/114387611