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 { ...
随机推荐
- python3 第十八章 - 迭代器与生成器
1.迭代器(Iterator) 迭代是访问集合元素的一种方式 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退. 迭代器 ...
- git log 查看指定文件的提交记录
使用git log除了可以看整个仓库的提交记录外,还可以指定某个文件的提交记录. 1. 查看指定文件的历史提交记录 命令: git log -- <file> 说明:只需要指定文件名称. ...
- python_如何使用临时文件
案例: 某项目中,从传感器中获得采集数据,每收集到1G的数据后做是数据分析,最终只保留数据分析的结果,收集到的数据放在内存中,将会消耗大量内存,我们希望把这些数据放到一个临时的文件中 临时文件不能命名 ...
- awkOFS问题
awk改变了OFS,$0却没变化一个文件1.txt,内容如下 a b c d e 目的把列变行,输出为: a b c d e 脚本如下: awk 'BEGIN{RS="";FS=& ...
- Div+Css画太极图源代码
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>D ...
- 各模拟器adb连接端口
如果执行自动化测试,在没有真机的情况下,我们唯一的选择便是模拟器.目前市面上有很多模拟器,他们使用adb连接时都会有不同的默认接口,当adb无法自动连接模拟器时,手动使用ip+端口连接是很好的选择.下 ...
- 关于define和const
1.通过define定义的常量,在C语言里面一般叫宏定义.define的本质是简单的文本替换. 2.const定义一个变量,但是这个变量的值只能在定义的时候赋予,之后就不能被更改了. 如果变量声明中带 ...
- Electron 桌面应用打包(npm run build)简述(windows + mac)
最近一段时间在用electron+vue做内部项目的一键构建发布系统的桌面应用,现就其中打包流程写个备注,以示记录. Windows环境打包:1.首先贴一下package.json. { " ...
- spark头脑镜像
思考是一件有意思的事情.遇到问题,思考出结论,那么脑子里面的过程是什么呢,或者脑子里面是什么呢.我一直认为,这团团的里面是一个模糊的n维空间.理解一个复杂的系统.公式.算法,都要在这个n维空间里具象化 ...
- python各种运算优先级一览表
##python各种运算的优先级 运算符 描述 lambda Lambda表达式 or 布尔"或" and 布尔"与" not x 布尔"非" ...