首页 新闻 会员 周边

python 如何实现某个元素上下移动

0
[已解决问题] 解决于 2017-01-07 16:32

正文


LiZR07的主页 LiZR07 | 菜鸟二级 | 园豆:208
提问于:2016-12-22 19:42
< >
分享
最佳答案
0
# -*- coding: utf-8 -*-
"""
:created on: 2016年12月23日
 
:copyright: 湖北省孝感市
:author:画船听雨眠
:contact:ceet@vip.qq.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
        print
        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

 

奖励园豆:5
雨落秋垣 | 初学一级 |园豆:137 | 2016-12-23 13:17
其他回答(1)
0

在画布中


LiZR07 | 园豆:208 (菜鸟二级) | 2016-12-22 19:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册