我刚刚入门Python,下面是我的代码,可能很简陋,如果有什么意见望告知,相互学习:
'''
给定一个年龄列表,输出以5为年龄分段的最多人数分段
'''
people_age=[12,56,78,48,56,23,36,2,45,23]#输入年龄
age_min=min(people_age)
age_max=max(people_age)
count=(age_max-age_min)//5
if count>0:
count+=1
fenduan=[0,]*count
for age in people_age:
fenduan[(age-age_min)//5]+=1
for i in range(len(fenduan)):
if(fenduan[i]==max(fenduan)):
print("%d~%d" %(age_min+i*5,age_min+i*5+5))
追问不对 ,我想要的是区间为5,但没有定具体什么区间。比如51-55 53-57,只要在5的区间内最大就行