简体   繁体   English

根据其他列表中的值对列表中的列表进行排序

[英]Order lists in list based on values from other list

I want to sort lists in a list based on values from another list.我想根据另一个列表中的值对列表中的列表进行排序。 In the example the 5th order should be placed before 0 and 1 because the due date is earlier.在这个例子中,第 5 个订单应该放在 0 和 1 之前,因为截止日期更早。

I already tried for loops and other sorting methods but the list in the list is not an integer, which makes ik too hard for me.我已经尝试过 for 循环和其他排序方法,但列表中的列表不是整数,这让 ik 对我来说太难了。 Therefore I have no idea to solve the following:因此,我不知道解决以下问题:

orders = [[0, 1, 5], [2, 3 ,4]]
##due date for each order
duedates = [5000, 5000, 4500, 3000, 3000, 2750]

expected output: [[5,0,1], [4,3,2]]

Can you try the following:您可以尝试以下操作:

>>> import numpy as np
>>> orders = np.array([[0, 1, 5], [2, 3 ,4]])
>>> duedates = np.array([5000, 5000, 4500, 3000, 3000, 2750])
>>> duedates = duedates.reshape(orders.shape)
>>> for i, j in zip(orders, duedates):
...   print([x for __, x in sorted(zip(j, i))])
...
[5, 0, 1]
[4, 2, 3]
for order in orders:
    orders_due = [duedates[i] for i in order]
    sorted_order = sorted(zip(order, orders_due), key=lambda x:x[1])
    print([i for (i,j) in sorted_order]) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM