[英]Reordering the list based on values from another list?
如何根据 Python 中的另一个列表重新排序第一个列表?
我无法重新排序 2 列表,所以这是我的代码:
import random
numElements1 = int(input("Enter the number of elements for first list: "))
numElements2 = int(input("Enter the number of elements for second list: "))
randomlist = random.sample(range(10, 99), numElements1)
print(randomlist)
randomlist2 = random.sample(range(10, 99), numElements2)
print(randomlist2)
您可以将sorted()
与zip()
arr, index = map(list, zip(*sorted(zip(arr, index), key=lambda x: x[1])))
print(arr) #[40, 60, 90, 50, 70]
print(index) #[0, 1, 2, 3, 4]
a={}
for i in index:
a[i]=randomlist[i]
for i,j in sorted(a.items()):
out_index.append(i)
out_arr.append(j)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.