您的位置:首页 > 编程语言 > Python开发

【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]

-----------------------------------------------------------------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐