[英]How to create a dictionary of dictionaries using a list of lists and a list of tuples
我想使用列表列表(距離)和元組列表(路由)創建字典結果。 我可以做些什么來獲取 Result 字典?
distance = [['167724.1407', '151859.5908', '150131.7254'],
['186216.5193', '170351.9694', '168624.1039']]
routes = [('A', 'ind1'), ('A', 'ind2'), ('A', 'ind3'),
('B', 'ind1'), ('B', 'ind2'), ('B', 'ind3')]
Result = {'A': {'ind1': '167724.1407', 'ind2': '151859.5908', 'ind3': '150131.7254' },
'B': {'ind1': '186216.5193', 'ind2': '170351.9694', 'ind3': '168624.1039' }}
[編輯] 我正在用 PULP 解決線性規划問題,所以 A 和 B 是倉庫的名稱,而 ind1、ind2、ind3 是商店的名稱。
我已經展平了distance
列表以使其變得容易,並使用defaultdicct
來更新現有字典以包含新值。
from collections import defaultdict
distance = [
["167724.1407", "151859.5908", "150131.7254"],
["186216.5193", "170351.9694", "168624.1039"],
]
routes = [
("A", "ind1"),
("A", "ind2"),
("A", "ind3"),
("B", "ind1"),
("B", "ind2"),
("B", "ind3"),
]
flat_distance = [d for sublist in distance for d in sublist]
result = defaultdict(dict)
for index, route in enumerate(routes):
result[route[0]].update({route[1]: flat_distance[index]})
result = dict(result) # converting defaultdict back to dict
Output:
{'A': {'ind1': '167724.1407', 'ind2': '151859.5908', 'ind3': '150131.7254'}, 'B': {'ind1': '186216.5193', 'ind2': '170351.9694', 'ind3': '168624.1039'}}
請使用數據結構:
distance = [['167724.1407', '151859.5908', '150131.7254'], ['186216.5193', '170351.9694', '168624.1039']]
routes = [('A', 'ind1'), ('A', 'ind2'), ('A', 'ind3'), ('B', 'ind1'), ('B', 'ind2'), ('B', 'ind3')]
index_dict = {} # e.g. A : 0, B : 1, helper dictionalry
result = {} # final dictionaly
index = 0
for key, inside_key in routes:
if index_dict.get(key) == None:
index_dict[key] = index # save index of dict value of routes, so you can get respective distance value list
index = index + 1
if result.get(key) == None:
result[key] = {} # initialize dict for e.g. A : {}
# finally store it in dictionary
result[key][inside_key] = distance[index_dict[key]][len(result[key])]
print(result)
使用上面的代碼,你可以得到結果:
{'A': {'ind1': '167724.1407', 'ind2': '151859.5908', 'ind3': '150131.7254'}, 'B': {'ind1': '186216.5193', 'ind2': '170351.9694', 'ind3': '168624.1039'}}
您也可以使用itertools
, chain.from_iterable
+ zip
from itertools import chain
result = {}
for (a,b), d in zip(routes, chain.from_iterable(distance)):
result.setdefault(a, {}).update({b: d})
{'A': {'ind1': '167724.1407', 'ind2': '151859.5908', 'ind3': '150131.7254'},
'B': {'ind1': '186216.5193', 'ind2': '170351.9694', 'ind3': '168624.1039'}}
如果您對列表推導很感興趣,那么這 2 行代碼可能會讓您感興趣:)
from collections import defaultdict
from operator import itemgetter
from itertools import groupby
kk = dict((k, [v[1] for v in itr]) for k, itr in groupby(routes, itemgetter(0)))
out = {list(kk.keys())[i]:dict(zip(list(kk.values())[i],distance[i])) for i in range(len(kk.keys()))}
print(out)
'A': {'ind1': '167724.1407', 'ind2': '151859.5908', 'ind3': '150131.7254'},
'B': {'ind1': '186216.5193', 'ind2': '170351.9694', 'ind3': '168624.1039'}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.