python 快速排序代码

yipeiwu_com6年前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的核心数据类型之一部分,这些都被称之为内置对象。 对象,就是你面对的所有东西都是对象,看官要逐渐熟悉这个称呼。所有的数据类型,就是...

tensorflow实现打印ckpt模型保存下的变量名称及变量值

有时候会需要通过从保存下来的ckpt文件来观察其保存下来的训练完成的变量值。 ckpt文件名列表:(一般是三个文件) xxxxx.ckpt.data-00000-of-00001 xxx...

python3访问sina首页中文的处理方法

复制代码 代码如下:"""如果只用普通的import urllib.requesthtml = urllib.request.urlopen("http://www.sina.com")...

Python使用指定端口进行http请求的例子

使用requests库 class SourcePortAdapter(HTTPAdapter): """"Transport adapter" that allows us to...

python输出100以内的质数与合数实例代码

具体代码如下所述: __author__ = 'Yue Qingxuan' # -*- coding: utf-8 -*- #求质数 p=[2] for i in range(2,1...