如何将字典集转换为字典列表?

时间:2019-09-17 02:29:44

标签: python-3.x

我正在尝试将各个单独的词典转换为词典列表

这是我当前的字典:

order = {'company_name': 'FRIENDS',  'delivery_timeslot': '18-21', 'latitude': '1.37315509207642000000', 'longitude': '103.76570152476201000000', 'status': 'pending', 'date': '2019-12-06', 'region': 'WEST'}
{'company_name': 'FLOWERS', 'delivery_timeslot': '18-21', 'latitude': '1.28821802835873000000', 'longitude': '103.84569230314800000000', 'status': 'pending', 'date': '2019-11-29', 'region': 'CENTRAL'}
{'company_name': 'SUNSHINE',  'delivery_timeslot': '18-21', 'latitude': '1.37414901860683000000', 'longitude': '103.94353973518100000000', 'status': 'pending', 'date': '2019-11-08', 'region': 'EAST'}
{'company_name': 'CLOUDS', 'delivery_timeslot': '18-21', 'latitude': '1.39663901543996000000', 'longitude': '103.81937713764700000000', 'status': 'pending', 'date': '2019-10-25', 'region': 'NORTH'}
{'company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH'}

这是我预期的输出:

order = [{'company_name': 'FRIENDS',  'delivery_timeslot': '18-21', 'latitude': '1.37315509207642000000', 'longitude': '103.76570152476201000000', 'status': 'pending', 'date': '2019-12-06', 'region': 'WEST'},
{'company_name': 'FLOWERS', 'delivery_timeslot': '18-21', 'latitude': '1.28821802835873000000', 'longitude': '103.84569230314800000000', 'status': 'pending', 'date': '2019-11-29', 'region': 'CENTRAL'},
{'company_name': 'SUNSHINE',  'delivery_timeslot': '18-21', 'latitude': '1.37414901860683000000', 'longitude': '103.94353973518100000000', 'status': 'pending', 'date': '2019-11-08', 'region': 'EAST'},
{'company_name': 'CLOUDS', 'delivery_timeslot': '18-21', 'latitude': '1.39663901543996000000', 'longitude': '103.81937713764700000000', 'status': 'pending', 'date': '2019-10-25', 'region': 'NORTH'},
{'company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH'}]

我尝试过:

neworder = list(order.items())

但这只会返回一个数据集

[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]

然后我尝试了一个循环:

for i in order:
    neworder = list(order.items()) 
    print(neworder)

但它仍然仅返回1个数据集和多个列表

[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]
[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]
[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]
[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]
[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]
[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]
[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]
[('company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH')]

1 个答案:

答案 0 :(得分:1)

从哪里获得字典集,如果要在这样的代码中初始化订单,那么您只分配了第一个字典来订购其他商品,而没有分配给任何变量。您应该将字典包装在“ []”中,并添加逗号,如下所示:

order = [
    {'company_name': 'FRIENDS',  'delivery_timeslot': '18-21', 'latitude': '1.37315509207642000000', 'longitude': '103.76570152476201000000', 'status': 'pending', 'date': '2019-12-06', 'region': 'WEST'}
    {'company_name': 'FLOWERS', 'delivery_timeslot': '18-21', 'latitude': '1.28821802835873000000', 'longitude': '103.84569230314800000000', 'status': 'pending', 'date': '2019-11-29', 'region': 'CENTRAL'},
    {'company_name': 'SUNSHINE',  'delivery_timeslot': '18-21', 'latitude': '1.37414901860683000000', 'longitude': '103.94353973518100000000', 'status': 'pending', 'date': '2019-11-08', 'region': 'EAST'},
    {'company_name': 'CLOUDS', 'delivery_timeslot': '18-21', 'latitude': '1.39663901543996000000', 'longitude': '103.81937713764700000000', 'status': 'pending', 'date': '2019-10-25', 'region': 'NORTH'},
    {'company_name': 'SUN',  'delivery_timeslot': '10-13', 'latitude': '1.23123112888888800000', 'longitude': '1.00000000000008100000', 'status': 'pending', 'date': '2019-10-18', 'region': 'NORTH'}
]