Help on return type(?)
Steven D'Aprano
steve at pearwood.info
Sat Jan 9 21:23:24 EST 2016
On Sun, 10 Jan 2016 12:45 pm, Robert wrote:
> Hi,
>
> I see below code snippet. The return line is not as the usual type.
>
>
>
> def make_cov(cov_type, n_comp, n_fea):
> mincv = 0.1
> rand = np.random.random
> return {
> 'spherical': (mincv + mincv * np.dot(rand((n_components, 1)),
> np.ones((1, n_features)))) **
> 2,
> 'tied': (make_spd_matrix(n_features)
> + mincv * np.eye(n_features)),
> 'diag': (mincv + mincv * rand((n_components, n_features))) ** 2,
> 'full': np.array([(make_spd_matrix(n_features)
> + mincv * np.eye(n_features))
> for x in range(n_components)])
> }[cov_type]
>
> Specifically, could you explain the meaning of
>
> {
> ... }[cov_type]
>
> to me?
It is a dictionary lookup. { ... } sets up a dictionary with keys
'spherical'
'tied'
'diag'
'full'
then { ... }[cov_type] extracts one of the values depending on whether
cov_type is 'spherical', 'tied', 'diag', or 'full'.
--
Steven
More information about the Python-list
mailing list