使用.format打印声明问题

时间:2017-06-16 10:27:22

标签: python string.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>

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

你可以尝试:

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。 :)