2013-06-03 49 views
0

我已經從被稱爲站數據創建的列表= []結合兩個元件蟒

打印出作爲

['france', 'england', 'germany', 'china', 'paris', 'sydney', 'gold coast', 'canberra', 'perth', 'melbourne'] 

我也有另一個函數x其中打印的打印時。

[0.0, 3043.004178666758, 21558.2996208357, 40246.748450913066, 40908.82213277263, 43786.0579097594, 67781.1426515405, 79693.11338661514, 65046.35819797423, 92804.01912347642] 

我有兩個問題。

首先)是有沒有辦法將兩者結合起來,即這樣我就可以打印,打印

[france = 0.0, england = 3043.004, germany = 21558.2999] and so on 

其次,我可以創建變量或列表或東西,使得法國持有的價值一個新的列表或功能= 0.0所以如果0.0後來產生,我可以調用法國和法國變量中的任何數據?

回答

5

使用dictzip

>>> lis1 = ['france', 'england', 'germany', 'china', 'paris', 'sydney', 'gold coast', 'canberra', 'perth', 'melbourne'] 
>>> lis2 = [0.0, 3043.004178666758, 21558.2996208357, 40246.748450913066, 40908.82213277263, 43786.0579097594, 67781.1426515405, 79693.11338661514, 65046.35819797423, 92804.01912347642] 
>>> dic = dict(zip(lis1,lis2)) 
>>> dic 
{'canberra': 79693.11338661514, 
'china': 40246.748450913066, 
'england': 3043.004178666758, 
'france': 0.0, 
'germany': 21558.2996208357, 
'gold coast': 67781.1426515405, 
'melbourne': 92804.01912347642, 
'paris': 40908.82213277263, 
'perth': 65046.35819797423, 
'sydney': 43786.0579097594} 

# Fetching values 
>>> dic['france'] 
0.0 
>>> dic['sydney'] 
43786.0579097594 

打印鍵和值:

>>> for k,v in dic.iteritems(): 
     # k points to the key, and v points to the value 
     if v < distance: 
      print k,"is less than", distance 
     else:  
      print k,"is greater than or equal to",distance 
...   
england is greater than or equal to 1000 
canberra is greater than or equal to 1000 
paris is greater than or equal to 1000 
france is less than 1000 
melbourne is greater than or equal to 1000 
germany is greater than or equal to 1000 
sydney is greater than or equal to 1000 
china is greater than or equal to 1000 
gold coast is greater than or equal to 1000 
perth is greater than or equal to 1000 
+0

我怎麼會去打印DIC作爲一個整體?有可能像打印所有值在一個 – user2430623

+0

@ user2430623使用'for'循環。 –

+0

怎麼所以我不明白在這種情況下使用循環。 – user2430623