2014-09-04 64 views
-1

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

它似乎並沒有出於某種原因。

+0

是不是因爲你的兩個語法錯誤? – 2014-09-04 08:21:23

+1

您使用的是哪一個版本的Python,添加':'和')',代碼適用於我。 – fredtantini 2014-09-04 08:21:46

回答

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 
2

您的是錯的,試試這個:

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

因爲你沒有在你的問題中提到,您所看到的錯誤(如果有的話),我看到兩個問題與您的代碼現在:

  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