Python:将字典转换为列表列表 [英] Python: Transform a Dictionary into a list of lists

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

问题描述

基本上,我有一个字典,希望将其转换为列表列表(每个组件列表均由字典中的键和值组成).

Basically, I have a dictionary that I want to transform into a list of lists (with each component list consisting of the key and value from the dictionary).

之所以这样做,是因为我可以使用for循环遍历这个新列表,并同时使用键和值进行操作.如果有更简单的方法可以做到这一点,我欢迎您提出建议.

The reason I am doing this is so that I can iterate through this new list with a for loop and do something with both the key and the value. If there is an easier way to do this, I am open to suggestions.

推荐答案

for key, value in my_dict.iteritems()

这将遍历字典,将每个键存储在key中,将每个值存储在value中. 请参见文档.

This will iterate through the dictionary, storing each key in key and each value in value. See the docs.

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

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