python对列表去重例子
#!/usr/bin/env python arr_num1 = [1,2,3,4,2,12,3,14,3,2,12,3,14,3,21,2,2,3,4111,22,3333,4] arr_num2 = [2,1,3,2,43,234,454,452,234,14,21,14] num_list = [] for i in arr_num1: if i in arr_num2 and i not in num_list: num_list.append(i) print num_list
冒泡排序例子
#!/usr/bin/env python arr_num = [1,2,3,4,2,12,3,14,3,2,12,3,14,3,21,2,2,3,4111,22,3333,4] for i in xrange(len(arr_num)): for j in xrange(len(arr_num)-1-i): if arr_num[j] > arr_num[j+1]: arr_num[j+1],arr_num[j] = arr_num[j],arr_num[j+1] print arr_num #脚本执行过程如下 [1, 99, -2, 3, 14, 12, 3, -14, 3333, 4] [1, -2, 99, 3, 14, 12, 3, -14, 3333, 4] [1, -2, 3, 99, 14, 12, 3, -14, 3333, 4] [1, -2, 3, 14, 99, 12, 3, -14, 3333, 4] [1, -2, 3, 14, 12, 99, 3, -14, 3333, 4] [1, -2, 3, 14, 12, 3, 99, -14, 3333, 4] [1, -2, 3, 14, 12, 3, -14, 99, 3333, 4] [1, -2, 3, 14, 12, 3, -14, 99, 3333, 4] [1, -2, 3, 14, 12, 3, -14, 99, 4, 3333] [-2, 1, 3, 14, 12, 3, -14, 99, 4, 3333] [-2, 1, 3, 14, 12, 3, -14, 99, 4, 3333] [-2, 1, 3, 14, 12, 3, -14, 99, 4, 3333] [-2, 1, 3, 12, 14, 3, -14, 99, 4, 3333] [-2, 1, 3, 12, 3, 14, -14, 99, 4, 3333] [-2, 1, 3, 12, 3, -14, 14, 99, 4, 3333] [-2, 1, 3, 12, 3, -14, 14, 99, 4, 3333] [-2, 1, 3, 12, 3, -14, 14, 4, 99, 3333] [-2, 1, 3, 12, 3, -14, 14, 4, 99, 3333] [-2, 1, 3, 12, 3, -14, 14, 4, 99, 3333] [-2, 1, 3, 12, 3, -14, 14, 4, 99, 3333] [-2, 1, 3, 3, 12, -14, 14, 4, 99, 3333] [-2, 1, 3, 3, -14, 12, 14, 4, 99, 3333] [-2, 1, 3, 3, -14, 12, 14, 4, 99, 3333] [-2, 1, 3, 3, -14, 12, 4, 14, 99, 3333] [-2, 1, 3, 3, -14, 12, 4, 14, 99, 3333] [-2, 1, 3, 3, -14, 12, 4, 14, 99, 3333] [-2, 1, 3, 3, -14, 12, 4, 14, 99, 3333] [-2, 1, 3, -14, 3, 12, 4, 14, 99, 3333] [-2, 1, 3, -14, 3, 12, 4, 14, 99, 3333] [-2, 1, 3, -14, 3, 4, 12, 14, 99, 3333] [-2, 1, 3, -14, 3, 4, 12, 14, 99, 3333] [-2, 1, 3, -14, 3, 4, 12, 14, 99, 3333] [-2, 1, -14, 3, 3, 4, 12, 14, 99, 3333] [-2, 1, -14, 3, 3, 4, 12, 14, 99, 3333] [-2, 1, -14, 3, 3, 4, 12, 14, 99, 3333] [-2, 1, -14, 3, 3, 4, 12, 14, 99, 3333] [-2, -14, 1, 3, 3, 4, 12, 14, 99, 3333] [-2, -14, 1, 3, 3, 4, 12, 14, 99, 3333] [-2, -14, 1, 3, 3, 4, 12, 14, 99, 3333] [-14, -2, 1, 3, 3, 4, 12, 14, 99, 3333] [-14, -2, 1, 3, 3, 4, 12, 14, 99, 3333] [-14, -2, 1, 3, 3, 4, 12, 14, 99, 3333] [-14, -2, 1, 3, 3, 4, 12, 14, 99, 3333] [-14, -2, 1, 3, 3, 4, 12, 14, 99, 3333] [-14, -2, 1, 3, 3, 4, 12, 14, 99, 3333]
插入排序例子
#!/usr/bin/env python arr_num = [99,1,-2,3,14,12,3,-14,3333,4] for i in xrange(1,len(arr_num)): key = arr_num[i] j = i -1 while j >= 0: if arr_num[j] > key: arr_num[j+1] = arr_num[j] arr_num[j] = key print arr_num j -= 1 #print arr_num #执行过程如下 [1, 99, -2, 3, 14, 12, 3, -14, 3333, 4] [1, -2, 99, 3, 14, 12, 3, -14, 3333, 4] [-2, 1, 99, 3, 14, 12, 3, -14, 3333, 4] [-2, 1, 3, 99, 14, 12, 3, -14, 3333, 4] [-2, 1, 3, 14, 99, 12, 3, -14, 3333, 4] [-2, 1, 3, 14, 12, 99, 3, -14, 3333, 4] [-2, 1, 3, 12, 14, 99, 3, -14, 3333, 4] [-2, 1, 3, 12, 14, 3, 99, -14, 3333, 4] [-2, 1, 3, 12, 3, 14, 99, -14, 3333, 4] [-2, 1, 3, 3, 12, 14, 99, -14, 3333, 4] [-2, 1, 3, 3, 12, 14, -14, 99, 3333, 4] [-2, 1, 3, 3, 12, -14, 14, 99, 3333, 4] [-2, 1, 3, 3, -14, 12, 14, 99, 3333, 4] [-2, 1, 3, -14, 3, 12, 14, 99, 3333, 4] [-2, 1, -14, 3, 3, 12, 14, 99, 3333, 4] [-2, -14, 1, 3, 3, 12, 14, 99, 3333, 4] [-14, -2, 1, 3, 3, 12, 14, 99, 3333, 4] [-14, -2, 1, 3, 3, 12, 14, 99, 4, 3333] [-14, -2, 1, 3, 3, 12, 14, 4, 99, 3333] [-14, -2, 1, 3, 3, 12, 4, 14, 99, 3333] [-14, -2, 1, 3, 3, 4, 12, 14, 99, 3333]