本文实例讲述了Python简单处理坐标排序问题。分享给大家供大家参考,具体如下:
前面一篇学了列表的冒泡排序算法,这一节利用此算法处理一下坐标排序的问题
我实现的功能是从上到下,从左到右的排序坐标点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# -*- coding:utf-8 -*-
#! python2
a = [( 4 , 5 ),( 2 , 4 ),( 3 , 4 ),( 1 , 1 ),( 2 , 5 )]
print a
def zuobiaopaixu(a):
b = []
l = len (a)
for i in range (l):
j = i
for j in range (l):
if (a[i][ 0 ]<a[j][ 0 ]):
a[i],a[j] = a[j],a[i]
if (a[i][ 1 ]>a[j][ 1 ]):
a[i],a[j] = a[j],a[i]
for k in range ( len (a)):
b.append(a[k])
return b
b = zuobiaopaixu(a)
print b
|
运行结果:
[(4, 5), (2, 4), (3, 4), (1, 1), (2, 5)]
[(2, 5), (4, 5), (2, 4), (3, 4), (1, 1)]
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://blog.csdn.net/mengtianwxs/article/details/55271636