1
我在字典中具有特定的值,可以從高到低排序,但可以有多達一千或更多的值。如果一個數字在1-10之間,並且在圖表中輸出一個圖表的前1-10個最高值,那麼如何處理用戶輸入。所以,如果他們輸入3,將繪製在先進如何根據用戶輸入將從字典中的值從最高值降到最低值
from collections import Counter
from scipy import *
from matplotlib.pyplot import *
import matplotlib.pyplot as plot
frequency1 = Counter({'1':100,'2':400,'3':200,'4':300,})
response1 = input("How many top domains from source? Enter a number between 1-10: ")
if response1 == "1":
if len(frequency1) >= 1:
print("\nTop 1 most is:")
for key, frequency1_value in frequency1.most_common(1):
print("\nNumber:",key,"with frequency:",frequency1_value)
########Graph for this output
x = [1,2]
y = [frequency1_value,0]
figure(1)
ax = plot.subplot(111)
ax.bar(x,y,align='center', width=0.2, color = 'm')
ax.set_xticklabels(['0', '1'])
xlabel("This graph shows amount of protocols used")
ylabel("Number of times used")
grid('on')
################################## END GRAPH
else:
print("\nThere are not enough domains for this top amount.")
if response1 == "2":
if len(frequency1) >= 2:
print("\nTop 2 most is:")
for key, frequency1_value in frequency1.most_common(2):
print("\nNumber:",key,"with frequency:",frequency1_value)
########Graph for this output
x = [1,2,3]
y = [frequency1_value,frequency1_value,0]
figure(1)
ax = plot.subplot(111)
ax.bar(x,y,align='center', width=0.2, color = 'm')
ax.set_xticklabels(['0', '1','','2'])
xlabel("This graph shows amount of protocols used")
ylabel("Number of times used")
grid('on')
################################## END GRAPH
else:
print("\nThere are not enough domains for this top amount.")
感謝您的響應!這是否適用於python 3.5?我得到一個錯誤在第19行str()> int()。 – k5man001
其實我得到它,輸入需要被轉換爲int ..感謝您的幫助! – k5man001