在python中使用for函数中的值时将代码与for函数分离
很抱歉让人困惑的标题:s在python中使用for函数中的值时将代码与for函数分离,python,for-loop,Python,For Loop,很抱歉让人困惑的标题:s for x in frequency: alphab = [x] frequencies = [frequency[x]] print alphab, frequencies 我如何才能将下面的代码与上面的代码分开,同时仍使用的输出来表示x的频率:如果我运行此处的内容,则会为x的每个值而不是整个字符串打开直方图。 如果我缩进下面的任何内容,如图所示,直方图也只针对x的第一个值运行。 是否有任何可能的方法可以使用整个字符串
for x in frequency:
alphab = [x]
frequencies = [frequency[x]]
print alphab, frequencies
我如何才能将下面的代码与上面的代码分开,同时仍使用的输出来表示x的频率:
如果我运行此处的内容,则会为x的每个值而不是整个字符串打开直方图。
如果我缩进下面的任何内容,如图所示,直方图也只针对x的第一个值运行。
是否有任何可能的方法可以使用整个字符串,而不必在中缩进for
pos = np.arange(len(alphab))
width = 1.0
ax = plt.axes()
ax.set_xticks(pos + (width / 2))
ax.set_xticklabels(alphab)
plt.xlabel('Letter')
plt.ylabel('Absolute Frequency')
plt.title("Absolute Frequency of letters in text")
plt.bar(pos, frequencies, width, color='r')
plt.show()
这是您一直在寻找的吗?我想您应该在调用plot函数之前填充数组,例如
alphab = []
frequencies = []
for x in frequency:
alphab.append(x)
frequencies.append(frequency[x])
# .. some more code here ..
plt.bar(pos, frequencies, width, color='r')
我不认为有人真的得到了你想要的东西…它不是已经分开了吗?你必须更精确地解释你想做什么。哪一个是你的整个字符串,直方图在哪里?对不起,太难理解了,标签也错了。(我正在纠正它)
alphab = []
frequencies = []
for x in frequency:
alphab.append(x)
frequencies.append(frequency[x])
# .. some more code here ..
plt.bar(pos, frequencies, width, color='r')