为了账号安全,请及时绑定邮箱和手机立即绑定

使用 zip() 对元组列表进行排序时,有时不支持“<”

使用 zip() 对元组列表进行排序时,有时不支持“<”

临摹微笑 2022-11-09 17:15:16
我想根据堆栈溢出帖子中的第二个列表对列表进行排序。就我而言,我的代码试图按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 fineworks fineworks fineworks fineworks fineworks fineworks fineworks fineworks fineworks fineworks fineworks fineworks fineworks fineworks fineworks fineworks fineworks fineworks fineworks fineworks fineworks 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 次我该如何解决?
查看完整描述

1 回答

?
慕运维8079593

TA贡献1876条经验 获得超5个赞

当您有两个(weight, chromo)权重相等的对时会发生错误,此时 Python 会尝试比较这些chromo值:


>>> class Chromo:

...     pass

...

>>> chromolist = [Chromo(), Chromo()]

>>> fitness_weights = [42, 42]

>>> sorted(zip(fitness_weights, chromolist))

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: '<' not supported between instances of 'Chromo' and 'Chromo'

您可以通过使用仅提取权重的自定义排序键,或者通过添加对排序序列中的每个值都是唯一的 tie 断路器来避免此问题,例如计数器:


from itertools import count


chromolist = [x for *_, x in sorted(zip(fitness_weights, count(), chromolist))]

计数器只是为了确保 Python 从不查看Chromo实例,因为现在每个元素都是一个(weight, unique_integer, Chromo)元组:


>>> from itertools import count

>>> sorted(zip(fitness_weights, count(), chromolist))

[(42, 0, <__main__.Chromo object at 0x1038cfa00>), (42, 1, <__main__.Chromo object at 0x103a396d0>)]

排序键只是一个生成要比较的值的函数,您可以使用 lambda ( lambda t: t[0]) 或operator.itemgetter()object:


from operator import itemgetter


chromolist = [x for _, x in sorted(zip(fitness_weights, chromolist), key=itemgetter(0))]

key 函数需要单独遍历输入列表,因此速度会稍微慢一些,如这个包含 200 个输入的简单计时赛所示:


>>> from timeit import timeit

>>> fw = fitness_weights * 100

>>> cl = chromolist * 100

>>> timeit('sorted(zip(fw, count(), cl))', globals=globals(), number=100000)

1.4618491119981627

>>> timeit('sorted(zip(fw, cl), key=itemgetter(0))', globals=globals(), number=100000)

1.6409574589997646


查看完整回答
反对 回复 2022-11-09
  • 1 回答
  • 0 关注
  • 87 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信