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



我在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])

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

您的for是错误的,请尝试以下操作:

for key in path_definition.keys():  #colon is missed
  print('value {}'.format(path_definition[key])) #a parenthesys is missed
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

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

  1. for语句末尾缺少一个:
  2. print语句的末尾缺少一个)

修复这些:

>>> 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

最新更新