Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,根据另一个子列表中相应的权重对子列表进行排序_Python_Numpy_Nested Lists - Fatal编程技术网

在python中,根据另一个子列表中相应的权重对子列表进行排序

在python中,根据另一个子列表中相应的权重对子列表进行排序,python,numpy,nested-lists,Python,Numpy,Nested Lists,我有两个嵌套列表list1和list2list1表示我需要排序的值的子列表,根据list2中表示list1权重的第二个子列表: list1 = [[0.002, 0.001, 0.002, 0.1, 0.001, 0.2, 0.03, 0.04, 0.002, 0.03, 0.004, 0.0004, 0.005, 0.05], [0.007, 0.0001, 0.0002, 0.0001, 0.001, 0.02, 0.0003, 0.007, 0.007, 0.008, 0

我有两个嵌套列表
list1
list2
list1
表示我需要排序的值的子列表,根据
list2
中表示
list1
权重的第二个子列表:

list1 = [[0.002, 0.001, 0.002, 0.1, 0.001, 0.2, 0.03, 0.04, 0.002, 0.03, 0.004, 0.0004, 0.005, 0.05],
         [0.007, 0.0001, 0.0002, 0.0001, 0.001, 0.02, 0.0003, 0.007, 0.007, 0.008, 0.0006, 0.0004, 0.005, 0.05],
         [0.0009, 0.000, 0.0002, 0.9, 0.091, 0.2, 0.03, 0.04, 0.002, 0.03, 0.004, 0.0004, 0.005, 0.0009],
         [0.002, 0.001, 0.002, 0.1, 0.001, 0.2, 0.03, 0.04, 0.002, 0.03, 0.004, 0.0004, 0.005, 0.055]]
   


  list2 =[[0.06165411, 0.04111233, 0.06165411, 0.049487340000000005, 0.04111233, 0.020541780000000003, 0.04111233, 0.020541780000000003, 0.06165411, 0.04111233, 0.020541780000000003, 0.0, 0.0, 0.049487340000000005], 
          [0.29708409, 0.19810227000000002, 0.049487340000000005, 0.19810227000000002, 0.020541780000000003, 0.09898182000000001, 0.09898182000000001, 0.29708409, 0.29708409, 0.09898182000000001, 0.09898182000000001, 0.0, 0.0, 0.049487340000000005], 
          [0.19810227000000002, 0.09898182000000001, 0.049487340000000005, 0.09898182000000001, 0.09898182000000001, 0.020541780000000003, 0.04111233, 0.020541780000000003, 0.020541780000000003, 0.04111233, 0.020541780000000003, 0.0, 0.0, 0.19810227000000002], 
          [0.06165411, 0.04111233, 0.06165411, 0.049487340000000005, 0.04111233, 0.020541780000000003, 0.04111233, 0.020541780000000003, 0.06165411, 0.04111233, 0.020541780000000003, 0.0, 0.0, 0.09898182000000001]]
我想根据
list2
中相应的排序子列表对
list1
中的每个子列表进行排序。我编写了以下代码:

def sort_list(list1, list2):
    z = [x for _, x in sorted(zip(list2, list1))]
    return(z)

sort_fea = []
for i in range(len(list1)):
    c1 = list1[i]
    c2 = list2[i]
    sort_fea.append(sort_list(c1, c2))
print('The sorted_list1 =',sort_fea)
我得到了以下正确的结果:

sorted_list1 =[[0.0004, 0.005, 0.004, 0.04, 0.2, 0.001, 0.001, 0.03, 0.03, 0.05, 0.1, 0.002, 0.002, 0.002],
               [0.0004, 0.005, 0.001, 0.0002, 0.05, 0.0003, 0.0006, 0.008, 0.02, 0.0001, 0.0001, 0.007, 0.007, 0.007],
               [0.0004, 0.005, 0.002, 0.004, 0.04, 0.2, 0.03, 0.03, 0.0002, 0.0, 0.091, 0.9, 0.0009, 0.0009],
               [0.0004, 0.005, 0.004, 0.04, 0.2, 0.001, 0.001, 0.03, 0.03, 0.1, 0.002, 0.002, 0.002, 0.055]]
是否有使用
numpy
或任何其他方法的更快的方法

sort_fea = []
for i in range(len(list1)):
    result = [x for _, x in sorted(zip(list2[i], list1[i]))]
    sort_fea.append(result)
print(sort_fea)
输出:

[[0.0004, 0.005, 0.004, 0.04, 0.2, 0.001, 0.001, 0.03, 0.03, 0.05, 0.1, 0.002, 0.002, 0.002],
 [0.0004, 0.005, 0.001, 0.0002, 0.05, 0.0003, 0.0006, 0.008, 0.02, 0.0001, 0.0001, 0.007, 0.007, 0.007],
 [0.0004, 0.005, 0.002, 0.004, 0.04, 0.2, 0.03, 0.03, 0.0002, 0.0, 0.091, 0.9, 0.0009, 0.0009],
 [0.0004, 0.005, 0.004, 0.04, 0.2, 0.001, 0.001, 0.03, 0.03, 0.1, 0.002, 0.002, 0.002, 0.055]]

据我所知,这将加快您的代码速度。

也许使用
pandas
的解决方案会更快:

import pandas as pd

list1 = [[0.002, 0.001, 0.002, 0.1, 0.001, 0.2, 0.03, 0.04, 0.002, 0.03, 0.004, 0.0004, 0.005, 0.05],
         [0.007, 0.0001, 0.0002, 0.0001, 0.001, 0.02, 0.0003, 0.007, 0.007, 0.008, 0.0006, 0.0004, 0.005, 0.05],
         [0.0009, 0.000, 0.0002, 0.9, 0.091, 0.2, 0.03, 0.04, 0.002, 0.03, 0.004, 0.0004, 0.005, 0.0009],
         [0.002, 0.001, 0.002, 0.1, 0.001, 0.2, 0.03, 0.04, 0.002, 0.03, 0.004, 0.0004, 0.005, 0.055]]

list2 =[[0.06165411, 0.04111233, 0.06165411, 0.049487340000000005, 0.04111233, 0.020541780000000003, 0.04111233, 0.020541780000000003, 0.06165411, 0.04111233, 0.020541780000000003, 0.0, 0.0, 0.049487340000000005],
          [0.29708409, 0.19810227000000002, 0.049487340000000005, 0.19810227000000002, 0.020541780000000003, 0.09898182000000001, 0.09898182000000001, 0.29708409, 0.29708409, 0.09898182000000001, 0.09898182000000001, 0.0, 0.0, 0.049487340000000005],
          [0.19810227000000002, 0.09898182000000001, 0.049487340000000005, 0.09898182000000001, 0.09898182000000001, 0.020541780000000003, 0.04111233, 0.020541780000000003, 0.020541780000000003, 0.04111233, 0.020541780000000003, 0.0, 0.0, 0.19810227000000002],
          [0.06165411, 0.04111233, 0.06165411, 0.049487340000000005, 0.04111233, 0.020541780000000003, 0.04111233, 0.020541780000000003, 0.06165411, 0.04111233, 0.020541780000000003, 0.0, 0.0, 0.09898182000000001]]

df = pd.DataFrame({'l1': list1, 'l2': list2}).apply(pd.Series.explode)
out = df.groupby(level=0).apply(lambda x: x.sort_values(['l2', 'l1'])['l1'].values.tolist()).tolist()

print(out)
印刷品:

[[0.0004, 0.005, 0.004, 0.04, 0.2, 0.001, 0.001, 0.03, 0.03, 0.05, 0.1, 0.002, 0.002, 0.002], 
 [0.0004, 0.005, 0.001, 0.0002, 0.05, 0.0003, 0.0006, 0.008, 0.02, 0.0001, 0.0001, 0.007, 0.007, 0.007], 
 [0.0004, 0.005, 0.002, 0.004, 0.04, 0.2, 0.03, 0.03, 0.0002, 0.0, 0.091, 0.9, 0.0009, 0.0009], 
 [0.0004, 0.005, 0.004, 0.04, 0.2, 0.001, 0.001, 0.03, 0.03, 0.1, 0.002, 0.002, 0.002, 0.055]]

谢谢你的回答。只有一个问题是,您的代码的结果是连续列表,而我需要的结果是子列表的原始列表,如sort_fea=[,…],[,…],[,…],…]您能看看我新编辑的答案吗?我想,我希望答案能解决你的问题。