1.8 range
哈哈,前边忘了介绍这个知识点了,老是用人家,不介绍一下都不好意思了。
range()函数是一个用来创建数字序列的函数。
问题来了,为什么要写函数?
封装代码啊,让使用者不需要关心具体业务逻辑是如何实现的,拿过来能用就行了。
问题又来了,既然不需要关注业务逻辑的具体实现过程,那需要关注什么?
关注函数的输入、输出这两部分就了,输入指的是函数的入参,输出指的是函数的返回值。知道函数执行需要什么参数、执行完返回什么就行了。
所以,我就要先介绍range()函数的参数了
range(start, end, scan):
range()函数有3个参数,下面分别介绍一下这三个参数的含义
start:计数的开始位置,默认值为0,即如果不给start传递参数值的话,默认start=0;
end:计数的结束位置,编程语言中的范围基本上都是含头不含尾的,稍后举例说明
scan:大家喜闻乐见的步长,跟切片中的步长是一个意思,注意range()中的步长只能是整数,不支持浮点数
正所谓:百闻不如一见,百说不一练
>>> #介绍range()的用法
>>> # 1 只有一个参数
>>> r = range(5)
>>> r
range(0, 5)
>>> l = list(r)
>>> l
[0, 1, 2, 3, 4]
>>>
>>> type(r)
<class 'range'>
>>>
>>> for i in r: #说明range是可迭代对象, 可迭代对象和迭代器在后面章节会有讲
print(i)
0
1
2
3
4
>>>
>>> # 2 两个参数的情况
>>> rr = range(5,10)
>>> rr
range(5, 10)
>>> ll = list(rr)
>>> ll
[5, 6, 7, 8, 9]
>>>
>>>
>>> # 3 三个参数的情况
>>> rrr = range(5,100,5)
>>> rrr
range(5, 100, 5)
>>> lll = list(rrr)
>>> lll
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
>>>
下面介绍range()在开发中的应用,我目前知道的range()更多的是用于循环,或转化为list、tuple。
>>> # 1 将range转化为list
>>> l = list(range(5))
>>> type(l)
<class 'list'>
>>>
>>> # 2 将range转化为tuple
>>> t = tuple(range(5))
>>> type(t)
<class 'tuple'>
>>>
>>> # 3 range是可迭代对象可用于循环遍历
>>> for i in range(5):
print(i)
0
1
2
3
4
>>>
>>> # 4 上面说到range的步长不能是浮点数,如果非要用浮点数会报什么错?
>>> r = range(1,100,7.0)
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
r = range(1,100,7.0)
TypeError: 'float' object cannot be interpreted as an integer
>>>
在整理素材的时候看到有网友用range()结合list()写了一个冒泡程序。我也想写个。
先搞懂什么是冒泡:
冒泡排序就是把小的元素往前调或者把大的元素往后调。比较是相邻的两个元素比较,交换也发生在这两个元素之间。所以,如果两个元素相等,我想你是不会再无聊地把他们俩交换一下的;如果两个相等的元素没有相邻,那么即使通过前面的两两交换把两个相邻起来,这时候也不会交换,所以相同元素的前后顺序并没有改变,所以冒泡排序是一种稳定排序算法。
自己编写代码实现冒泡:
>>> l = list(range(9)) #先生成一个列表 >>> l [0, 1, 2, 3, 4, 5, 6, 7, 8] >>> import random >>> random.shuffle(l) #把列表元素顺序打乱,这个在random模块有介绍 >>> l [6, 5, 7, 8, 4, 3, 1, 0, 2] >>> >>> i = 0 >>> #我想了一下,需要排序的次数最多为len(l)-1次,每次排序有len(l)-1个比较元素大小的判断。
if len(l) < 2: print('元素太少了,排什么序啊') else: while i < len(l)-1: #看到这里的len(l)-1了吗,在这段代码中出现了两次,以前在做数据库开发做报表的时候,项目经理就跟我说要把SQL中重复的部分 i += 1 #提取出来, 如本例中可以定义一个变量 a = len(l)-1,代码就会简洁一些。 for j in list(range(len(l)-1)): if l[j] >= l[j+1]: l[j],l[j+1] = l[j+1],l[j] >>> l [0, 1, 2, 3, 4, 5, 6, 7, 8] >>>
def bubble(bubbleList):
listLength = len(bubbleList)
while listLength > 0:
for i in range(listLength - 1):
if bubbleList[i] > bubbleList[i+1]:
bubbleList[i] = bubbleList[i] + bubbleList[i+1]
bubbleList[i+1] = bubbleList[i] - bubbleList[i+1]
bubbleList[i] = bubbleList[i] - bubbleList[i+1]
listLength -= 1
print bubbleList
if __name__ == '__main__':
bubbleList = [3, 4, 1, 2, 5, 8, 0]
bubble(bubbleList)
#这段代码是在网上找到的网友实现的冒泡排序的函数
#对于有可能反复利用到的代码段还是要封装起来,便于引用。
下节课没什么毛病的话就该介绍list了
1.8 range的更多相关文章
- SQL Server 合并复制遇到identity range check报错的解决
最近帮一个客户搭建跨洋的合并复制,由于数据库非常大,跨洋网络条件不稳定,因此只能通过备份初始化,在初始化完成后向海外订阅端插入数据时发现报出如下错误: Msg 548, Level 16, S ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- [LeetCode] Range Addition 范围相加
Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- C++11中自定义range
python中的range功能非常好用 for i in range(100): print(i) 现在利用C++11的基于范围的for循环特性实现C++中的range功能 class range { ...
随机推荐
- Linux指令--cp
原文出处:http://www.cnblogs.com/peida/archive/2012/10/29/2744185.html cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一.一 ...
- 04_Javascript初步第二天(下)
错误对象 try{ aa();//这是一个未被定义的方法 }catch(e){ alert(e.name+":"+e.message);//输出:ReferenceError:aa ...
- unity getcomponentsinchildren 翻船
今天使用GetComponentsInChildren, 老司机翻船.因为一直以来我使用这个函数,下意识的从来所有的相同component都是放在子节点下,本身节点肯定不会放一个相同的componen ...
- php之快速排序
<?phpfunction shell_sort(array $arr){ $right=$left = array(); $Rights=$Lefts = array() ...
- Eralng的常用数据结构
1.记录(record) 适用于小数据,并且用属性名方便查找 2.Key/Value 类型 a.属性列表 就是类似[{Key, Value}]的列表,可以通过proplists模块来处理这样的列表 当 ...
- mysql数据库在windows下安装与配置
mysql是一种开源源代码的关系型数据库系统(RDBMS),使用最常用的数据库管理语言--结构化查询语句(SQL)进行数据库管理. MySQL是开放源代码的,因此任何人都可以在General Pu ...
- phantomjs集成到scrapy中,并禁用图片,切换UA
phantomjs是一个没有界面的浏览器,支持各种web标准,提供DOM 处理, CSS 选择器, JSON, Canvas, 和 SVG,对于爬取一些经过js渲染的页面非常有用.但是phantomj ...
- python3 爬取百合网的女人们和男人们
学Python也有段时间了,目前学到了Python的类.个人感觉Python的类不应称之为类,而应称之为数据类型,只是数据类型而已!只是数据类型而已!只是数据类型而已!重要的事情说三篇. 据书上说一个 ...
- Spring整合JMS(四)——事务管理
原文链接:http://haohaoxuexi.iteye.com/blog/1983532 Spring提供了一个JmsTransactionManager用于对JMS ConnectionFact ...
- bzoj 2618: [Cqoi2006]凸多边形 [半平面交]
2618: [Cqoi2006]凸多边形 半平面交 注意一开始多边形边界不要太大... #include <iostream> #include <cstdio> #inclu ...