Python-根据第一个列表的排序对4个列表进行排序

Python-根据第一个列表的排序对4个列表进行排序,python,python-3.x,list,sorting,Python,Python 3.x,List,Sorting,我有4个列表,希望将第一个列表从最小到最大排序,然后对第二个、第三个和第四个列表应用相同的更改 例如: nodeA=['1','4','3','5','2'] nodeB=['0','5','0','6','3'] 标识=['R','G','C','L','L'] 值=['100','125','300','400','275'] 将转到: nodeA=['1','2','3','4','5'] nodeB=['0','3','0','5','6'] 标识=['R','L','C','G','L

我有4个列表,希望将第一个列表从最小到最大排序,然后对第二个、第三个和第四个列表应用相同的更改

例如:

nodeA=['1','4','3','5','2']
nodeB=['0','5','0','6','3']
标识=['R','G','C','L','L']
值=['100','125','300','400','275']

将转到:

nodeA=['1','2','3','4','5']
nodeB=['0','3','0','5','6']
标识=['R','L','C','G','L']
值=['100','275','300','125','400']

(我想我做得对)


我不知道如何做到这一点,任何帮助是非常感谢

如果您没有严格的性能/内存要求,并且列表很少且数量固定,您可以尝试使用此脚本,也许这是更容易实现的方法

nodeA = ['1', '4', '3', '5' , '2']
nodeB = ['0', '5', '0', '6', '3']
identity = ['R', 'G', 'C', 'L', 'L']
value = ['100', '125', '300', '400', '275']

s_A = sorted(nodeA)
s_B = []
s_i = []
s_v = []

for i in range(len(nodeA)):
    index = nodeA.index(s_A[i])
    s_B.append(nodeB[index])
    s_i.append(identity[index])
    s_v.append(value[index])

print(s_A)
print(s_B)
print(s_i)
print(s_v)

我们可以结合使用
zip
numpy
来快速实现这一点

import numpy as np

a, b, c, d = np.transpose(sorted(zip(nodeA, nodeB, identity, value)))

for arr in (a, b, c, d):
    print(arr, '\n')

['1' '2' '3' '4' '5'] 

['0' '3' '0' '5' '6'] 

['R' 'L' 'C' 'G' 'L'] 

['100' '275' '300' '125' '400'] 
我们也可以不使用
numpy
而使用
zip
两次

a, b, c, d = zip(*sorted(zip(nodeA, nodeB, identity, value)))

for arr in (a, b, c, d):
    print(list(arr), '\n')

['1', '2', '3', '4', '5'] 

['0', '3', '0', '5', '6'] 

['R', 'L', 'C', 'G', 'L'] 

['100', '275', '300', '125', '400']

警告:如果所有列表的大小都不相同,这将无法正常工作。

您可以创建元组列表并对其进行排序,然后使用其中的信息对其他列表进行排序

>>> nodeA = ['1', '4', '3', '5' , '2']
>>> tracker = [(item, i) for i, item in enumerate(nodeA)]
>>> tracker.sort()
>>> tracker
[('1', 0), ('2', 4), ('3', 2), ('4', 1), ('5', 3)]
现在我们有了一个元组列表,这些元组在第一项上排序,它们的原始位置作为第二项。我们可以使用该位置值作为其他列表的索引

>>> nodeB = ['0', '5', '0', '6', '3']
>>> identity = ['R', 'G', 'C', 'L', 'L']
>>> value = ['100', '125', '300', '400', '275']
>>> 
>>> for li in (nodeA, nodeB, identity, value):
...     li = [li[i] for item, i in tracker]
...     print(li)
...     
['1', '2', '3', '4', '5']
['0', '3', '0', '5', '6']
['R', 'L', 'C', 'G', 'L']
['100', '275', '300', '125', '400']
>>> 
使用dict映射

d = {}
for a_value, b_value, i_value, value in zip(nodeA, nodeB, identity, value):
    d[a_value]=[b_value,i_value,value]
nodeA = sorted(nodeA)
并使用刚才创建的dict再次查找值

nodeB=[d.get(i)[0] for i in nodeA] #['0', '3', '0', '5', '6']
identity=[d.get(i)[1] for i in nodeA] #['R', 'L', 'C', 'G', 'L']
value=[d.get(i)[2] for i in nodeA] #['100', '275', '300', '125', '400']

如果您必须继续使用列表,我建议您查看其他答案

但是,如果所有列表中的每个索引都绑定到某个对象(我想象的是一个具有两个节点、一个标识和一个测量值的测量设备),那么对您来说,更复杂的数据结构可能是更好的选择

假设您创建了一个类
设备
,如下所示

class Device:

    def __init__(self, nodeA, nodeB, identity, value):
        self.nodeA = nodeA
        self.nodeB = nodeB
        self.identity = identity
        self.value = value
然后创建了该类的5个实例

devices = [Device(1, 0, 'R', 100),
           Device(4, 5, 'G', 125),
           Device(3, 0, 'C', 300),
           Device(5, 6, 'L', 400),
           Device(2, 3, 'L', 275)]
然后,您可以使用内置函数(带有可选的
参数)和内置
操作符
模块中的函数轻松对该列表进行排序。这将按照作为键的函数对对象列表进行排序,特别是一个获取所需属性的函数

import operator
sorted_devices = sorted(devices, key=operator.attrgetter('nodeA'))

在这里,设备是按
nodeA
属性排序的,但现在您也可以轻松地按任何其他属性对它们进行排序。

您可以为Python
列表创建一个版本的
argsort()

def argsort(seq):
    ix = list(range(len(seq)))
    ix.sort(key=seq.__getitem__)
    return ix
您可以使用它对所有列表重新排序:

nodeA = ['1', '4', '3', '5' , '2']
nodeB = ['0', '5', '0', '6', '3']
identity = ['R', 'G', 'C', 'L', 'L']
value = ['100', '125', '300', '400', '275']

ix = argsort(nodeA)
nodeA = [nodeA[i] for i in ix]
nodeB = [nodeB[i] for i in ix]
identity = [identity[i] for i in ix]
value = [value[i] for i in ix]

print(nodeA)
# ['1', '2', '3', '4', '5']
print(nodeB)
# ['0', '3', '0', '5', '6']
print(identity)
# ['R', 'L', 'C', 'G', 'L']
print(value)
# ['100', '275', '300', '125', '400']

你能先把数据收集到一个对象中吗?或者你必须使用原始列表(独家)?这太棒了。谢谢分享