使用 .format 打印语句问题

Print statement issue using .format

我的代码如下:

import scipy.stats as st

DISTRIBUTIONS = [ st.burr,st.cauchy,st.chi,st.chi2,st.cosine,st.dgamma]

    for distribution in DISTRIBUTIONS:
       params = distribution.fit(df['diff']) #df['diff'] is the data frame column
       print ' Generating params for distribution {0}:'.format(distribution)
       ##Do something further

在 print 语句的输出中我没有得到分布名称。相反,我得到 scipy 对象名称为:

Generating params for distribution <scipy.stats._continuous_distns.burr_gen object at 0x7f16d8b9a050>

scipy.stat 对象有一个 name 属性,改为打印:

import scipy.stats as st

DISTRIBUTIONS = [ st.burr,st.cauchy,st.chi,st.chi2,st.cosine,st.dgamma]

for distribution in DISTRIBUTIONS:
   params = distribution.fit(df['diff']) #df['diff'] is the data frame column
   print ' Generating params for distribution {0.name}:'.format(distribution)
       ##Do something further

示例:

>>> for distro in DISTRIBUTIONS: 
...     print('{0.name}'.format(distro))
... 
burr
cauchy
chi
chi2
cosine
dgamma

你可以试试:

import scipy.stats as st

DISTRIBUTIONS = {"burr": st.burr, "cauchy":st.cauchy,"chi":st.chi,"chi2":st.chi2,"cosine":st.cosine,"dgamma":st.dgamma}

for distribution in DISTRIBUTIONS:
   params = DISTRIBUTIONS[distribution].fit(df['diff']) #df['diff'] is the data frame column
   print ' Generating params for distribution {0}:'.format(distribution)
   ##Do something further

我认为这将打印具有您要查找的字符串的密钥?

编辑:另一个答案更好。我不知道 .name。 :)