0
如何將其他函數的字典用作下面的組織的輸入參數,並用字典值(除x軸之外)標記y軸?還是有更簡單的替代這個代碼呢?在直方圖上打印Y軸並使用字典功能作爲輸入
def histo(his_dict = {1:1, 2:10, 3:10, 4:6, 5:5, 6:4, 7:2, 8:1}):
x_max = max(his_dict.keys()) + 2 #get maximum value of x
y_max = max(his_dict.values()) + 2 #get minimum value of y
# print line per line
print ('^')
for j in range(y_max, 0, -1):
s = '|'
for i in range(1, x_max):
if i in his_dict.keys() and his_dict[i] >= j:
s += '*'
else:
s += ' '
print (s)
# print x axis
s = '+'
for i in range(1, x_max):
s += '---'
s += '>'
print (s)
# print indexes
s = ' '
for i in range(1, x_max):
s += ' %d ' % i
print (s)
histo()
你似乎在假設有人知道你的函數應該做什麼,「his_dict」是什麼意思,以及你的軸標籤指的是什麼。嘗試解釋更多你實際要做的事情。 – dkamins