无法理解此代码中

时间:2016-03-28 14:35:06

标签: python python-2.7

我是一个python项目的新手并且学习这门语言。他们使用的是python v2.7。

字典在代码中以不同的方式使用,我无法理解下面代码中究竟发生了什么。一位蟒蛇大师能否为我揭开神秘面纱的神秘面纱。看看我的评论。

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的分配令人困惑。但是下一行dict到results列表的行分配更令人困惑。不知道那里发生了什么。

2 个答案:

答案 0 :(得分: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)
    )

答案 1 :(得分: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)
)

查看dict的{​​{3}}:

  

如果给出了关键字参数,则将关键字参数及其值添加到从位置参数创建的字典中。

因此,这些行相当于:

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': ...}
相关问题