def rast(t,o): return ((t[0]-o[0])**2+(t[1]-o[1])**2)**0.5,o a={0:[],1:[],2:[]} for i in open('27-8b.txt'): x,y=map(float,i.replace(',','.').split()) if x<1: a[0].append([x,y]) elif y>1.1: a[1].append([x,y]) else: a[2].append([x,y]) c=[] for i in a: d=None for s in a[i]: r=sum(rast(q,s)[0] for q in a[i]) if d==None or r