Python 如何在不使用hist()函数的情况下创建值及其计数的直方图?
我想不出一种方法来打印范围格式的值的直方图。我想写一个代码来输出项目和计数(乘以“x”)。如果项目不存在,则输出将为“” 以下是我到目前为止写的内容:Python 如何在不使用hist()函数的情况下创建值及其计数的直方图?,python,python-3.x,Python,Python 3.x,我想不出一种方法来打印范围格式的值的直方图。我想写一个代码来输出项目和计数(乘以“x”)。如果项目不存在,则输出将为“” 以下是我到目前为止写的内容: item=['15', '14', '13', '12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0'] count=[1, 1, 1, 1, 3, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1] max=15 min=0 def histogram(
item=['15', '14', '13', '12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
count=[1, 1, 1, 1, 3, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1]
max=15
min=0
def histogram( count, item ):
i=0
if(max-min<=5):
for n in count:
output = ''
times = n
while( times > 0 ):
output += '*'
times = times - 1
#print(item[i],output)
print(f'{item[i]:<10}',output)
i+=1
elif(max-min>5):
#print the histogram in range of 5s
histogram( count, item )
输出(最大最小值>5)
范围
采取步骤
类似范围(开始、结束、步骤)
。您可以使用step来指示执行5
步骤,如15-10
,10-5
,5-0
item=['15','14','13','12','11','10','9','8','7','6','5','4','3','2','1','0']
计数=[1,1,1,1,3,0,0,0,0,1,0,0,0,1,0,0,0,1]
最大值=15
最小值=0
def直方图(计数,项目):
i=0
如果(最大值-最小值):
输出+='*'
次数=次数-1
#打印(项目[i],输出)
打印(f'{item[i]:清理了您的代码。'*'*x
将打印'*'
x
次
这也适用于项目数不是5的倍数的情况
item=['15', '14', '13', '12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
count=[1, 1, 1, 1, 3, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1]
maxi=15
mini=0
def histogram( count, item ):
if(maxi-mini<=5):
for i, c in zip(item, count):
output = '*'*c
print(f'{i:<10}',output)
elif(maxi-mini>5):
for index in range(0, len(item), 5):
#print the histogram in range of 5s
end_index = min(index+4, len(item)-1) # this is to handle if number of items is not divisible by 5
range_str = f'{item[end_index]}-{item[index]}'
output = '*'*sum(count[index:end_index+1])
print(range_str, output)
histogram( count, item )
如果我们先使用
item = list(reversed(item))
count = list(reversed(count))
并稍微调整一下函数:
item=['15', '14', '13', '12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
count=[1, 1, 1, 1, 3, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1]
maxi=15
mini=0
def histogram( count, item ):
if(maxi-mini<=5):
for i, c in zip(item, count):
output = '*'*c
print(f'{i:<10}',output)
elif(maxi-mini>5):
for index in range(0, len(item), 5):
#print the histogram in range of 5s
end_index = min(index+4, len(item)-1) # this is to handle if number of items is not divisible by 5
range_str = f'{item[index]}-{item[end_index]}'
output = '*'*sum(count[index:end_index+1])
print(range_str, output)
如果max
是16呢?这样做有什么具体的原因吗?值得一提的是…感谢您抽出时间,当我试图修改此代码时,出现了一个错误:“int object not callable”,因为min=0
覆盖了内置的min
函数。将min
变量重命名为mini
。
item=['15', '14', '13', '12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
count=[1, 1, 1, 1, 3, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1]
maxi=15
mini=0
def histogram( count, item ):
if(maxi-mini<=5):
for i, c in zip(item, count):
output = '*'*c
print(f'{i:<10}',output)
elif(maxi-mini>5):
for index in range(0, len(item), 5):
#print the histogram in range of 5s
end_index = min(index+4, len(item)-1) # this is to handle if number of items is not divisible by 5
range_str = f'{item[end_index]}-{item[index]}'
output = '*'*sum(count[index:end_index+1])
print(range_str, output)
histogram( count, item )
11-15 *******
6-10 *
1-5 *
0-0 *
item = list(reversed(item))
count = list(reversed(count))
item=['15', '14', '13', '12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
count=[1, 1, 1, 1, 3, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1]
maxi=15
mini=0
def histogram( count, item ):
if(maxi-mini<=5):
for i, c in zip(item, count):
output = '*'*c
print(f'{i:<10}',output)
elif(maxi-mini>5):
for index in range(0, len(item), 5):
#print the histogram in range of 5s
end_index = min(index+4, len(item)-1) # this is to handle if number of items is not divisible by 5
range_str = f'{item[index]}-{item[end_index]}'
output = '*'*sum(count[index:end_index+1])
print(range_str, output)
0-4 **
5-9 *
10-14 ******
15-15 *