共有三个词典: trucks_dic,items_dic和loaded_trucks
如何从“卡车和物品字典”的键和值中替换卡车的键和值?
truck_dic = {6: [21.0, 7.0, 7.0],
7: [23.0, 7.0, 7.0],
8: [27.0, 7.0, 7.0],
9: [20.0, 7.0, 7.0],
10: [24.0, 7.0, 7.0],
11: [28.0, 8.0, 8.0],
12: [32.0, 8.0, 8.0],
13: [32.0, 9.0, 10.0],
14: [32.0, 9.0, 10.0],
15: [20.0, 8.0, 8.0],
16: [20.0, 8.0, 8.0]}
Items_dic = {0: [4.6, 4.3, 4.3],
1: [4.6, 4.3, 4.3],
2: [6.0, 5.6, 9.0],
3: [8.75, 5.6, 6.6],
4: [6.0, 5.16, 6.6],
5: [6.0, 5.6, 9.0],
6: [8.75, 5.6, 6.6],
7: [4.6, 4.3, 4.3],
8: [6.0, 5.6, 9.0],
9: [8.75, 5.6, 6.6],
10: [6.0, 5.16, 6.6]}
loaded_trucks = {'23.0x7.0x7.0': ['4.60x4.30x4.30, 4.60x4.30x4.30,
6.60x6.00x5.16, 6.60x6.00x5.16'],
'27.0x7.0x7.0': ['4.60x4.30x4.30, 9.00x6.00x5.60,
8.75x6.60x5.60'],
'20.0x8.0x8.0': ['9.00x6.00x5.60, 8.75x6.60x5.60'],
'21.0x7.0x7.0': ['9.00x6.00x5.60, 8.75x6.60x5.60']}
它给了
loaded_trucks = {'7': ['0', '7', '4', '10'],
'8': ['1', '5', '9'],
'16': ['2', '3'],
'6': ['6', '8']}
PS:loaded_trucks中的尺寸混乱
答案 0 :(得分:1)
truck_dic = {6: [21.0, 7.0, 7.0],
7: [23.0, 7.0, 7.0],
8: [27.0, 7.0, 7.0],
9: [20.0, 7.0, 7.0],
10: [24.0, 7.0, 7.0],
11: [28.0, 8.0, 8.0],
12: [32.0, 8.0, 8.0],
13: [32.0, 9.0, 10.0],
14: [32.0, 9.0, 10.0],
15: [20.0, 8.0, 8.0],
16: [20.0, 8.0, 8.0]}
Items_dic = {0: [4.6, 4.3, 4.3],
1: [4.6, 4.3, 4.3],
2: [6.0, 5.6, 9.0],
3: [8.75, 5.6, 6.6],
4: [6.0, 5.16, 6.6],
5: [6.0, 5.6, 9.0],
6: [8.75, 5.6, 6.6],
7: [4.6, 4.3, 4.3],
8: [6.0, 5.6, 9.0],
9: [8.75, 5.6, 6.6],
10: [6.0, 5.16, 6.6]}
loaded_trucks = {'23.0x7.0x7.0': ['4.60x4.30x4.30, 4.60x4.30x4.30, 6.60x6.00x5.16, 6.60x6.00x5.16'],
'27.0x7.0x7.0': ['4.60x4.30x4.30, 9.00x6.00x5.60, 8.75x6.60x5.60'],
'20.0x8.0x8.0': ['9.00x6.00x5.60, 8.75x6.60x5.60'],
'21.0x7.0x7.0': ['9.00x6.00x5.60, 8.75x6.60x5.60']}
# Build dimensions to truck and item mappings
truck_dict = {tuple(sorted(v)): str(k) for k, v in truck_dic.items()}
items_dict = dict()
for k, v in Items_dic.items():
items_dict.setdefault(tuple(sorted(v)), []).append(str(k))
# Generate result
result = dict()
def standardize(s):
return tuple(sorted(float(v) for v in s.split('x')))
for k, v in loaded_trucks.items():
# Note that the values in loaded_trucks are lists of a ONE str
# and not a list of strs of dimensions.
items = v[0].split(',')
items = [i.strip() for i in items]
print(items)
print([standardize(i) for i in items])
k = standardize(k)
result[truck_dict[k]] = [items_dict[standardize(i)].pop() for i in items]
输出result
是
{'7': ['7', '1', '10', '4'],
'8': ['0', '8', '9'],
'16': ['5', '6'],
'6': ['2', '3']}
请注意,相同尺寸的项目可以互换。例如,第5项和第8项具有相同的尺寸,因此可以互换。
此外,我们不在乎单个尺寸的顺序是否重新排序,因为这仅表示该项目已旋转。例如,尺寸为“ 4.60x4.30x4.30”的商品被视为与尺寸为“ 4.30x4.30x4.60”的另一商品相同。
最后,每个项目仅分配了一次,但是我们假设原始loaded_trucks
词典中的项目都在Items_dic
中进行了说明,因此不会出现短缺或剩余的问题。列表将产生错误。