我有一个列表“distancechunks”,里面有 90 个列表。这 90 个列表中的每一个都由其中的 2 个元组组成(见图)。我想按第一个元组(图像中突出显示的黄色)对每个列表(总共 90 个)进行排序。因此,每个列表都是从低到高排序的。数组值(第 2 个元组)不应排序,只需在左侧保持相同的值。我尝试了经典的东西,比如 .sort() 和用 lambda 等排序:for x in distancechunks: for i in x: lst2 = sorted(i) print(lst2)但是,我总是收到不同的错误消息,例如:ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()或者我试过:vallah = distancechunksvallah.sort(key=lambda x: int(x[0]))vallah并得到错误信息:int() argument must be a string, a bytes-like object or a number, not 'tuple'不幸的是,我不确定如何实现这个错误......这是“distancechunks”列表的摘录:
1 回答

拉莫斯之舞
TA贡献1820条经验 获得超10个赞
您的第二次尝试已结束,您只需要对每个子列表进行排序。
for chunk in distancechunks: chunk.sort(key=lambda x: x[0])
我不确定你为什么要在已经有浮点数的情况下尝试转换为 int。
添加回答
举报
0/150
提交
取消