2016-03-28 87 views
1

我是一名Python項目和學習語言的新手。他們正在使用python v2.7。無法理解此代碼中Python字典的不同用法

字典在代碼中以不同的方式使用,我無法理解下面代碼中究竟發生了什麼。一位蟒蛇大師能爲我揭開下面的事情嗎?看看我的評論。

key_mapping={} # is being passed into the below procedure as an argument 

for key, result in cache_results.iteritems(): # loop over key value pair a normal dict 
     client_key = self._client_key_from_result(result, oid_hoid_map, is_trade=is_trade) 
     if not client_key: 
      continue 
    result_key = (# I guess this is a tupple? 
     int(result.osKey) if is_trade else result.portfolioID, 
     self._force_int(result.collateralGroupID), 
     self._force_int(result.bookingBU) 
    ) 

    key_mapping[key]=client_key, result_key # What the? huh? 
    results[client_key, result_key] = dict(# What in the world? 
     col_amts=(col_amt_ph, col_amt), 
     exps=(max_exp, max_exp_yr1, sum_exp) + padded_exposures, 
     additional_columns=(col_alpha, col_beta, col_isFinancial, col_financial_factor, 
          col_pd, col_lgd, col_effective_maturity, col_k, col_ead) 
    ) 

轉讓給key_mapping令人困惑。但是,下一行字典賦予results列表更令人困惑。不知道那裏發生了什麼。

回答

1

通過,你有問題(括號加元組周圍可能會有點幫助)的部分場地狀況:

# This is assigning a tuple of (client_key, result_key) as the value of key in key_mapping 
# Which actually becomes (client_key, 
#       (int(result.osKey) if is_trade else result.portfolioID, 
#       self._force_int(result.collateralGroupID), 
#       self._force_int(result.bookingBU))) 
# Which is in turn 
# (
#  self._client_key_from_result(result, oid_hoid_map, is_trade=is_trade), 
#  (int(result.osKey) if is_trade else result.portfolioID, 
#  self._force_int(result.collateralGroupID), 
#  self._force_int(result.bookingBU))) 
#) 
# Dictionary keys can be tuples: 
# d = {(1, 2): 3, (3, 4): 4} 
# d[(1, 2)] 
# >>> 3 
# d[(3, 4)] 
# >>> 4 
# In your case it's a slightly more complex tuple mapped to some dictionary 
# d = {(1, (2, 3)): {'a': 'b'}, 
#  (2, (3, 4)): {'a': 'b'}, # values can be the same 
#  (1, (2, 1)): {'a': 'c'}} 
# d[(1, (2, 3))] 
# >>> {'a': 'b'} 
key_mapping[key] = (client_key, result_key) # What the? huh? 
    # This is assigning a new dictionary to the value of a tuple (client_key, result_key) 
    # Notice how the keys in results must be all tuples 
    # The dict definition below could also be written as 
    # {'col_amts': (col_amt_ph, col_amt), 'exps': (max_exp, max_exp_yr1, sum_exp) + padded_exposures, etc.} 
    results[(client_key, result_key)] = dict(# What in the world? 
     col_amts=(col_amt_ph, col_amt), 
     exps=(max_exp, max_exp_yr1, sum_exp) + padded_exposures, 
     additional_columns=(col_alpha, col_beta, col_isFinancial, col_financial_factor, 
          col_pd, col_lgd, col_effective_maturity, col_k, col_ead) 
    ) 
+0

謝謝。那麼可以肯定地說client_key是'col_amts'而result_key是'(col_amt_ph,col_amt)'? – Doublespeed

+0

否@Doublespeed,***合併***,這兩個鍵形成一個新鍵,可用於查找包含「col_amts,exps等」的字典。請參閱我的編輯 – Bahrom

1
result_key = (# I guess this is a tupple? 
    int(result.osKey) if is_trade else result.portfolioID, 
    self._force_int(result.collateralGroupID), 
    self._force_int(result.bookingBU) 
) 

你是正確的。這是一個元組。如果你把它放在一行中,它會更明顯,但由於它在括號內,所以Python允許程序員將它分成多行。

key_mapping[key]=client_key, result_key # What the? huh? 

client_key, result_key仍然是一個元組。只需將x = 4, 5; print x放入Python shell中即可看到。在大多數情況下,元組實例化中的括號並不是必須的。如果他們在那裏,這會讓事情變得更加明顯。

results[client_key, result_key] = dict(# What in the world? 
    col_amts=(col_amt_ph, col_amt), 
    exps=(max_exp, max_exp_yr1, sum_exp) + padded_exposures, 
    additional_columns=(col_alpha, col_beta, col_isFinancial, col_financial_factor, 
         col_pd, col_lgd, col_effective_maturity, col_k, col_ead) 
) 

看看在docsdict

如果關鍵字給出參數,關鍵字參數和它們的值被添加到從位置參數創建的字典。

因此,這些行等效於:

results[client_key, result_key] = {'col_amts': (col_amt_ph, col_amt), 'exps': ...} 

正如client_key, result_key是一個元組時key_mapping[key]分配給它們,它們在這種情況下也是一個元組。您的results字典可能看起來像這樣:

{('someclientkey', 'someresultkey'): {'col_amts': ...}, ('someotherclientkey', 'someotherresultkey'): {'col_amts': ...} 
+0

謝謝。在你的解釋中,什麼是'col_amts'?是client_key還是results_key? – Doublespeed

+0

關於這個賦值:'results [client_key,result_key] = {'col_amts':(col_amt_ph,col_amt),'exps':...}',是否'col_amts'等於client_key?並且值'(col_amt_ph,col_amt)'將取代result_key? – Doublespeed

+0

與我在其他地方提到的一樣:'client_key,result_key'只是一個沒有括號的元組。它與result [(client_key,result_key)] = ...一樣('client_key,result_key)'是關鍵字,'{'col_amts':...}'是值。我編輯過的更清楚。 – zondo