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

我想根据堆栈溢出帖子中的第二个列表对列表进行排序。就我而言,我的代码试图按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 次

我该如何解决?


临摹微笑
浏览 88回答 1
1回答

慕运维8079593

当您有两个(weight, chromo)权重相等的对时会发生错误,此时 Python 会尝试比较这些chromo值:>>> class Chromo:...&nbsp; &nbsp; &nbsp;pass...>>> chromolist = [Chromo(), Chromo()]>>> fitness_weights = [42, 42]>>> sorted(zip(fitness_weights, chromolist))Traceback (most recent call last):&nbsp; File "<stdin>", line 1, in <module>TypeError: '<' not supported between instances of 'Chromo' and 'Chromo'您可以通过使用仅提取权重的自定义排序键,或者通过添加对排序序列中的每个值都是唯一的 tie 断路器来避免此问题,例如计数器:from itertools import countchromolist = [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 itemgetterchromolist = [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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python