在 Python 中的字典列表中提取最优解
Extract optimal solution in list of dictionaries in Python
我有一个问题,就是要找到为不同公司提供成本最低的选择的地点和工厂。因此,给出名单如下:
[[{4: [14.038, 28.423, 43.483, 58.00699999999999]},
{3: [31.645999999999997, 78.993, 110.59100000000001]},
{1: [30.967000000000002, 68.55000000000001, 100.32900000000001]}],
[{4: [27.651999999999997, 59.993, 93.76999999999998, 122.67799999999998]},
{3: [34.604, 80.786, 116.641]},
{2: [59.28399999999999, 136.2, 206.18, 277.543, 340.03800000000007]},
{1: [32.953, 77.665, 123.44800000000001]}],
[{4: [15.295000000000002, 30.656, 47.892999999999994, 63.731999999999985]},
{3: [29.290999999999997, 74.506, 110.141]}],
[{4: [36.424, 67.84299999999999, 99.04999999999998, 134.88299999999998]},
{3: [39.557, 75.643, 111.09500000000001]}]]
例如,第一家公司是:
[{4: [14.038, 28.423, 43.483, 58.00699999999999]},
{3: [31.645999999999997, 78.993, 110.59100000000001]},
{1: [30.967000000000002, 68.55000000000001, 100.32900000000001]}]
成本最低的选项是:位置 4,工厂 0,成本为 14.038。
我希望能够找到 return 该特定解决方案的正确代码。此外,我想知道是否有办法 return 第二好的解决方案。对于第一家公司,我们将有:位置 4,工厂 1,成本为 28.423。
谢谢!
from operator import itemgetter
temp = [[{4: [14.038, 28.423, 43.483, 58.00699999999999]},
{3: [31.645999999999997, 78.993, 110.59100000000001]},
{1: [30.967000000000002, 68.55000000000001, 100.32900000000001]}],
[{4: [27.651999999999997, 59.993, 93.76999999999998, 122.67799999999998]},
{3: [34.604, 80.786, 116.641]},
{2: [59.28399999999999, 136.2, 206.18, 277.543, 340.03800000000007]},
{1: [32.953, 77.665, 123.44800000000001]}],
[{4: [15.295000000000002, 30.656, 47.892999999999994, 63.731999999999985]},
{3: [29.290999999999997, 74.506, 110.141]}],
[{4: [36.424, 67.84299999999999, 99.04999999999998, 134.88299999999998]},
{3: [39.557, 75.643, 111.09500000000001]}]]
#returns tuple: (index of lowest value, value)
def get_lowest_value_in_list(location_arr):
i, value = min(enumerate(location_arr), key=itemgetter(1))
return i,value
lowest = False
for i, firm in enumerate(temp):
for j,location in enumerate(firm):
location_id = list(location.keys())[0]
index, value = get_lowest_value_in_list( temp[i][j][location_id] )
if lowest:
current_lowest_value = temp[lowest[0]][lowest[1]][lowest[2]][lowest[3]]
if current_lowest_value > value:
lowest = (i,j,location_id,index)
else:
lowest = (i,j,location_id,index)
print(lowest)
result = temp[lowest[0]][lowest[1]][lowest[2]][lowest[3]]
print(result)
lowest
是一个存储最低值位置的元组(firm,location_index,location id,index of lowest value)
一个可能的解决方案是对每个公司、地点和工厂的所有成本进行排序,并取成本较低的 k
,其中 k
是(公司、地点、工厂)的选项数量) 您正在寻找:
k = 2
best_k = sorted(
[(c, firm_id, location_id, plant_id)
for firm_id, plant_res in enumerate(costs)
for location_res in plant_res
for location_id, location_costs in location_res.items()
for plant_id, c in enumerate(location_costs)
]
)[:k]
其中 costs
是您问题中的列表。
此处best_k
:
[(14.038, 0, 4, 0), (15.295000000000002, 2, 4, 0), (27.651999999999997, 1, 4, 0)]
因此,成本较低的选项是 14.038,由公司 0 在位置 4、工厂 0 提供。
第二个选项是 15.295,由公司 2 在地点 4,工厂 0 提供。
最后,第一个选项是公司 1 在位置 4,工厂 0 提供的 27.652。
我有一个问题,就是要找到为不同公司提供成本最低的选择的地点和工厂。因此,给出名单如下:
[[{4: [14.038, 28.423, 43.483, 58.00699999999999]},
{3: [31.645999999999997, 78.993, 110.59100000000001]},
{1: [30.967000000000002, 68.55000000000001, 100.32900000000001]}],
[{4: [27.651999999999997, 59.993, 93.76999999999998, 122.67799999999998]},
{3: [34.604, 80.786, 116.641]},
{2: [59.28399999999999, 136.2, 206.18, 277.543, 340.03800000000007]},
{1: [32.953, 77.665, 123.44800000000001]}],
[{4: [15.295000000000002, 30.656, 47.892999999999994, 63.731999999999985]},
{3: [29.290999999999997, 74.506, 110.141]}],
[{4: [36.424, 67.84299999999999, 99.04999999999998, 134.88299999999998]},
{3: [39.557, 75.643, 111.09500000000001]}]]
例如,第一家公司是:
[{4: [14.038, 28.423, 43.483, 58.00699999999999]},
{3: [31.645999999999997, 78.993, 110.59100000000001]},
{1: [30.967000000000002, 68.55000000000001, 100.32900000000001]}]
成本最低的选项是:位置 4,工厂 0,成本为 14.038。
我希望能够找到 return 该特定解决方案的正确代码。此外,我想知道是否有办法 return 第二好的解决方案。对于第一家公司,我们将有:位置 4,工厂 1,成本为 28.423。
谢谢!
from operator import itemgetter
temp = [[{4: [14.038, 28.423, 43.483, 58.00699999999999]},
{3: [31.645999999999997, 78.993, 110.59100000000001]},
{1: [30.967000000000002, 68.55000000000001, 100.32900000000001]}],
[{4: [27.651999999999997, 59.993, 93.76999999999998, 122.67799999999998]},
{3: [34.604, 80.786, 116.641]},
{2: [59.28399999999999, 136.2, 206.18, 277.543, 340.03800000000007]},
{1: [32.953, 77.665, 123.44800000000001]}],
[{4: [15.295000000000002, 30.656, 47.892999999999994, 63.731999999999985]},
{3: [29.290999999999997, 74.506, 110.141]}],
[{4: [36.424, 67.84299999999999, 99.04999999999998, 134.88299999999998]},
{3: [39.557, 75.643, 111.09500000000001]}]]
#returns tuple: (index of lowest value, value)
def get_lowest_value_in_list(location_arr):
i, value = min(enumerate(location_arr), key=itemgetter(1))
return i,value
lowest = False
for i, firm in enumerate(temp):
for j,location in enumerate(firm):
location_id = list(location.keys())[0]
index, value = get_lowest_value_in_list( temp[i][j][location_id] )
if lowest:
current_lowest_value = temp[lowest[0]][lowest[1]][lowest[2]][lowest[3]]
if current_lowest_value > value:
lowest = (i,j,location_id,index)
else:
lowest = (i,j,location_id,index)
print(lowest)
result = temp[lowest[0]][lowest[1]][lowest[2]][lowest[3]]
print(result)
lowest
是一个存储最低值位置的元组(firm,location_index,location id,index of lowest value)
一个可能的解决方案是对每个公司、地点和工厂的所有成本进行排序,并取成本较低的 k
,其中 k
是(公司、地点、工厂)的选项数量) 您正在寻找:
k = 2
best_k = sorted(
[(c, firm_id, location_id, plant_id)
for firm_id, plant_res in enumerate(costs)
for location_res in plant_res
for location_id, location_costs in location_res.items()
for plant_id, c in enumerate(location_costs)
]
)[:k]
其中 costs
是您问题中的列表。
此处best_k
:
[(14.038, 0, 4, 0), (15.295000000000002, 2, 4, 0), (27.651999999999997, 1, 4, 0)]
因此,成本较低的选项是 14.038,由公司 0 在位置 4、工厂 0 提供。 第二个选项是 15.295,由公司 2 在地点 4,工厂 0 提供。 最后,第一个选项是公司 1 在位置 4,工厂 0 提供的 27.652。