如何使用列表列表和元组列表创建字典词典

时间:2020-07-15 04:39:55

标签: python python-3.x list dictionary

我想使用列表(距离)列表和元组列表(路线)创建字典结果。如何获得结果字典?

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是Warehouse的名称,ind1,ind2,ind3是商店的名称。

4 个答案:

答案 0 :(得分:0)

为了方便起见,我将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

输出:

{'A': {'ind1': '167724.1407', 'ind2': '151859.5908', 'ind3': '150131.7254'}, 'B': {'ind1': '186216.5193', 'ind2': '170351.9694', 'ind3': '168624.1039'}}

答案 1 :(得分:0)

请使用数据结构:

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'}}

答案 2 :(得分:0)

您还可以使用std::removechain.from_iterable + zip

itertools

from itertools import chain

result = {}

for (a,b), d in zip(routes, chain.from_iterable(distance)):
    result.setdefault(a, {}).update({b: d})

答案 3 :(得分:0)

如果您不喜欢列表理解,那么这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'}}