将Every Every 6字典合并为List的单个字典

时间:2015-11-16 06:54:48

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

问题:

[
{'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')})

但是我在字典输出中得到了一些无结果值:

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

1 个答案:

答案 0 :(得分: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'}]