list(dictionary.values()) 与dictionary.values()

Est*_*net 0 python dictionary list

我想返回给定字典的值列表dict。与 有何list(dict.values())不同dict.values()

为什么dict.values不可迭代?

Ale*_*lex 8

不同之处在于,在 Python3 中,values() 返回一个view。代表着:

  • 这不是一个列表

  • 它的内容会跟踪字典的更改,而list会创建一个列表并且不会跟踪更改。

>>> d = { "one" : 1, "two" : 2, "three" : 3} 
>>> l = list(d.values()) 
>>> v = d.values() 
>>> d["one"] = 5 
>>> l 
[1, 2, 3] 
>>> v
dict_values([5, 2, 3])
Run Code Online (Sandbox Code Playgroud)