如何从Python中的字典提取所有值? [英] How can I extract all values from a dictionary in Python?
本文介绍了如何从Python中的字典提取所有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字典 d = {1:-0.3246,2:-0.9185,3:-3985,...}
。
如何将 d
的所有值提取到列表 l
?
How do I extract all of the values of d
into a list l
?
推荐答案
如果您只需要字典键 1
, 2
和 3
use: your_dict.keys()
。
If you only need the dictionary keys 1
, 2
, and 3
use: your_dict.keys()
.
如果您只需要字典值 -0.3246
, -0.9185
,和 -3985
使用: your_dict.values()
。
If you only need the dictionary values -0.3246
, -0.9185
, and -3985
use: your_dict.values()
.
如果您希望键和值都使用: your_dict.items()
返回一个元组列表 [(key1,value1),( key2,value2),...]
。
If you want both keys and values use: your_dict.items()
which returns a list of tuples [(key1, value1), (key2, value2), ...]
.
这篇关于如何从Python中的字典提取所有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文