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 第二十一章 - 函数式编程之return函数和闭包
我们来实现一个可变参数的求和.通常情况下,求和的函数是这样定义的: def calc_sum(*args): ax = 0 for n in args: ax = ax + n return ax 但 ...
- linux_文件权限
权限贯穿linux整个系统 创建文件或目录,属主和组都是当前用户 linux权限位? 9位基础权限位, 3位一组,总共12位权限 用户对文件权限,相当于你的笔记本 r 读 4 w ...
- python_如何在循环引用中管理内存?
案例: python中通过引用计数来回收垃圾对象,在某些环形数据结构(树,图--),存在对象间的循环引用,比如树的父节点引用子节点,子节点同时引用父节点,此时通过del掉引用父子节点,两个对象不能被立 ...
- 微信屏蔽js分享、复制链接
页面内引入js(不放在页面内部不起作用) $(function(){ function onBridgeReady() { WeixinJSBridge.call('hideOptionMenu'); ...
- Oracle多行记录合并的几种方法
今天正好遇到需要做这个功能,顺手搜了一下网络,把几种方法都列出来,方便以后参考. 1 什么是合并多行字符串(连接字符串)呢,例如: SQL> desc test; Name Type Nulla ...
- mybatis 中文文档
http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html
- Java并发系列[2]----AbstractQueuedSynchronizer源码分析之独占模式
在上一篇<Java并发系列[1]----AbstractQueuedSynchronizer源码分析之概要分析>中我们介绍了AbstractQueuedSynchronizer基本的一些概 ...
- linux判断文件是否存在
#!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" # 这里的-x 参 ...
- javacript 组合使用构造函数模式和原型模式
构造函数模式创建对象 基本方法 function Person(name,age){ this.name=name; this.age=age; this.sayName=function(){ al ...
- Java自学能学会吗?最新Java高级学习路线
就好像一千个人心中有一千个哈利波特一样,这个答案不绝对也不唯一,Java好学但自学的确阻力有些大,作为一门技术语言,它所蕴含的智慧绝对不是靠几本XX入门,XX框架之类的书所能概括,自学Java你要做好 ...