我想根据堆栈溢出帖子中的第二个列表对列表进行排序。就我而言,我的代码试图按Chromo对象对它们进行排序fitness_weights,因此我尝试了链接帖子上的解决方案:
def foobar():
...
chromolist = [x for _, x in sorted(zip(fitness_weights, chromolist))]
...
给出错误:
TypeError: '<' not supported between instances of 'Chromo' and 'Chromo'
调试我试过:
def foobar():
...
try:
chromolist = [x for _, x in sorted(zip(fitness_weights, chromolist))]
except Exception as e:
print(fitness_weights)
print(chromolist)
print([i for i in zip(fitness_weights, chromolist)])
raise e
print('works fine')
...
输出:
works fine
works fine
works fine
works fine
works fine
works fine
works fine
works fine
works fine
works fine
works fine
works fine
works fine
works fine
works fine
works fine
works fine
works fine
works fine
works fine
works fine
works fine
[2630793242, 2662598634, 1204127226, 1218205610, 1224753838, 1212750850,
1212293610, 1221507266, 1269226518, 1363578674, 1209661338, 2674408754,
1179213986, 1209887778, 2281636710, 1906925334, 1156258126, 1287144442,
1218205610, 1256241498, 2926198286, 1533442630, 1587421406, 2685579290,
1203563674, 1205066274, 1181576990, 1188462746, 1127834446, 2295554650,
1216261042, 1193222146, 1191591394, 1206052810, 1206800842, 1213410890,
1202786310, 1230097202, 1277296358, 1218982810]
[Chromo Object, Chromo Object, Chromo Object, Chromo Object, Chromo Object,
Chromo Object, Chromo Object, Chromo Object, Chromo Object, Chromo Object,
Chromo Object, Chromo Object, Chromo Object, Chromo Object, Chromo Object,
Chromo Object, Chromo Object, Chromo Object, Chromo Object, Chromo Object,
Chromo Object, Chromo Object, Chromo Object, Chromo Object, Chromo Object,
Chromo Object, Chromo Object, Chromo Object, Chromo Object, Chromo Object,
Chromo Object, Chromo Object, Chromo Object, Chromo Object, Chromo Object,
Chromo Object, Chromo Object, Chromo Object, Chromo Object, Chromo Object]
这令人困惑,因为:
所有数据类型均正确
该功能正常工作 22 次
我该如何解决?
慕运维8079593
相关分类