[英]Python - Sort 4 lists according to the sorting of the first list
我有 4 个列表,想要从最小到最大对第一个列表进行排序,然后对第二个、第三个和第四个列表应用相同的更改。
例如:
nodeA = ['1', '4', '3', '5', '2'] nodeB = ['0', '5', '0', '6', '3'] identity = ['R', 'G', 'C', 'L', 'L'] value = ['100', '125', '300', '400', '275']
go 是否会:
nodeA = ['1', '2', '3', '4', '5'] nodeB = ['0', '3', '0', '5', '6'] identity = ['R', 'L', 'C', 'G', 'L'] value = ['100', '275', '300', '125', '400']
(我认为我做对了)
我不知道该怎么做,非常感谢任何帮助!
您可以为 Python list
s 创建一个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']
我们可以使用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']
警告:如果所有列表的大小不同,这将无法按预期工作。
如果您必须继续使用列表,我建议您查看其他答案。
但是,如果所有列表中的每个索引都与某个 object 相关联(我正在想象一些具有两个节点、一个标识和一个测量值的测量设备),那么更复杂的数据结构可能是您更好的选择.
假设您像这样创建了 class Device
class Device:
def __init__(self, nodeA, nodeB, identity, value):
self.nodeA = nodeA
self.nodeB = nodeB
self.identity = identity
self.value = value
然后创建了该 class 的 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)]
然后,您可以使用内置的sorted
function(带有可选的key
参数)和内置operator
模块中的attrgetter
function 轻松排序此列表。 这将按照您作为键提供的 function 对对象列表进行排序,特别是一个 function ,它可以获得您想要排序的所需属性。
import operator
sorted_devices = sorted(devices, key=operator.attrgetter('nodeA'))
这里设备按nodeA
属性排序,但现在您也可以轻松地按任何其他属性对它们进行排序。
如果您没有严格的性能/内存要求,并且列表很少且数量固定,您可以尝试这个脚本,也许它是更容易实现的方法。
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)
您可以创建一个元组列表并对其进行排序,并使用其中的信息对其他列表进行排序......
>>> 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)]
现在我们有一个按第一项排序的元组列表,它们的原始 position 作为第二项。 我们可以使用 position 值作为其他列表的索引。
>>> 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']
>>>
使用字典映射
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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.