【问题标题】:Merge every Every 6 dictionary into single dictionary of List将每 6 个字典合并为 List 的单个字典
【发布时间】:2015-11-16 06:54:47
【问题描述】:

问题:

[
{'field_id': u'36908'},{'field_name': u'Code'},{'field_value': u'900321'},
{'field_id': u'36909'},{'field_name': u'Description'}, {'field_value': u'TIG 2.4MM TUNGSTEN (EACH ROD)'},
{'field_id': u'36910'}, {'field_name': u'Quantity'}, {'field_value': u'2'},
{'field_id': u'36911'}, {'field_name': u'Price'}, {'field_value': u'21.00'}, 
{'field_id': u'36912'}, {'field_name': u'Line Total'}, {'field_value': u'42.00'}, 

{'field_id': u'36908'}, {'field_name': u'Code'}, {'field_value': u'92.01.15.08'}, 
{'field_id': u'36909'}, {'field_name': u'Description'}, {'field_value': u'BINZEL .8MM MIG TIPS MB15'},
{'field_id': u'36910'}, {'field_name': u'Quantity'}, {'field_value': u'6'}, 
{'field_id': u'36911'}, {'field_name': u'Price'}, {'field_value': u'2.60'},
{'field_id': u'36912'}, {'field_name': u'Line Total'}, {'field_value': u'15.60'}]

需要的输出:

[
{'Code':u'900321',
'Description':u'TIG 2.4MM TUNGSTEN (EACH ROD)',
'Quantity':u'2',
'Price':u'21.00',
'Line Total': u'42.00'},

{'Code':u'92.01.15.08',
'Description': u'BINZEL .8MM MIG TIPS MB15',
'Quantity':u'6',
'Price':u'2.60',
'Line Total': u'15.60'}]

我试图通过以下方式获得我的解决方案,但没有得到正确的结果。

first_list_dict=
[{'field_id': u'36908'}, {'field_name': u'Code'}, {'field_value': u'900321'}, {'field_id': u'36909'}, {'field_name': u'Description'}, {'field_value': u'TIG 2.4MM TUNGSTEN (EACH ROD)'}, {'field_id': u'36910'}, {'field_name': u'Quantity'}, {'field_value': u'2'}, {'field_id': u'36911'}, {'field_name': u'Price'}, {'field_value': u'21.00'}, {'field_id': u'36912'}, {'field_name': u'Line Total'}, {'field_value': u'42.00'}, {'field_id': u'36908'}, {'field_name': u'Code'}, {'field_value': u'92.01.15.08'}, {'field_id': u'36909'}, {'field_name': u'Description'}, {'field_value': u'BINZEL .8MM MIG TIPS MB15'}, {'field_id': u'36910'}, {'field_name': u'Quantity'}, {'field_value': u'6'}, {'field_id': u'36911'}, {'field_name': u'Price'}, {'field_value': u'2.60'}, {'field_id': u'36912'}, {'field_name': u'Line Total'}, {'field_value': u'15.60'}]

new_dict={}

for l in first_list_dict:
     new_dict.update({l.get('field_name'):l.get('field_value')})

但我的字典输出中有一些 None 结果值:

{u'Code': None, u'Description': None, u'Price': None, None: u'15.60', u'Line Total': None, u'Quantity': None}

【问题讨论】:

  • 你有什么尝试吗?
  • 是的,我在 first_list_dict 中为 l 做了:... newd.update({l.get('field_name'):l.get('field_value')}) 但我得到了一些值为 None
  • 在您的问题中发布。
  • 如果你在这个问题上展示你的尝试会很好:)
  • 好的,我也会添加它

标签: python list python-2.7 python-3.x dictionary


【解决方案1】:

注意事项:

  • 使用 python generator function method 遍历列表
  • 使用临时变量来计算和存储临时数据
  • 使用字典更新和获取方法

代码:

lst=[
{'field_id': u'36908'},{'field_name': u'Code'},{'field_value': u'900321'},
{'field_id': u'36909'},{'field_name': u'Description'}, {'field_value': u'TIG 2.4MM TUNGSTEN (EACH ROD)'},
{'field_id': u'36910'}, {'field_name': u'Quantity'}, {'field_value': u'2'},
{'field_id': u'36911'}, {'field_name': u'Price'}, {'field_value': u'21.00'},
{'field_id': u'36912'}, {'field_name': u'Line Total'}, {'field_value': u'42.00'},
{'field_id': u'36908'}, {'field_name': u'Code'}, {'field_value': u'92.01.15.08'},
{'field_id': u'36909'}, {'field_name': u'Description'}, {'field_value': u'BINZEL .8MM MIG TIPS MB15'},
{'field_id': u'36910'}, {'field_name': u'Quantity'}, {'field_value': u'6'},
{'field_id': u'36911'}, {'field_name': u'Price'}, {'field_value': u'2.60'},
{'field_id': u'36912'}, {'field_name': u'Line Total'}, {'field_value': u'15.60'}]

new_lst=[] # List to save output
dic={} # Temporary dictionary to create output dictionary 
count=0 # Count variable to count the list element
def iterating_list(lst): # Function to iterate over list
    for value in lst:
        yield value
iterating=iterating_list(lst)

for value in iterating :
    if value.get('field_name'): # If `field_name` matches in the given lists 
    #By default get method return `None` when there is no given key
        dic.update({value.get('field_name'):next(iterating).get('field_value')})
        count+=1
    if count==5: # Resetting when count reaches to 5 
        count=0
        new_lst.append(dic)
        dic={}
print new_lst

输出:

[{u'Price': u'21.00', u'Code': u'900321', u'Description': u'TIG 2.4MM TUNGSTEN (EACH ROD)', u'Line Total': u'42.00', u'Quantity': u'2'},
{u'Price': u'2.60', u'Code': u'92.01.15.08', u'Description': u'BINZEL .8MM MIG TIPS MB15', u'Line Total': u'15.60', u'Quantity': u'6'}]

【讨论】:

  • 非常感谢 Vignesh Kalai
  • @DaSaDiYaChaiTAnYa 很乐意提供帮助 :)
猜你喜欢
  • 2011-05-04
  • 2021-03-13
  • 1970-01-01
  • 2019-05-16
  • 2011-03-30
  • 2017-01-04
  • 2017-07-23
  • 2018-04-16
相关资源
最近更新 更多