本文实例讲述了Python实现简单的列表冒泡排序和反转列表操作。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# -*- coding:utf-8 -*- #! python2 a = [ 3 , 4 , 6 , 2 , 1 ] print a def sortAndAddNew(a): b = [] l = len (a) for i in range (l): j = i for j in range (l): if (a[i]>a[j]): a[i],a[j] = a[j],a[i] for k in range ( len (a)): b.append(a[k]) return b b = sortAndAddNew(a) print b print list ( reversed (b)) |
运行结果:
[3, 4, 6, 2, 1]
[6, 4, 3, 2, 1]
[1, 2, 3, 4, 6]
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://blog.csdn.net/mengtianwxs/article/details/55271379