Python 如何创建字典值列表?
我如何列出字典的值?例如,如果我有Python 如何创建字典值列表?,python,dictionary,Python,Dictionary,我如何列出字典的值?例如,如果我有 {1:Apple,2:Red,3:Purple,4:Green} 我将如何收到 ['Apple','Red','Purple','Green'] 在python上鬼混了一个小时后,我似乎仍然无法理解它。谢谢你的帮助。去查字典 在Python3中,这将返回一个,因此对其调用list() Python 2: >>> d = {1:'Apple', 2:'Red', 3:'Purple', 4:'Green'} >>> d.v
{1:Apple,2:Red,3:Purple,4:Green}
我将如何收到
['Apple','Red','Purple','Green']
在python上鬼混了一个小时后,我似乎仍然无法理解它。谢谢你的帮助。去查字典
在Python3中,这将返回一个,因此对其调用list()
Python 2:
>>> d = {1:'Apple', 2:'Red', 3:'Purple', 4:'Green'}
>>> d.values()
['Apple', 'Red', 'Purple', 'Green']
Python 3:
>>> d = {1:'Apple', 2:'Red', 3:'Purple', 4:'Green'}
>>> d.values()
dict_values(['Apple', 'Red', 'Purple', 'Green'])
>>> list(d.values())
['Apple', 'Red', 'Purple', 'Green']
很难从OP中分辨出来,但我的印象是可能需要更像
[d[k]的东西来表示k在排序(d)]
。@DSM:不可能分辨;如果OP希望字典具有排序功能,该怎么办?就目前情况而言,OP要求从字典中获取一个值列表,这就是我们所知道的。我应该澄清我的道歉。我知道如何排序,只是需要弄清楚如何接收我的价值观列表。