【问题标题】:Merging to lists of different length to for list of dictionaries合并到不同长度的列表以获取字典列表
【发布时间】:2017-11-30 10:47:23
【问题描述】:

您好,请帮我开发一个遵循的逻辑。

list_1 = [1,2,3]
list_2 = [a,b,c,d,e,f,g,h,i]

所需的输出(字典列表):

output = [{1:a,2:b,3:c}, {1:d,2:e,3:f}, {1:g,2:h,3:i}]

我的脚本:

return_list = []
k = 0
temp_dict = {}

for i, value in enumerate(list_2):
    if k <= len(list_1)-1:
        temp_dict[list_1[k]] = value
        if k == len(list_1)-1:
            k = 0
            print temp_dict
            return_list.append(temp_dict)
            print return_list
            print '\n'
        else:
            k = k + 1
print return_list

我的输出:

{1: 'a', 2: 'b', 3: 'c'}
[{1: 'a', 2: 'b', 3: 'c'}]


{1: 'd', 2: 'e', 3: 'f'}
[{1: 'd', 2: 'e', 3: 'f'}, {1: 'd', 2: 'e', 3: 'f'}]


{1: 'g', 2: 'h', 3: 'i'}
[{1: 'g', 2: 'h', 3: 'i'}, {1: 'g', 2: 'h', 3: 'i'}, {1: 'g', 2: 'h', 3: 'i'}]


[{1: 'g', 2: 'h', 3: 'i'}, {1: 'g', 2: 'h', 3: 'i'}, {1: 'g', 2: 'h', 3: 'i'}]

如您所见, temp_dict 打印正确,但 return_list 是最后一个 temp_dict 3 次。 请帮忙解决。

【问题讨论】:

    标签: python-2.7 list dictionary merge


    【解决方案1】:

    这里的问题是您没有将temp_dict 重置为新对象。 当您将它附加到列表时,它仍然保持对 dict 对象的引用,在您在下一个循环中更改它之后,它会更改数组上的值,因为它是相同的引用。

    如果你重置它应该可以工作的值

    list_1 = [1,2,3]
    list_2 = ['a','b','c','d','e','f','g','h','i']
    
    return_list = []
    k = 0
    temp_dict = {}
    
    for i, value in enumerate(list_2):
        if k <= len(list_1)-1:
            temp_dict[list_1[k]] = value
            if k == len(list_1)-1:
                k = 0
                print temp_dict
                return_list.append(temp_dict)
                temp_dict = {} # Here is the change
                print return_list
                print '\n'
            else:
                k = k + 1
    print return_list
    

    【讨论】:

      【解决方案2】:

      这应该可行:

      list_1 = [1,2,3]
      list_2 = ['a','b','c','d','e','f','g','h','i']
      
      output = []
      
      j = 0
      for i in range(1, len(list_1) + 1):
          output.append(dict(zip(list_1, list_2[j:i * 3])))
          j = i * 3
      
      print(output)
      

      假设您的第二个列表正好是第一个列表的 3 倍。

      【讨论】:

        【解决方案3】:
        def merge_them(list1, list2):
            output = []
            i = 0
            while i < len(list_2):
                output.append(dict(zip(list_1, list_2[i: i + len(list1)])))
                i += len(list1)
        
            return output
        

        你可以测试一下:

        测试1:

        list_1 = [1,2,3]
        list_2 = ['a','b','c','d','e','f','g','h','i']
        print merge_them(list_1, list_2)
        

        你会得到:

        [{1: 'a', 2: 'b', 3: 'c'}, {1: 'd', 2: 'e', 3: 'f'}, {1: 'g', 2: 'h', 3: 'i'}]
        

        测试2:

        list_1 = [1,2,3,]
        list_2 = ['a','b','c','d','e']
        print merge_them(list_1, list_2)
        
        you will get:
        
        [{1: 'a', 2: 'b', 3: 'c'}, {1: 'd', 2: 'e'}]
        

        测试3:

        list_1 = [1,2,3,4,5,6,7,8]
        list_2 = ['a','b','c','d','e']
        print merge_them(list_1, list_2)
        
        you will get:
        
        [{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}]
        

        【讨论】:

          猜你喜欢
          • 2017-12-04
          • 2020-06-27
          • 2022-11-13
          • 2020-01-11
          • 2020-04-10
          • 2017-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多