python - TypeError: sort() 没有位置参数
问题描述
我正在尝试将 Python2 代码更改为 Python3 代码。有类似的代码吗?
indice.sort(lambda x,y: -cmp(topic[x], topic[y]))
topic
是一个浮动列表。
indice
是topic
的索引列表。喜欢:indice = list(range(len(topic))).
解决方案
按照 λuser 在他们的评论中给出的链接,Python 3 (>= 3.2) 中的标准方法是使用functools.cmp_to_key
:
import functools
...
keyfunc = functools.cmp_to_key(lambda x,y: -cmp(topic[x], topic[y]))
indice.sort(key=keyfunc)
推荐阅读
- r - 使用分组连接列中的字符串
- three.js - THREEJS:如何计算 THREE.ray 上到另一个 THREE.ray 的最近点
- reactjs - React - 顶级组件变得臃肿?
- php - 如何在 Popover 中获取两个函数
- python - python -spyder中的D素数方程
- javascript - 如何创建顺序连续变量以使用循环推入数组
- php - Laravel 路由政策:函数 1 的参数太少,预期通过 2
- android - NotifyItemChanged 在 OnResume() 中被调用但不起作用
- c - 如何就地修改项目(不复制/传递缓冲区)
- python - 在水平条形图上显示数字