将字典字典压缩到python中的列表中

时间:2013-02-07 00:40:56

标签: python mysql list dictionary

我有一本字典词典,需要将其压缩成一个列表。我的目标是以一种可以发送到MySQL数据库的形式。字典的格式为

{
 'Customer 1': 
   {'Vitamin A': 0, 'Calcium': 1, 'Protein': 1, 'B vitamins': 0, 'HPMR': 0},
 'Customer 2': 
   {'Vitamin A': 0, 'Calcium': 0, 'Protein': 1, 'B vitamins': 1, 'HPMR': 0}
 }

我希望它最终成像:

('Customer 1', 'Vitamin A', 0), ('Customer 1', 'Calcium', 1)
...
('Customer 2', 'Vitamin A', 0), ('Customer 2', 'Calcium', 0)`

感谢您的帮助

5 个答案:

答案 0 :(得分:2)

customers = {'Customer 1': {'Vitamin A': 0, 
                            'Calcium': 1, 
                            'Protein': 1, 
                            'B vitamins': 0, 
                            'HPMR': 0}, 
             'Customer 2': {'Vitamin A': 0, 
                            'Calcium': 0, 
                            'Protein': 1, 
                            'B vitamins': 1, 
                            'HPMR': 0}}
all_orders = []
for customer, order in customers.items():
    customer_orders = [(customer, vitamin, quantity) for vitamin, quantity in order.items()]
    all_orders.append(customer_orders)

all_orders = [[('Customer 2', 'Calcium', 0),
               ('Customer 2', 'Vitamin A', 0),
               ('Customer 2', 'HPMR', 0),
               ('Customer 2', 'Protein', 1),
               ('Customer 2', 'B vitamins', 1)],
              [('Customer 1', 'Calcium', 1),
               ('Customer 1', 'Vitamin A', 0),
               ('Customer 1', 'HPMR', 0),
               ('Customer 1', 'Protein', 1),
               ('Customer 1', 'B vitamins', 0)]]

答案 1 :(得分:1)

In [6]: dic={'Customer 1': {'Vitamin A': 0, 'Calcium': 1, 'Protein': 1, 'B vitamins': 0, 'HPMR': 0}, 'Customer 2': {'Vitamin A': 0, 'Calcium': 0, 'Protein': 1, 'B vitamins': 1, 'HPMR': 0}}

In [7]: [tuple([x]+list(y)) for x in dic for y in dic[x].items() ]

Out[7]: 
[('Customer 2', 'Calcium', 0),
 ('Customer 2', 'Vitamin A', 0),
 ('Customer 2', 'HPMR', 0),
 ('Customer 2', 'Protein', 1),
 ('Customer 2', 'B vitamins', 1),
 ('Customer 1', 'Calcium', 1),
 ('Customer 1', 'Vitamin A', 0),
 ('Customer 1', 'HPMR', 0),
 ('Customer 1', 'Protein', 1),
 ('Customer 1', 'B vitamins', 0)]

或使用itertools.chain()

In [8]: from itertools import chain

In [9]: [tuple(chain(*([x],y))) for x in dic for y in dic[x].items() ]
Out[9]: 
[('Customer 2', 'Calcium', 0),
 ('Customer 2', 'Vitamin A', 0),
 ('Customer 2', 'HPMR', 0),
 ('Customer 2', 'Protein', 1),
 ('Customer 2', 'B vitamins', 1),
 ('Customer 1', 'Calcium', 1),
 ('Customer 1', 'Vitamin A', 0),
 ('Customer 1', 'HPMR', 0),
 ('Customer 1', 'Protein', 1),
 ('Customer 1', 'B vitamins', 0)]

答案 2 :(得分:0)

[(k, k2, v) for k in dct for k2, v in k.items()]

答案 3 :(得分:0)

dictio = {'Customer 1': {'Vitamin A': 0, 'Calcium': 1, 'Protein': 1, 'B vitamins': 0, 'HPMR': 0}, 'Customer 2': {'Vitamin A': 0, 'Calcium': 0, 'Protein': 1, 'B vitamins': 1, 'HPMR': 0}}
a = []
for key in dictio.keys():
    for key_ in dictio[key].keys():
        a.append((key,key_,dictio[key][key_]))

print a

输出: [('Customer 2', 'Calcium', 0), ('Customer 2', 'Vitamin A', 0), ('Customer 2', 'HPMR', 0), ('Customer 2', 'Protein', 1), ('Customer 2', 'B vitamins', 1), ('Customer 1', 'Calcium', 1), ('Customer 1', 'Vitamin A', 0), ('Customer 1', 'HPMR', 0), ('Customer 1', 'Protein', 1), ('Customer 1', 'B vitamins', 0)]

你要插入的是列表中的元组,而不是列表。

答案 4 :(得分:0)

我也像其他人一样想到了列表理解:

cust = {'Customer 1': {'Vitamin A': 0, 'Calcium': 1, 'Protein': 1, 'B vitamins': 0, 'HPMR': 0}, 'Customer 2': {'Vitamin A': 0, 'Calcium': 0, 'Protein': 1, 'B vitamins': 1, 'HPMR': 0}}
cust_flat = [ (k, k2, v2) for k2, v2 in v.iteritems() for k, v in cust.iteritems() ]

结果如下:

[('Customer 2', 'Calcium', 1),
 ('Customer 1', 'Calcium', 1),
 ('Customer 2', 'Vitamin A', 0),
 ('Customer 1', 'Vitamin A', 0),
 ('Customer 2', 'HPMR', 0),
 ('Customer 1', 'HPMR', 0),
 ('Customer 2', 'Protein', 1),
 ('Customer 1', 'Protein', 1),
 ('Customer 2', 'B vitamins', 0),
 ('Customer 1', 'B vitamins', 0)]