import sys,os,csv
iFile=open('./file_copd.out','rU')
earth = [0,0]
##getDistance return distance given two stars
def getDistance(star1,star2):
return sqrt((star1[0]-star2[0])**2 +(star1[1]-star2[1])**2)
##diction dict_galaxy looks like this {key,distance} key is the seq assign to each star, value is a list [distance,its cordinance]
##{1,[distance1,[x,y]];2,[distance2,[x,y]]}
dict_galaxy={}
#list_galaxy=[]
count = 0
sour=iFile.readlines()
for line in sour:
star=line.split(',') ##Star is a list [x,y]
dict_galaxy[count]=[getDistance(earth,star),star]
count++
###Now sort this dictionary based on their distance, and return you a list of keys.
list_sorted_key = sorted(dict_galaxy,key=lambda x:dict_galaxy[x][0])
print 'is this what you want %s'%(list_sorted_key[:100].to_s)
iFile.close()
[在长度为n的列表中查找x个最小整数]的可能重复(http://stackoverflow.com/questions/3764355/find-the-x-smallest-integers-in-a-list-of-长度-n) – hugomg 2012-02-08 22:16:43
是的,可惜。这是一个有趣的问题,但已经在这里回答。 – 2012-02-08 22:21:24
@missingno:它有点类似,但这个问题可以很容易地通过我上面提供的解决方案来解决。这里有一些额外的计算需要,我想知道是否有办法将它们最小化。 – noMAD 2012-02-08 22:31:28