如何从dict获取值列表?

Muh*_*uhd 254 python dictionary list

如何在Python中获取dict中的值列表?

在Java中,将Map的值作为List获取就像操作一样简单list = map.values();.我想知道在Python中是否有类似的简单方法来获取dict中的值列表.

jam*_*lak 381

是的,它在Python 2中完全相同:

d.values()
Run Code Online (Sandbox Code Playgroud)

Python 3中(其中dict.values返回字典值的视图):

list(d.values())
Run Code Online (Sandbox Code Playgroud)

  • 或者,对于python2.x和3.x都可以使用`[d [k] for k in d]`(*请注意,我实际上并不建议您使用此*).通常你实际上并不需要*一个值列表,所以`d.values()`就好了. (12认同)
  • @Muhd Python文档总是包含所有内容:http://docs.python.org/2/library/stdtypes.html (3认同)
  • 稍微"更好"的链接(到您发布的页面上的特定位置):http://docs.python.org/2/library/stdtypes.html#dict.values (2认同)
  • @Peterino是的,尽管在 python 3 中,您很少需要显式调用“iter(d.values())”。您可以简单地迭代这些值: `for value in d.values(): ` 顺便说一句,这就是每个人在大多数实际用例中可能会做的事情。通常,您不需要仅仅为了拥有像这个问题一样的列表而需要字典值列表。我所做的许多 Python 2 评论现在几乎毫无用处,并且可能会让现在阅读本文的 Python 3 程序员感到困惑 (2认同)

小智 13

按照下面的例子——

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))))
Run Code Online (Sandbox Code Playgroud)


Ron*_*Luc 13

应该有一个?最好只有一个?显而易见的方法。

因此list(dictionary.values())一种方法

但是,考虑使用Python3,哪种方法更快?

[*L]vs. [].extend(L)vs.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())
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)

在Intel®CoreTM i7-8650U CPU @ 1.90GHz上完成。

# Name                    Version                   Build
ipython                   7.5.0            py37h24bf2e0_0
Run Code Online (Sandbox Code Playgroud)

结果

  1. 对于小词典* operator更快
  2. 对于重要的大型词典,list()可能会更快

  • 感谢您分享基准。所以只是一个小更新:在带有 Python 3.10 的 Apple Silicon 上和对于大字典来说,没有区别(更多)。 (4认同)
  • `list(L)`,因为“应该有一种——最好只有一种——明显的方法来做到这一点。” (2认同)

Vla*_*den 12

您可以使用*运算符解压缩dict_values:

>>> d = {1: "a", 2: "b"}
>>> [*d.values()]
['a', 'b']
Run Code Online (Sandbox Code Playgroud)

或列出对象

>>> d = {1: "a", 2: "b"}
>>> list(d.values())
['a', 'b']
Run Code Online (Sandbox Code Playgroud)

  • 哪一个更快? (8认同)