Python 如何从 dict 获取值列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16228248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get list of values from dict?
提问by Muhd
How can I get a list of the values in a dict in Python?
如何在 Python 中获取 dict 中的值列表?
In Java, getting the values of a Map as a List is as easy as doing list = map.values();. I'm wondering if there is a similarly simple way in Python to get a list of values from a dict.
在 Java 中,将 Map 的值作为 List 获取就像执行 一样简单list = map.values();。我想知道 Python 中是否有一种类似的简单方法可以从 dict 中获取值列表。
采纳答案by jamylak
Yes it's the exact same thing in Python 2:
是的,它在Python 2 中完全相同:
d.values()
In Python 3(where dict.valuesreturns a viewof the dictionary's values instead):
在Python 3 中(其中dict.values返回字典值的视图):
list(d.values())
回答by Mohan. A
Follow the below example --
按照下面的例子——
songs = [
{"title": "happy birthday", "playcount": 4},
{"title": "AC/DC", "playcount": 2},
{"title": "Billie Jean", "playcount": 6},
{"title": "Human Touch", "playcount": 3}
]
print("====================")
print(f'Songs --> {songs} \n')
title = list(map(lambda x : x['title'], songs))
print(f'Print Title --> {title}')
playcount = list(map(lambda x : x['playcount'], songs))
print(f'Print Playcount --> {playcount}')
print (f'Print Sorted playcount --> {sorted(playcount)}')
# Aliter -
print(sorted(list(map(lambda x: x['playcount'],songs))))
回答by Vlad Bezden
You can use * operatorto unpack dict_values:
您可以使用* 运算符来解压 dict_values:
>>> d = {1: "a", 2: "b"}
>>> [*d.values()]
['a', 'b']
or list object
或列表对象
>>> d = {1: "a", 2: "b"}
>>> list(d.values())
['a', 'b']
回答by Ronald Luc
There should be one ? and preferably only one ? obvious way to do it.
应该有一个?最好只有一个?显而易见的方法。
Therefore list(dictionary.values())is the one way.
所以list(dictionary.values())是一种方式。
Yet, considering Python3, what is quicker?
然而,考虑到 Python3,什么更快?
[*L]vs. [].extend(L)vs. list(L)
[*L]对比[].extend(L)对比list(L)
small_ds = {x: str(x+42) for x in range(10)}
small_df = {x: float(x+42) for x in range(10)}
print('Small Dict(str)')
%timeit [*small_ds.values()]
%timeit [].extend(small_ds.values())
%timeit list(small_ds.values())
print('Small Dict(float)')
%timeit [*small_df.values()]
%timeit [].extend(small_df.values())
%timeit list(small_df.values())
big_ds = {x: str(x+42) for x in range(1000000)}
big_df = {x: float(x+42) for x in range(1000000)}
print('Big Dict(str)')
%timeit [*big_ds.values()]
%timeit [].extend(big_ds.values())
%timeit list(big_ds.values())
print('Big Dict(float)')
%timeit [*big_df.values()]
%timeit [].extend(big_df.values())
%timeit list(big_df.values())
Small Dict(str)
256 ns ± 3.37 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
338 ns ± 0.807 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
336 ns ± 1.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Small Dict(float)
268 ns ± 0.297 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
343 ns ± 15.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
336 ns ± 0.68 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Big Dict(str)
17.5 ms ± 142 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
16.5 ms ± 338 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
16.2 ms ± 19.7 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Big Dict(float)
13.2 ms ± 41 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
13.1 ms ± 919 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
12.8 ms ± 578 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Done on Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz.
在 Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz 上完成。
# Name Version Build
ipython 7.5.0 py37h24bf2e0_0
The result
结果
- For small dictionaries
* operatoris quicker - For big dictionaries where it matters
list()is maybe slightly quicker
- 对于小字典
* operator更快 - 对于重要的大词典
list()来说可能会稍微快一点
回答by Raja jain
out: dict_values([{1:a, 2:b}])
in: str(dict.values())[14:-3]
out: 1:a, 2:b
Purely for visual purposes. Does not produce a useful product... Only useful if you want a long dictionary to print in a paragraph type form.
纯粹出于视觉目的。不会产生有用的产品...仅当您希望以段落类型形式打印长字典时才有用。

