[英]Sort List with duplicates based on related list
Shortened the scenario for brevity.为简洁起见缩短了场景。
I have three arrays我有三个 arrays
A1 = [A,B,C,D,E,F]
A2 = [20,30,45,60,20,10]
A3 = [30,30,15,20,40,60]
where there is a relationship between the elements at i-th
position between the three arrays..其中三个 arrays 之间的i-th
position 的元素之间存在关系。
for eg A1[0]
is related to A2[0]
and also A3[0]
and so on...例如, A1[0]
与A2[0]
以及A3[0]
等相关......
I want to sort the three arrays BASED ON THE A2 (in ascending order).我想根据 A2(按升序)对三个 arrays 进行排序。
so after sorting, the arrays become所以排序后,arrays 变成
A1 = [F,A,E,B,C,D]
A2 = [10,20,20,30,45,60]
A3 = [60,30,40,30,15,20]
One thing I am not able to figure out is while sorting, if there is a duplicate record in A2 (20 in this case), then the sort should take the value in A2 which has a lesser corresponding value in A3.. Thats is why A should come before E in the final A1 list.我无法弄清楚的一件事是,在排序时,如果 A2 中有重复记录(在本例中为 20),那么排序应该采用 A2 中的值,而 A3 中的对应值较小。这就是为什么在最终的 A1 列表中,A 应该排在 E 之前。
Any help would be appreciated.任何帮助,将不胜感激。
As of now, I am trying to do this using quick sort, Please find my code below截至目前,我正在尝试使用快速排序来做到这一点,请在下面找到我的代码
def partition(self, A1, A2, A3, low, high):
pivot = A2[high]
i = low - 1
for j in range(low, high):
if A2[j] <= pivot:
i = i + 1
(A2[i], A2[j]) = (A2[j], A2[i])
(A3[i], A3[j]) = (A3[j], A3[i])
(A1[i], A1[j]) = (A1[j], A1[i])
(A2[i + 1], A2[high]) = (A2[high], A2[i + 1])
(A3[i + 1], A3[high]) = (A3[high], A3[i + 1])
(A1[i + 1], A1[high]) = (A1[high], A1[i + 1])
return i + 1
def quickSort(self, A1, A2, A3, low, high):
if low < high:
pi = self.partition(A1, A2, A3, low, high)
self.quickSort(A1, A2, A3, low, pi - 1)
self.quickSort(A1, A2, A3, pi + 1, high)
Please note: I have to do this without using inbuilt functions请注意:我必须在不使用内置函数的情况下执行此操作
A2, A3, A1 = map(list, zip(*sorted(zip(A2, A3, A1))))
And since you need it without built-in functions:而且由于您需要它而没有内置功能:
A1 = ['A', 'B', 'C', 'D', 'E', 'F']
A2 = [20, 30, 45, 60, 20, 10]
A3 = [30, 30, 15, 20, 40, 60]
def myzip(*iterables):
myiters = [myiter(it) for it in iterables]
try:
while True:
yield tuple(mynext(it) for it in myiters)
except RuntimeError:
pass
def mymap(func, *iterables):
myiters = [myiter(it) for it in iterables]
try:
while True:
yield func(*(mynext(it) for it in myiters))
except RuntimeError:
pass
def mylist(*args):
return [*args]
def myiter(iterable):
for item in iterable:
yield item
raise StopIteration()
def mynext(it):
return it.__next__()
def _partition(l, r, arr):
pivot, ptr = arr[r], l
for i in range(l, r):
if arr[i] <= pivot:
arr[i], arr[ptr] = arr[ptr], arr[i]
ptr += 1
arr[ptr], arr[r] = arr[r], arr[ptr]
return ptr
def _quicksort(l, r, arr):
if len(arr) == 1:
return arr
if l < r:
pi = _partition(l, r, arr)
_quicksort(l, pi-1, arr)
_quicksort(pi+1, r, arr)
return arr
def quicksort(arr):
arr = list(arr)
return _quicksort(0, len(arr)-1, arr)
A2, A3, A1 = mymap(mylist, myzip(*quicksort(myzip(A2, A3, A1))))
print(A1, A2, A3, sep='\n')
Resulting in导致
['F', 'A', 'E', 'B', 'C', 'D']
[10, 20, 20, 30, 45, 60]
[60, 30, 40, 30, 15, 20]
Note : I took some shortcuts in my reimplementation of the built-ins.注意:我在重新实现内置插件时采取了一些捷径。 Their exception handling is less than ideal, some features are missing, and the behavior isn't exactly the same, but they work for their intended purposes.它们的异常处理不太理想,缺少一些功能,并且行为不完全相同,但它们可以达到预期目的。
Note 2 : range()
and print()
were not reimplemented.注意 2 : range()
和print()
没有重新实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.