相关疑难解决方法(0)

如何在Python中将字典键作为列表返回?

Python 2.7中,我可以将字典,项目作为列表获取:

>>> newdict = {1:0, 2:0, 3:0}
>>> newdict.keys()
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)

现在,在Python> = 3.3中,我得到这样的东西:

>>> newdict.keys()
dict_keys([1, 2, 3])
Run Code Online (Sandbox Code Playgroud)

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

newlist = list()
for i in newdict.keys():
    newlist.append(i)
Run Code Online (Sandbox Code Playgroud)

我想知道,有没有更好的方法来返回Python 3中的列表?

python dictionary list python-2.x python-3.x

656
推荐指数
10
解决办法
101万
查看次数

标签 统计

dictionary ×1

list ×1

python ×1

python-2.x ×1

python-3.x ×1