【python实现列表的排序】教程文章相关的互联网学习教程文章

python算法学习之基数排序实例

基数排序法又称桶子法(bucket sort)或bin sort,顾名思义,它是透过键值的部份资讯,将要排序的元素分配至某些"桶"中,藉以达到排序的作用,基数排序法是属于稳定性的排序,其时间复杂度为O (nlog(r)m),其中r为所采取的基数,而m为堆数,在某些时候,基数排序法的效率高于其它的比较性排序法。代码如下:# -*- coding: utf-8 -*- def _counting_sort(A, i): """计数排序,以i位进行排序,以适用于基数排序。 Args: ...

python实现的各种排序算法代码

代码如下:# -*- coding: utf-8 -*-# 测试各种排序算法# link:www.bitsCN.com# date:2013/2/2 #选择排序def select_sort(sort_array): for i, elem in enumerate(sort_array): for j, elem in enumerate(sort_array[i:]): if sort_array[i] > sort_array[j + i]: #交换 sort_array[i], sort_array[j + i] = sort_array[j + i], sort_array[i]#冒泡排序def bubble_sort(sort_a...

python实现归并排序算法

理论不多说: 代码如下: #!/usr/bin/python import sys def merge(array, q, p, r): left_array = array[q:p+1] right_array = array[p+1:r+1] left_array_num = len(left_array) right_array_num = len(right_array) i, j , k= [0, 0, q] while i < left_array_num and j < right_array_num: if (left_array[i] < right_array[j]): array[k] = left_array[i] i+=1 else: array[k] = right_array[j] j+=1 k+=1 while i < left_arr...

python3.0字典key排序

IDLE 3.0 >>> dic = {"aa":1,"bb":2,"ab":3} >>> dic {'aa': 1, 'ab': 3, 'bb': 2} >>> for k in sorted(dic.keys()): print (k) aa ab ----------------------------------------------- 字典对象其实就是键-值对 下面是字典对象的添加,修改,删除 (修改与添加方法相同,当key值不存在的时候添加) >>> dic["cc"] = 4 >>> dic {'aa': 1, 'cc': 4, 'ab': 3, 'bb': 2} >>> dic["bc"] = 5 >>> dic {'aa': 1, 'cc': 4, 'ab': 3, 'bb':...

python快速排序代码

代码如下: def quick_sort(ls): return [] if ls == [] else quick_sort([y for y in ls[1:] if y < ls[0]]) + [ls[0]] + quick_sort([y for y in ls[1:] if y >= ls[0]]) if __name__ == __main__: l1 = [3,56,8,1,34,56,89,234,56,231,45,90,33,66,88,11,22] l2 = quick_sort(l1) print l1 print l2 注意:quick_sort函数中的代码是在一行里面的

Python实现的数据结构与算法之快速排序详解【图】

本文实例讲述了Python实现的数据结构与算法之快速排序。分享给大家供大家参考。具体分析如下: 一、概述 快速排序(quick sort)是一种分治排序算法。该算法首先 选取 一个划分元素(partition element,有时又称为pivot);接着重排列表将其 划分 为三个部分:left(小于划分元素pivot的部分)、划分元素pivot、right(大于划分元素pivot的部分),此时,划分元素pivot已经在列表的最终位置上;然后分别对left和right两个部分进行...

探究数组排序提升Python程序的循环的运行效率的原因

早上我偶然看见一篇介绍两个Python脚本的博文,其中一个效率更高。这篇博文已经被删除,所以我没办法给出文章链接,但脚本基本可以归结如下: fast.pyimport time a = [i for i in range(1000000)] sum = 0 t1 = time.time() for i in a:sum = sum + i t2 = time.time() print t2-t1 slow.pyimport time from random import shuffle a = [i for i in range(1000000)] shuffle(a) sum = 0 t1 = time.time() for i in a:sum = sum + ...

python让图片按照exif信息里的创建时间进行排序的方法

本文实例讲述了python让图片按照exif信息里的创建时间进行排序的方法。分享给大家供大家参考。具体分析如下: 我们经常会从不同的设备里取出照片,比如照相机,手机,iphone等等,操作系统记录的创建日期经常 会因为拷贝等原因变动,下面的代码可以给图片按照exif里的创建时间进行排序,非常有用。代码如下: import os import shutil import Image from PIL.ExifTags import TAGS def print_all_known_exif_tags():for k in sorted...

Python中对列表排序实例

很多时候,我们需要对List进行排序,Python提供了两个方法,对给定的List L进行排序: 方法1.用List的成员函数sort进行排序 方法2.用built-in函数sorted进行排序(从2.4开始) 这两种方法使用起来差不多,以第一种为例进行讲解: 从Python2.4开始,sort方法有了三个可选的参数,Python Library Reference里是这样描述的代码如下: cmp:cmp specifies a custom comparison function of two arguments (iterable elements) which shou...

python选择排序算法实例总结

本文实例总结了python选择排序算法。分享给大家供大家参考。具体如下: 代码1:def ssort(V): #V is the list to be sorted j = 0#j is the "current" ordered position, starting with the first one in the list while j != len(V):#this is the replacing that ends when it reaches the end of the list for i in range(j, len(V)):#here it replaces the minor value that it finds with j position if V[i] < V[j]:#but it d...

python插入排序算法实例分析

本文实例讲述了python插入排序算法。分享给大家供大家参考。具体如下:def insertsort(array): for removed_index in range(1, len(array)): removed_value = array[removed_index] insert_index = removed_index while insert_index > 0 and array[insert_index - 1] > removed_value: array[insert_index] = array[insert_index - 1] insert_index -= 1 array[insert_index] = removed_value另外一个版本:def insertsort(array)...

pymongo实现多结果进行多列排序的方法

本文实例讲述了pymongo实现多结果进行多列排序的方法。分享给大家供大家参考。具体分析如下: 这里多列排序即指定多个排序字段。 集合查询结果排序代码如下:>>> db.Account.find().sort("UserName") --默认为升序 >>> db.Account.find().sort("UserName",pymongo.ASCENDING) --升序 >>> db.Account.find().sort("UserName",pymongo.DESCENDING) --降序 集合查询结果多列排序代码如下:>>> db.Account.find().sort([("UserName",...

python简单实现基数排序算法

本文实例讲述了python简单实现基数排序算法。分享给大家供大家参考。具体实现方法如下:from random import randint def main():A = [randint(1, 99999999) for _ in xrange(9999)]for k in xrange(8):S = [ [] for _ in xrange(10)]for j in A:S[j / (10 ** k) % 10].append(j)A = [a for b in S for a in b]for i in A:print i main()希望本文所述对大家的Python程序设计有所帮助。

Django中对数据查询结果进行排序的方法

在你的 Django 应用中,你或许希望根据某字段的值对检索结果排序,比如说,按字母顺序。 那么,使用 order_by() 这个方法就可以搞定了。>>> Publisher.objects.order_by("name") [, <Publisher: OReilly>]跟以前的 all() 例子差不多,SQL语句里多了指定排序的部分:SELECT id, name, address, city, state_province, country, website FROM books_publisher ORDER BY name;我们可以对任意字段进行排序:>>> Publisher.objects.orde...

python中列表的反转与排序【代码】

1、反转 永久反转>>> test1 [aa, bb, aa, cc, aa, cc, dd, xx, bb] >>> test1.reverse() >>> test1 [bb, xx, dd, cc, aa, cc, aa, bb, aa] 临时反转>>> test1 [bb, xx, dd, cc, aa, cc, aa, bb, aa] >>> for i in reversed(test1):print(i)aa bb aa cc aa cc dd xx bb >>> test1 [bb, xx, dd, cc, aa, cc, aa, bb, aa] 2、排序 永久排序>>> test1 [bb, xx, dd, cc, aa, cc, aa, bb, aa] >>> test1.sort() >>> test1 [aa, aa, aa,...