points = [2,5,10,22,26,32,42,54,66]
centroids = [0,16,30]
for i in range(3):
#initialize clusters
c1 = []
c2 = []
c3 = []
#find midpoints
mid_one = (centroids[0]+centroids[1])/2
mid_two = (centroids[1]+centroids[2])/2
#categorize points
for i in range(len(points)):
if (points[i]<mid_one):
c1.append(points[i])
elif (points[i]<mid_two):
c2.append(points[i])
else:
c3.append(points[i])
print (f'cluster 1: {c1} \
cluster 2: {c2} \
cluster 3: {c3}')
#find averages of clusters
c1_sum = 0
for i in range(len(c1)):
c1_sum+=c1[i]
c2_sum = 0
for i in range(len(c2)):
c2_sum+=c2[i]
c3_sum = 0
for i in range(len(c3)):
c3_sum+=c3[i]
c1_avg = round(c1_sum/len(c1),2)
c2_avg = round(c2_sum/len(c2),2)
c3_avg = round(c3_sum/len(c3),2)
print (f'cluster 1 avg: {c1_avg} \
cluster 2 avg: {c2_avg} \
cluster 3 avg: {c3_avg} \n')
centroids = [c1_avg,c2_avg,c3_avg]