如何通过对一个列表进行排序来对多个列表进行排序?

时间:2020-09-15 13:16:29

标签: python list sorting

所以我有一个关于列表和赋值的问题。我有5个客户和一些人为创建的随机变量(价格和成本)。我想将这些值分配给客户1、2 ... 5,以便将这些值固定给客户。

因此,对于价格为均值10和标准偏差为1的客户,客户1应该具有随机创建的价值。对于客户的均值8和标准偏差为1的成本,该客户应该具有随机产生的价值。

应该为每个客户完成此操作。我该如何为每个客户编写代码(这样我就不必为每个客户创建带有价格[...]和成本[...]的新行?下一个问题是,我如何按成本排序这样客户4的成本最高(= 9),那么客户1的成本最高(= 9),客户1、3、5的成本最高,最后是客户3的成本。那么我也希望能够通过此排名对其他价格进行排序。代码如下:

avg_price = [10 3 5 5 8] 
avg_costs = [8 3 5 9 4]
std_dev_price = [1 2 1 1 2]
std_dev_costs = [1 1 2 2 1]
num_reps = 10
price0 = np.random.normal(avg_price[0], std_dev_price[0], num_reps)
costs0 = np.random.normal(avg_costs[0], std_dev_costs[0], num_reps)
...
price4 = np.random.normal(avg_price[4], std_dev_price[4], num_reps)
costs4 = np.random.normal(avg_costs[4], std_dev_costs[4], num_reps)

1 个答案:

答案 0 :(得分:0)

要创建规范化数组,可以使用数组和循环。要按单个列表中的值对所有列表进行排序,可以将它们合并到一个数据框中,然后对该数据框进行排序。

尝试以下代码:

import numpy as np
import pandas as pd

avg_price = [10, 3, 5, 5, 8] 
avg_costs = [8, 3, 5, 9, 4]
std_dev_price = [1, 2, 1, 1, 2]
std_dev_costs = [1, 1, 2, 2, 1]

print('Start:')
print(' avg_price',avg_price,'\n','avg_costs',avg_costs,'\n','std_dev_price',std_dev_price,'\n','std_dev_costs',std_dev_costs,'\n')

num_reps = 10
price = []
costs = []
for i in range(len(avg_price)):
    price.append(np.random.normal(avg_price[i], std_dev_price[i], num_reps))
    costs.append(np.random.normal(avg_costs[i], std_dev_costs[i], num_reps))
    
# merge lists to dataframe
dd = {'avg_price':avg_price,'avg_costs':avg_costs,'std_dev_price':std_dev_price,'std_dev_costs':std_dev_costs,'price':price,'costs':costs}
df = pd.DataFrame(dd) 

# sort all columns by avg_costs
df = df.sort_values(by=['avg_costs'], ascending=False)  

# revert back to lists
avg_price = df['avg_price'].to_list()
avg_costs = df['avg_costs'].to_list()
std_dev_price = df['std_dev_price'].to_list()
std_dev_costs = df['std_dev_costs'].to_list()
price = df['price'].to_list()
costs = df['costs'].to_list()

print('Sorted by avg costs:')
print(' avg_price',avg_price,'\n','avg_costs',avg_costs,'\n','std_dev_price',std_dev_price,'\n','std_dev_costs',std_dev_costs,'\n')

print('Full Dataframe:')
print(df)

输出(对齐)

 Start:
 avg_price     [10, 3, 5, 5, 8]
 avg_costs     [8,  3, 5, 9, 4]
 std_dev_price [1,  2, 1, 1, 2]
 std_dev_costs [1,  1, 2, 2, 1]
 
 Sorted by avg costs:
 avg_price     [5, 10, 5, 8, 3]
 avg_costs     [9, 8,  5, 4, 3]
 std_dev_price [1, 1,  1, 2, 2]
 std_dev_costs [2, 1,  2, 1, 1]

 Full Datframe:
   avg_price  avg_costs  std_dev_price  std_dev_costs                                              price                                              costs
3          5          9              1              2  [2.5092028090378355, 4.81567722966441, 5.49058...  [10.563731356333232, 6.647727831982642, 8.0096...
0         10          8              1              1  [9.341336323005978, 9.810670988947805, 10.7077...  [7.339563107482252, 8.790936460961769, 8.40626...
2          5          5              1              2  [5.656265155737828, 4.975811559532342, 6.49261...  [1.6639841191325848, 3.691022076817463, 7.0424...
4          8          4              2              1  [7.868428684276111, 8.65633376629758, 11.67775...  [4.141047169505752, 4.25009712746727, 2.993419...
1          3          3              2              1  [2.920446641165024, 3.958967186635695, 3.31161...  [3.5024403403983557, 3.7703884020352803, 2.708...