如何将字典键作为Python中的列表返回(How to return dictionary keys as a list in Python?)

Python> = 3.3相比,我注意到Python 2.7和旧版本的Python 3有所不同。


以前在Python 2.7中 ,我可以得到字典项目作为列表:

>>> newdict = {1:0, 2:0, 3:0} >>> newdict.keys() [1, 2, 3]

现在在Python> = 3.3我得到的东西:

>>> newdict.keys() dict_keys([1, 2, 3])

所以我必须这样做得到一个列表:

newlist = list() for i in newdict.keys(): newlist.append(i)

我想知道是否有更好的方式在Python 3中返回列表?

In Python 2.7, I could get dictionary keys, values, or items as a list:

>>> newdict = {1:0, 2:0, 3:0} >>> newdict.keys() [1, 2, 3]

Now, in Python >= 3.3, I get something like this:

>>> newdict.keys() dict_keys([1, 2, 3])

So, I have to do this to get a list:

newlist = list() for i in newdict.keys(): newlist.append(i)

I'm wondering, is there a better way to return a list in Python 3?

最满意答案

尝试list(newdict.keys()) 。

这将将dict_keys对象转换为列表。

另一方面,你应该问自己是否重要。 Pythonic的代码是假设鸭子打字( 如果它看起来像一只鸭子,它像一只鸭子,它是一只鸭子 )。 dict_keys对象将作为大多数用途的列表。 例如:

for key in newdict.keys(): print(key)

显然,插入运算符可能不起作用,但是对于字典键列表来说,这并不太有意义。

Try list(newdict.keys()).

This will convert the dict_keys object to a list.

On the other hand, you should ask yourself whether or not it matters. The Pythonic way to code is to assume duck typing (if it looks like a duck and it quacks like a duck, it's a duck). The dict_keys object will act like a list for most purposes. For instance:

for key in newdict.keys(): print(key)

Obviously, insertion operators may not work, but that doesn't make much sense for a list of dictionary keys anyway.

如何将字典键作为Python中的列表返回(How to return dictionary keys as a list in Python?)

Python> = 3.3相比,我注意到Python 2.7和旧版本的Python 3有所不同。


以前在Python 2.7中 ,我可以得到字典项目作为列表:

>>> newdict = {1:0, 2:0, 3:0} >>> newdict.keys() [1, 2, 3]

现在在Python> = 3.3我得到的东西:

>>> newdict.keys() dict_keys([1, 2, 3])

所以我必须这样做得到一个列表:

newlist = list() for i in newdict.keys(): newlist.append(i)

我想知道是否有更好的方式在Python 3中返回列表?

In Python 2.7, I could get dictionary keys, values, or items as a list:

>>> newdict = {1:0, 2:0, 3:0} >>> newdict.keys() [1, 2, 3]

Now, in Python >= 3.3, I get something like this:

>>> newdict.keys() dict_keys([1, 2, 3])

So, I have to do this to get a list:

newlist = list() for i in newdict.keys(): newlist.append(i)

I'm wondering, is there a better way to return a list in Python 3?

最满意答案

尝试list(newdict.keys()) 。

这将将dict_keys对象转换为列表。

另一方面,你应该问自己是否重要。 Pythonic的代码是假设鸭子打字( 如果它看起来像一只鸭子,它像一只鸭子,它是一只鸭子 )。 dict_keys对象将作为大多数用途的列表。 例如:

for key in newdict.keys(): print(key)

显然,插入运算符可能不起作用,但是对于字典键列表来说,这并不太有意义。

Try list(newdict.keys()).

This will convert the dict_keys object to a list.

On the other hand, you should ask yourself whether or not it matters. The Pythonic way to code is to assume duck typing (if it looks like a duck and it quacks like a duck, it's a duck). The dict_keys object will act like a list for most purposes. For instance:

for key in newdict.keys(): print(key)

Obviously, insertion operators may not work, but that doesn't make much sense for a list of dictionary keys anyway.