【发布时间】:2018-07-12 15:11:26
【问题描述】:
在 python 中,我需要以这种方式汇总 count_list 中的数据(如直方图):
"""
number | occurence
0 | *
1 | **
2 | ***
3 | **
4 | **
5 | *
6 | *
7 | **
8 | ***
9 | *
10 | **
"""
但是我得到了这个错误的输出:
"""
number | occurence
0 |
1 | **
2 |
3 |
4 |
5 |
6 | **
7 |
8 |
9 |
10 | **
"""
这是我的代码:
import random
random_list = []
list_length = 20
while len(random_list) < list_length:
random_list.append(random.randint(0,10))
count_list = [0] * 11
index = 0
while index < len(random_list):
number = random_list[index]
count_list[number] = count_list[number] + 1
index = index + 1
def summerizer():
index = 0
print count_list
print '"'*3
print 'number | occurrence'
while index < len(count_list):
print '%s' %' '*(7),
print index,#the problem is here
print ' | ',#and here
print '%s' %'*'*(count_list[index])
index += 1
print '%s'%'"'*3
summerizer()
【问题讨论】:
-
你得到的输出是什么?请尽可能地提供更多的资料!很有帮助
-
我已经编辑了问题,添加了我得到的错误答案....附图
-
错误是数字前缺少空格?
-
你为什么选择
print '%s'%'"'*3而不仅仅是print '"""'? -
我认为您的输出没有问题。计数似乎匹配。你知道你在桌子前打印
count_list,而不是random_list,对吧?