UU Blog

Python冒泡排序算法

需要将json的数据,按program属性的数组大小进行排序。Python实现特别轻松,主要在交换值方面轻松,以前用Delphi代码量起码是这个两倍。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#coding:utf-8
import json

jsonraw='[{"scores":0,"program":[{"type_name":"标准间","type_id":"15","orientation_id":"19","orientation_name":"东","map_name":"格局三","start":"2017-05-09","floor_name":"东2楼","number":"2009","map_id":"16","days":"25","end":"2017-06-02","floor_id":"64","id":"104"}]},{"scores":0,"program":[{"type_name":"标准间","type_id":"15","orientation_id":"21","orientation_name":"西","map_name":"格局三","start":"2017-05-09","floor_name":"东2楼","number":"2011","map_id":"16","days":"25","end":"2017-06-02","floor_id":"64","id":"105"}]},{"scores":0,"program":[{"type_name":"普通房","type_id":"16","orientation_id":"19","orientation_name":"东","map_name":"格局二","start":"2017-05-09","floor_name":"东3楼","number":"3009","map_id":"15","days":"25","end":"2017-06-02","floor_id":"65","id":"106"}]},{"scores":0,"program":[{"type_name":"标准间","type_id":"15","orientation_id":"19","orientation_name":"东","map_name":"格局二","start":"2017-05-09","floor_name":"东5楼","number":"5005","map_id":"15","days":"25","end":"2017-06-02","floor_id":"67","id":"108"}]},{"scores":0,"program":[{"type_name":"普通房","type_id":"16","orientation_id":"22","orientation_name":"南","map_name":"格局二","start":"2017-05-09","floor_name":"东5楼","number":"5006","map_id":"15","days":"25","end":"2017-06-02","floor_id":"67","id":"109"}]},{"scores":0,"program":[{"type_name":"标准间","type_id":"15","orientation_id":"21","orientation_name":"西","map_name":"格局二","start":"2017-05-09","floor_name":"东5楼","number":"5007","map_id":"15","days":"25","end":"2017-06-02","floor_id":"67","id":"110"}]},{"scores":0,"program":[{"type_name":"标准间","type_id":"15","orientation_id":"23","orientation_name":"北","map_name":"格局三","start":"2017-05-09","floor_name":"东5楼","number":"5008","map_id":"16","days":"25","end":"2017-06-02","floor_id":"67","id":"111"}]},{"scores":0,"program":[{"type_name":"标准间","type_id":"15","orientation_id":"21","orientation_name":"西","map_name":"格局二","start":"2017-05-09","floor_name":"东5楼","number":"5009","map_id":"15","days":"25","end":"2017-06-02","floor_id":"67","id":"112"}]},{"scores":0,"program":[{"type_name":"标准间","type_id":"15","orientation_id":"19","orientation_name":"东","map_name":"格局三","start":"2017-05-09","floor_name":"东2楼","number":"2009","map_id":"16","days":"22","end":"2017-05-30","floor_id":"64","id":"104"},{"type_name":"普通房","type_id":"16","orientation_id":"19","orientation_name":"东","map_name":"格局11","start":"2017-05-31","floor_name":"东6楼","number":"6007","map_id":"14","days":"3","end":"2017-06-02","floor_id":"68","id":"114"}]},{"scores":0,"program":[{"type_name":"标准间","type_id":"15","orientation_id":"21","orientation_name":"西","map_name":"格局三","start":"2017-05-09","floor_name":"东2楼","number":"2011","map_id":"16","days":"22","end":"2017-05-30","floor_id":"64","id":"105"},{"type_name":"普通房","type_id":"16","orientation_id":"19","orientation_name":"东","map_name":"格局11","start":"2017-05-31","floor_name":"东6楼","number":"6007","map_id":"14","days":"3","end":"2017-06-02","floor_id":"68","id":"114"}]},{"scores":0,"program":[{"type_name":"普通房","type_id":"16","orientation_id":"19","orientation_name":"东","map_name":"格局二","start":"2017-05-09","floor_name":"东3楼","number":"3009","map_id":"15","days":"22","end":"2017-05-30","floor_id":"65","id":"106"},{"type_name":"普通房","type_id":"16","orientation_id":"19","orientation_name":"东","map_name":"格局11","start":"2017-05-31","floor_name":"东6楼","number":"6007","map_id":"14","days":"3","end":"2017-06-02","floor_id":"68","id":"114"}]},{"scores":0,"program":[{"type_name":"标准间","type_id":"15","orientation_id":"19","orientation_name":"东","map_name":"格局二","start":"2017-05-09","floor_name":"东5楼","number":"5005","map_id":"15","days":"22","end":"2017-05-30","floor_id":"67","id":"108"},{"type_name":"普通房","type_id":"16","orientation_id":"19","orientation_name":"东","map_name":"格局11","start":"2017-05-31","floor_name":"东6楼","number":"6007","map_id":"14","days":"3","end":"2017-06-02","floor_id":"68","id":"114"}]},{"scores":0,"program":[{"type_name":"普通房","type_id":"16","orientation_id":"22","orientation_name":"南","map_name":"格局二","start":"2017-05-09","floor_name":"东5楼","number":"5006","map_id":"15","days":"22","end":"2017-05-30","floor_id":"67","id":"109"},{"type_name":"普通房","type_id":"16","orientation_id":"19","orientation_name":"东","map_name":"格局11","start":"2017-05-31","floor_name":"东6楼","number":"6007","map_id":"14","days":"3","end":"2017-06-02","floor_id":"68","id":"114"}]},{"scores":0,"program":[{"type_name":"标准间","type_id":"15","orientation_id":"21","orientation_name":"西","map_name":"格局二","start":"2017-05-09","floor_name":"东5楼","number":"5007","map_id":"15","days":"22","end":"2017-05-30","floor_id":"67","id":"110"},{"type_name":"普通房","type_id":"16","orientation_id":"19","orientation_name":"东","map_name":"格局11","start":"2017-05-31","floor_name":"东6楼","number":"6007","map_id":"14","days":"3","end":"2017-06-02","floor_id":"68","id":"114"}]},{"scores":0,"program":[{"type_name":"标准间","type_id":"15","orientation_id":"23","orientation_name":"北","map_name":"格局三","start":"2017-05-09","floor_name":"东5楼","number":"5008","map_id":"16","days":"22","end":"2017-05-30","floor_id":"67","id":"111"},{"type_name":"普通房","type_id":"16","orientation_id":"19","orientation_name":"东","map_name":"格局11","start":"2017-05-31","floor_name":"东6楼","number":"6007","map_id":"14","days":"3","end":"2017-06-02","floor_id":"68","id":"114"}]},{"scores":0,"program":[{"type_name":"标准间","type_id":"15","orientation_id":"21","orientation_name":"西","map_name":"格局二","start":"2017-05-09","floor_name":"东5楼","number":"5009","map_id":"15","days":"22","end":"2017-05-30","floor_id":"67","id":"112"},{"type_name":"普通房","type_id":"16","orientation_id":"19","orientation_name":"东","map_name":"格局11","start":"2017-05-31","floor_name":"东6楼","number":"6007","map_id":"14","days":"3","end":"2017-06-02","floor_id":"68","id":"114"}]}]'

jsonarr=json.loads(jsonraw)

arrlen = len(jsonarr)

for i in range(0,arrlen):
for j in range(i+1,arrlen):
if len(jsonarr[i]['program']) > len(jsonarr[j]['program']):
jsonarr[i],jsonarr[j] = jsonarr[j], jsonarr[i]

print jsonarr
给作者打一针鸡血