使用python中的字典中的键获取值不起作用

时间:2014-09-04 08:19:35

标签: python dictionary

我在python中有一本字典如下。

path_definition = {
       '00:00:00:00:00:01':'00:00:00:00:00:04',
       '00:00:00:00:00:02':'00:00:00:00:00:05',
       '00:00:00:00:00:03':'00:00:00:00:00:06',
};

如果我这样做

for key in path_definition.keys()
      print('value {}'.format(path_definition[key])

由于某种原因它似乎不起作用。

3 个答案:

答案 0 :(得分:2)

你错了,试试这个:

for key in path_definition.keys():  #colon is missed
  print('value {}'.format(path_definition[key])) #a parenthesys is missed

答案 1 :(得分:1)

由于您在问题中没有提到,您看到了什么错误(如果有的话),我现在看到您的代码存在两个问题:

  1. 您在:声明的末尾缺少for
  2. 您在)声明的末尾遗漏了print
  3. 解决这些问题:

    >>> for key in path_definition.keys():
    ...     print('value {}'.format(path_definition[key]))
    ...
    value 00:00:00:00:00:06
    value 00:00:00:00:00:05
    value 00:00:00:00:00:04
    

答案 2 :(得分:1)

path_definition = {
       '00:00:00:00:00:01':'00:00:00:00:00:04',
       '00:00:00:00:00:02':'00:00:00:00:00:05',
       '00:00:00:00:00:03':'00:00:00:00:00:06' #comma not required
} #Semicolon not required


for key in path_definition.keys(): #semicolon missing
      print('value {}'.format(path_definition[key])) #extra parentheses missing