按顺序执行功能

时间:2017-08-04 04:41:16

标签: python

我们说我有三个功能,a,b和c。我想按顺序执行特定参数,并打印与其执行相关的值。类似的东西:

funcDict = {
    a: "a",
    b: "b",
    c: "c"
}

for func in funcDict:
    func("test")
    print(funcDict(func))

2 个答案:

答案 0 :(得分:3)

您有更好的列表选项,主要是订单:

import matplotlib.pyplot as plt
from matplotlib.pyplot import savefig
import numpy as np
import matplotlib.gridspec as gridspec

plt.clf()
plt.cla()
plt.close()
labels_b = ["Negative",  "Positive"]
dev_sentences_b = [428, 444]
test_sentences_b = [912, 909]
train_sentences_b = [3310, 3610]

gs = gridspec.GridSpec(2, 2)
ax1= plt.subplot(gs[0, 0])
ax1.pie(train_sentences_b,  autopct='%1.1f%%',
        shadow=True, startangle=90)
ax1.axis('equal')
ax1.set_title("Train")

ax2= plt.subplot(gs[0, 1])
ax2.pie(dev_sentences_b, autopct='%1.1f%%',
        shadow=True, startangle=90)
ax2.axis('equal')
ax2.set_title("Dev")

ax3 = plt.subplot(gs[1, 1])
ax3.pie(test_sentences_b, autopct='%1.1f%%',
        shadow=True, startangle=90)
ax3.axis('equal')
ax3.set_title("Test")

ax3.legend(labels=labels_b, bbox_to_anchor=(-1,1), loc="upper left")

plt.savefig('sstbinary', format='pdf')

答案 1 :(得分:3)

如果您正在 python3.6 上运行,则会对字典进行排序,因此确实有效:

for func in funcDict:
    func("test")
    print(func(funcDict[func]))

在较旧的python版本中,我建议列出一个列表或元组列表,如this answer推荐。