相关疑难解决方法(0)

在Python3中按索引访问dict_keys元素

我正在尝试通过索引访问dict_key的元素:

test = {'foo': 'bar', 'hello': 'world'}
keys = test.keys()  # dict_keys object

keys.index(0)
AttributeError: 'dict_keys' object has no attribute 'index'
Run Code Online (Sandbox Code Playgroud)

我想得到foo.

同样的:

keys[0]
TypeError: 'dict_keys' object does not support indexing
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

python dictionary key python-3.x

107
推荐指数
4
解决办法
10万
查看次数

标签 统计

dictionary ×1

key ×1

python ×1

python-3.x ×1