2024-11-02 07:40:28
# -*- coding: utf-8 -*-
"""
:created on: 2015年9月5日
:copyright: Nokia Solutions and Networks
:author: Chuanqing Qin
:contact: chuanqing.qin@nokia.com
"""
'''
[(2.0,2.1),(2.1,2.0),(2.0,2.0),(4.0,4.0),(4.1,4.2),(4.2,4.1),(3.1,2.9)],现在要
算出这个list中的每个点与其他所有点的距离,并且比较这些距离大小,然后得出与第一个点
第一近的点,第二近的点。。与第二个点第一近的点,第二近的点。。。以此类推
'''
import math
def distance_of_two_point(x, y):
return math.sqrt(math.pow(x[0] - y[0], 2) + math.pow(x[1]- y[1], 2))
def compare(index):
def compare_item(x, y):
if distance_of_two_point(index, x) > distance_of_two_point(index, y):
return 1
else:
return -1
return compare_item
def deal_list(list_in):
for index in list_in:
temp = [items for items in list_in if items != index]
print temp
temp = sorted(temp, compare(index))
print temp
a = []
for number, item in enumerate(temp):
a.append(distance_of_two_point(index, item))
print a
list_in = [(2.0,2.1),(2.1,2.0),(2.0,2.0),(4.0,4.0),(4.1,4.2),(4.2,4.1),(3.1,2.9)]
deal_list(list_in)
if __name__ == '__main__':
pass