【问题标题】:convert 2-d list into dictionary将二维列表转换为字典
【发布时间】:2019-09-17 08:56:29
【问题描述】:

我想从三个二维列表创建一个字典。例如:

list1 = [['Brand', 'Release Date', 'Related'], ['Structure', 'Screen', 'Photos', 'Videos']]

list2 = [['Brand', 'Aliases'], ['Release date', 'State'], ['Predecessor', 'Successors'], ['Size', 'Aspect Ratio', 'Weight', 'Usable surface', 'Materials', 'Colors', 'Origami']]

list3 = [['OppoSmartphones by Oppo', 'PAFM00, Oppo Find X Standard Edition'], ['June 2018, 10 months ago', 'On Sale'], ['Oppo Find 7', 'Oppo Reno'], ['74.2 mm x 156.7 mm x 9.4 mm', '19:9', '186 g', '87 %', 'Glass', 'Turquoise Violet', 'Print 3D Model']]

我想要的结果是

{'Brand':{'Brand': 'OppoSmartphones by Oppo', 'Aliases': 'PAFM00, Oppo Find X Standard Edition'}, 'Release Date':{ 'Release date': 'June 2018, 10 months ago', 'State': 'On Sale'}, 'Related':{'Predecessor': 'Oppo Find 7', 'Successors': 'Oppo Reno'}}

【问题讨论】:

  • 是什么阻止你这样做?
  • 输出中的['Structure', 'Screen', 'Photos', 'Videos'] 发生了什么?
  • 什么都没发生。我只是不想显示结果太长。

标签: python list dictionary


【解决方案1】:

你可以使用循环,dict()zip() 来实现,如下所示:

list1 = [['Brand', 'Release Date', 'Related'], 
         ['Structure', 'Screen', 'Photos', 'Videos']]

list2 = [['Brand', 'Aliases'], ['Release date', 'State'],
         ['Predecessor', 'Successors'],
         ['Size', 'Aspect Ratio', 'Weight', 'Usable surface', 'Materials', 'Colors', 'Origami']]

list3 = [['OppoSmartphones by Oppo', 'PAFM00, Oppo Find X Standard Edition'],
         ['June 2018, 10 months ago', 'On Sale'],
         ['Oppo Find 7', 'Oppo Reno'],
         ['74.2 mm x 156.7 mm x 9.4 mm', '19:9', '186 g', '87 %', 'Glass', 'Turquoise Violet', 'Print 3D Model']]

d1 = {}
for i in range(len(list1)):
    for j in range(len(list1[i])):
        #print(i,j)
        d1[list1[i][j]] = dict(zip(list2[j], list3[j]))


# to get this patter: dict = {list1[0][0]:{list2[0][0]:list3[0][0], list2[0][1]:list3[0][1]},
#                             list1[0][1]:{list2[1][0]:list3[1][0], list2[1][1]:list3[1][1]},
#                             list1[0][2]:{list2[2][0]:list3[2][0], list2[2][1]:list3[2][1]}}
d2 = {}
for i in range(len(list1[0])):
    if list1[0][i] not in d2.keys(): d2[list1[0][i]] = {}
    if list1[0][i] not in d2.keys(): d2[list1[0][i]] = {}

    d2[list1[0][i]][list2[i][0]] = list3[i][0] 
    d2[list1[0][i]][list2[i][1]] = list3[i][1] 


# if you want to apply the behaviour on all elements:
d3 = {}
for k in range(len(list1)): 
    for i in range(len(list1[k])):
        if list1[k][i] not in d3.keys(): d3[list1[k][i]] = {}
        if list1[k][i] not in d3.keys(): d3[list1[k][i]] = {}

        d3[list1[k][i]][list2[i][0]] = list3[i][0] 
        d3[list1[k][i]][list2[i][1]] = list3[i][1] 

输出(d2):

{      'Brand': {'Aliases': 'PAFM00, Oppo Find X Standard Edition',
                 'Brand': 'OppoSmartphones by Oppo'},

     'Related': {'Predecessor': 'Oppo Find 7', 
                  'Successors': 'Oppo Reno'},

'Release Date': {'Release date': 'June 2018, 10 months ago',
                        'State': 'On Sale'}}

【讨论】:

  • 它工作正常..但不适用于所有长度的列表...意思是...list1 的长度为 7,list2 和 list3 的长度均为 30
  • 那么描述的模式不清楚。这仅适用于长度相等的列表。
  • list1= [['Brand', 'Release Date', 'Related']] list2= [['Brand', 'Aliases'], ['Release date', 'State'], ['Predecessor', 'Successors']] list3= [['OppoSmartphones by Oppo', 'PAFM00, Oppo Find X Standard Edition'], ['June 2018, 10 months ago', 'On Sale'], ['Oppo查找 7', 'Oppo Reno']]
  • 模式应该是这样的..dict = {list1[0][0]:{list2[0][0]:list3[0][0], list2[0][1] :list3[0][1]}, list1[0][1]:{list2[1][0]:list3[1][0], list2[1][1]:list3[1][1] }, list1[0][3]:{list2[2][0]:list3[2][0], list2[2][1]:list3[2][1]}}
  • 我的代码为您提供了如何实现这一目标的良好开端,您应该能够操纵索引以生成所需的行为。
【解决方案2】:

试试:

res = {}

for i in range(3):
    res[list1[0][i]] = {list2[i][0]:list3[i][0], list2[i][1]:list3[i][1]}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-16
    • 1970-01-01
    • 2020-04-05
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多