【Python】Python_learning6:Python中的sort排序函数之序列排序-从小到大&从大到小
2016-08-29 20:54
447 查看
Python_Learning6:
First:Func-sort
python中提供了两个排序函数:(1)List的成员函数sort (2)built-in函数sorted--------------------------------------------------sort------------------------------------------------------------------------
In[3]: help(list.sort)Help on method_descriptor:
sort(...)
L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
method_descriptor
帮助:
排序(…)
L.sort(关键字=无,排序规则= false)->没有稳定排序*中*
~ ~ ~ ~
~ iterable:是可迭代类型,中文意思是迭代器;
iteralbe指的是能够一次返回它的一个成员的对象。iterable主要包括3类:
第一类是所有的序列类型,比如list(列表)、str(字符串)、tuple(元组)。
第二类是一些非序列类型,比如dict(字典)、file(文件)。
第三类是你定义的任何包含__iter__()或__getitem__()方法的类的对象。
~ ~ ~ ~
~ cmp: 用于比较的函数,比较什么由key决定; ~
~ ~ ~
~ key: 指定一个接收一个参数的函数,这个函数用于从每个元素中提取一个用于比较的关键字; ~
~ ~ ~
~ reverse:排序规则,布尔值. reverse = True 降序 或者 reverse = False 升序,有默认值。 ~ ~ ~ ~
~ ~ ~ ~
在本地进行排序,不返回副本
--------------------------------------------------sorted-------------------------------------------------------------------------
In[4]: help(sorted)
Help on built-in function sorted in module builtins:
sorted(iterable, key=None, reverse=False)
Return a new list containing all items from the iterable in ascending order.A custom key function can be supplied to customise the sort order, and the reverse flag can be set to request
the result in descending order.
内置功能模块之进行排序帮助:
排序(迭代,关键 = 无,反 = false)
在升序返回一个包含所有的项目的新列表。一个自定义键功能可以提供自定义排序顺序和反向可以设置标志以降序排列的结果要求。
返回副本,原始输入不变
------------------------------------------------------------------------------------------------------------------------------------
Code ResourceListing:
""" File: example5.py: Time:Created on 2016-08-29 Author: Sure ''' ----------------------------------------------------------------------------- 题目:将输入的数由小到大排序输出 """ #!/usr/bin/python # -*- coding: UTF-8 -*-
l = [] for i in range(7): #随机产生7个数 x = int(input('integer:\n')) l.append(x) #append(x)向列表的尾部添加一个新的元素。只接受一个参数。
<pre name="code" class="python">''' append()用法示例: >>> mylist = [1,2,0,'abc'] >>> mylist [1, 2, 0, 'abc'] >>> mylist.append(4) >>> mylist [1, 2, 0, 'abc', 4] >>> mylist.append('haha') >>> mylist [1, 2, 0, 'abc', 4, 'haha'] >>> '''l.sort()
print('The result is:',l) print(l)
</pre><pre code_snippet_id="1856848" snippet_file_name="blog_20160829_5_5575442" name="code" class="python">结果输出:
"F:\Program Files\Anaconda\python.exe" "E:/Python Programs/Bressanone/example5.py" integer: 3 integer: 45 integer: 6 integer: 5 integer: 3 integer: 2 integer: 1 The result is: [1, 2, 3, 3, 5, 6, 45] [1, 2, 3, 3, 5, 6, 45]
-----------------------------------------------------------------------------------------------------------------------------------
相关文章推荐
- python中列表排序sort()方法或者内建函数sorted()
- 已知字母序列【d, g, e, c, f, b, o, a】,请实现一个函数针对输入的一组字符串 input[] = {"bed", "dog", "dear", "eye"},按照字母顺序排序并打印
- python的排序函数sort,sorted在列表排序和字典排序中的应用详解和举例
- 【php数组函数序列】之sort() - 对数组的元素值进行升序排序
- python几个排序函数 sort sorted argsort
- Python的排序函数Sort,Sorted
- Python中的排序函数argsort,sort,sorted用法
- python的排序函数sort,sorted在列表排序和字典排序中的应用详解和举例
- matlab sort() 排序函数…
- Python中的排序函数argsort,sort,sorted
- python中排序,函数sort、sorted、argsort,collections.OrderedDict类
- 整理:STL 中 sort 函数用法简介 && 七种 qsort 排序方法
- 已知字母序列【d, g, e, c, f, b, o, a】,请实现一个函数针对输入的一组字符串 input[] = {"bed", "dog", "dear", "eye"},按照字母顺序排序并打印
- python容器排序sort()和内建函数sorted()的区别
- 【转】sort()函数定义在头文件<algorithm>中,它把容器中的数据重新排序成非递减序列
- python的排序函数sort,sorted
- 【php数组函数序列】之rsort() - 对数组的元素值进行降序排序
- php数组函数序列之sort() 对数组的元素值进行升序排序
- C# List<T>.Sort排序
- php数组函数序列之rsort() - 对数组的元素值进行降序排序