Python直方图

Python直方图,python,python-3.x,histogram,Python,Python 3.x,Histogram,我试图在python中创建一个直方图,作为我的python类的一部分 应该是这样的: # fake data sumValues = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ... # calculate how long is the list and adjust the padding for 'Element' padding = max(len(sumValues), len('Element')) # now the padding for 'Value' p

我试图在python中创建一个直方图,作为我的
python
类的一部分

应该是这样的:

# fake data
sumValues = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
...
# calculate how long is the list and adjust the padding for 'Element'
padding = max(len(sumValues), len('Element'))
# now the padding for 'Value'
padding1 = max(len(str(max(sumValues))), len('Value')) 

print("\nCreating a histogram from values: ")
print("%s %10s %10s" %("Element", "Value", "Histogram"))
# use enumerate to loop your list and giving the index started from 1
for i,n in enumerate(sumValues, start=1):
    print '{0} {1}     {2}'.format( # print each line with its elements
              str(i).ljust(padding), # print with space using str.ljust
              str(i).rjust(padding1), # print with space using str.rjust
              '*'*n) # '*' * n = '*' multiply by 'n' times 

Creating a histogram from values: 
Element      Value  Histogram
1              1     *
2              2     **
3              3     ***
4              4     ****
5              5     *****
6              6     ******
7              7     *******
8              8     ********
9              9     *********
10            10     **********

然而,我无法计算出直方图。这是我目前的代码:

sumValues = [] 

print("Enter 10 integers")

for i in range( 10 ):
    newValue = int( input("Enter integer %d: " % (i + 1) ))
    sumValues.append(newValue)

print("\nCreating a histogram from values: ")
print("%s %10s %10s" %("Element", "Value", "Histogram"))
如何创建实际直方图?

一些提示: 新样式的Python格式允许:

In [1]: stars = '*' * 4    # '****'
In [2]: '{:<10s}'.format(stars)
Out[3]: '****      '
[1]中的
stars='*'*4#'**'
在[2]:“{:就像这样:

# fake data
sumValues = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
...
# calculate how long is the list and adjust the padding for 'Element'
padding = max(len(sumValues), len('Element'))
# now the padding for 'Value'
padding1 = max(len(str(max(sumValues))), len('Value')) 

print("\nCreating a histogram from values: ")
print("%s %10s %10s" %("Element", "Value", "Histogram"))
# use enumerate to loop your list and giving the index started from 1
for i,n in enumerate(sumValues, start=1):
    print '{0} {1}     {2}'.format( # print each line with its elements
              str(i).ljust(padding), # print with space using str.ljust
              str(i).rjust(padding1), # print with space using str.rjust
              '*'*n) # '*' * n = '*' multiply by 'n' times 

Creating a histogram from values: 
Element      Value  Histogram
1              1     *
2              2     **
3              3     ***
4              4     ****
5              5     *****
6              6     ******
7              7     *******
8              8     ********
9              9     *********
10            10     **********

正确地说,您显示的不是“直方图”(例如,请参阅)。但是,这不会改变有关如何打印所显示内容的答案。