将词典列表转换为列表列表 [英] Turning a list of dictionaries into a list of lists

查看:72
本文介绍了将词典列表转换为列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道列表理解是可能的,但是我似乎无法弄清楚.目前,我有一个像这样的词典列表:

I know this is possible with list comprehension but I can't seem to figure it out. Currently I have a list of dictionaries like so:

 [ {'field1': 'a', 'field2': 'b'},
   {'field1': 'c', 'field2': 'd'},
   {'field1': 'e', 'field2': 'f'} ]

我正在尝试将其转换为:

I'm trying to turn this into:

list = [
    ['b', 'a'],
    ['d', 'c'],
    ['f', 'e'],
]

推荐答案

您可以尝试:

[[x['field2'], x['field1']] for x in l]

其中l是您的输入列表.您的数据结果将是:

where l is your input list. The result for your data would be:

[['b', 'a'], ['d', 'c'], ['f', 'e']]

这样,您可以确保field2的值早于field1

This way you ensure that the value for field2 comes before the value for field1

这篇关于将词典列表转换为列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆