2011-01-26 88 views

回答

3

我想你必须有一个dict看起来像这个,对吗?

>>> d = {1:1, 2:10, 3:10, 4:6, 5:5, 6:4, 7:2, 8:1} 
>>> d 
{1: 1, 2: 10, 3: 10, 4: 6, 5: 5, 6: 4, 7: 2, 8: 1} 

如果是这样,我有做的伎俩功能:

>>> def histo(dict_words): 
    # Get max values, plus delta to ease display 
    x_max = max(dict_words.keys()) + 2 
    y_max = max(dict_words.values()) + 2 
    # print line per line 
    print '^' 
    for j in range(y_max, 0, -1): 
     s = '|' 
     for i in range(1, x_max): 
      if i in dict_words.keys() and dict_words[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(d) 
^ 
|       
|       
| ******     
| ******     
| ******     
| ******     
| *********    
| ************    
| ***************   
| ***************   
| ******************  
|************************ 
+---------------------------> 
    1 2 3 4 5 6 7 8 9 
>>> 

好,有一个小更多的工作在左边显示价值观和正确格式化数字大于10不有但我认为这是一个好的开始:-)