相关疑难解决方法(0)

使用'for'循环迭代字典

我对以下代码感到有点困惑:

d = {'x': 1, 'y': 2, 'z': 3} 
for key in d:
    print key, 'corresponds to', d[key]
Run Code Online (Sandbox Code Playgroud)

我不明白的是这个key部分.Python如何识别它只需要从字典中读取密钥?keyPython中是一个特殊的词吗?或者它只是一个变量?

python dictionary python-2.7

2901
推荐指数
13
解决办法
411万
查看次数

循环遍历 json 对象数组

循环以下 json 对象的正确方法是什么?

test = [{
    'start': 'ieo5',
    'end': 'tiu9',
    'chain': 10489
}, {
    'start': 'qvc5',
    'end': 'tiu9',
    'chain': 45214
}, {
    'start': 'ieo5',
    'end': 'tiu9',
    'chain': 69296
}]
Run Code Online (Sandbox Code Playgroud)

我本质上想循环并打印出 的值start

我已经尝试了很多像这里列出的选项,但似乎无法让它工作。

这不起作用:

for x in test
    print x['start'] 
Run Code Online (Sandbox Code Playgroud)

python json python-3.x

4
推荐指数
1
解决办法
9313
查看次数

标签 统计

python ×2

dictionary ×1

json ×1

python-2.7 ×1

python-3.x ×1