018.Python迭代器以及map和reduce函数
一 迭代器
- 能被next进行调用,并且不断返回下一个值的对象
- 特征:迭代器会生成惰性序列,它通过计算把值依次的返回,一边循环一边计算而不是一次性得到所有数据
- 优点:需要数据的时候,一次取一个,可以大大节省内存空间.而不是一股脑的把所有数据放进内存.
- 可以遍历无限量的数据
- next调用迭代器时,方向是单向不可逆的.
1.1 可迭代性对象
__iter__ 如果这个数据类型含有__iter__ 方法 我们就说他是可迭代对象
dir 获取当前数据内置的方法和属性.
setvar = {1,2,"abc",54,"dd"}
for i in setvar:
print(i)
lst = dir(setvar)
print(lst)
print("__iter__" in lst)
执行
dd
1
2
54
abc
['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__',
'__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__init_subclass__',
'__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__',
'__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__',
'__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear',
'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update',
'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update',
'union', 'update']
True
1.2 迭代器
可迭代型数据:可以遍历的数据
for 循环在遍历集合的时候,在底层用next方法实现的集合的调用
区别
可迭代对象 -> 迭代器 不可直接调用 -> 可直接调用的过程
如何变成迭代器
(1) iter (2)__iter__() #这两个方法可以变成迭代器
如何遍历迭代器?
(1) next (2)__next__()
如何判断迭代器?
__iter__ __next__ 如果含有这两个方法,就说他是迭代器
可迭代对象不一定是迭代器,迭代器一定是可迭代对象
setvar = {1,2,"abc",54,"dd"}
it = iter(setvar)
lst = dir(it)
print(lst)
print('__iter__' in lst and '__next__' in lst)
res = next(it)
print(res)
res = next(it)
print(res)
res = next(it)
print(res)
res = next(it)
print(res)
res = next(it)
print(res)
执行
[root@node10 python]# python3 test.py t test.py
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__length_hint__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
True
1
2
dd
54
abc
1.3 判断是否是可迭代对象或者迭代器
- from .. import 从哪个模块 ... 引入 ...东西
- 从collections模块 引入 Iterator类型(迭代器类型) Iterable(可迭代对象)
判断集合的迭代属性
from collections import Iterator,Iterable
# 判断集合的迭代属性
setvar = {1,2,"abc",54,"dd"}
res = isinstance(setvar,Iterable)
print(res)
res = isinstance(setvar,Iterator)
print(res)
执行
[root@node10 python]# python3 test.py t test.py
True
False
判断range对象的迭代属性
from collections import Iterator,Iterable
# 1.判断集合的迭代属性
setvar = {1,2,"abc",54,"dd"}
print(isinstance(range(10),Iterable)) # True
print(isinstance(range(10),Iterator)) # False #使用iter方法,可以把一个可迭代对向变成一个迭代器
it = iter(range(10))
res = next(it)
print(res)
res = next(it)
print(res)
执行
[root@node10 python]# python3 test.py t test.py
True
False
0
1
遍历迭代器
it = iter(range(10))
for i in it:
print(i)
执行
[root@node10 python]# python3 test.py
0
1
2
3
4
5
6
7
8
9
1.4 迭代器的越界现象错误
it = iter(range(10))
for i in it:
print(i)res = next(it)print(res)
执行
[root@node10 python]# python3 test.py
0
1
2
3
4
5
6
7
8
9
Traceback (most recent call last):
File "test.py", line 4, in <module>
res = next(it)
StopIteration
StopIteration 是迭代器的越界现象错误,是因为没有值了
1.5 重置迭代器
it = iter(range(10))
# for i in it:
# print(i) # 使用for 和 next 搭配来遍历迭代器for i in range(3):
res = next(it)
print(res)
执行
[root@node10 python]# python3 test.py
0
1
2
二 高阶函数
- 能够把函数当成参数传递的就是高阶函数 (map reduce sorted filter)
2.1 map(func,iterable)
功能:把iterable里面的数据一个一个的拿出来,扔到func当中进行处理,然后把处理之后的结果放到迭代器当中,最终返回迭代器
参数:
func:自定义函数 或者 内置函数
iterable:可迭代对象(常用:容器类型数据,range对象,迭代器)
返回值:
迭代器
["1","2","3","4"] 变成整型 [1,2,3,4]
listvar = ["1","2","3","4"]
lst = []
for i in listvar:
print(i,type(i)) #打印数值并输出类型
res = int(i) #强制转换成整型
lst.append(res) #塞进空列表
print(lst)
执行
[root@node10 python]# python3 test.py
1 <class 'str'>
2 <class 'str'>
3 <class 'str'>
4 <class 'str'>
[1, 2, 3, 4]
使用map实现
- 每次从listvar当中拿出一个值 ,
- 放到int函数当中进行强转,处理后的结果扔到迭代器当中
- 依次类推,直到所有数据拿完为止.
listvar = ["1","2","3","4"]
from collections import Iterator,Iterable
it = map(int,listvar)
print(isinstance(it,Iterator))
print(isinstance(it,Iterable))
#next取值
res = next(it)
print(res)
res = next(it)
print(res)
res = next(it)
print(res)
res = next(it)
print(res)
执行
[root@node10 python]# python3 test.py
True
True
1
2
3
4
使用for循环取值
listvar = ["1","2","3","4"]
from collections import Iterator,Iterable
it = map(int,listvar)
print(isinstance(it,Iterator))
print(isinstance(it,Iterable))
for i in it:
print(i)
执行
[root@node10 python]# python3 test.py
True
True
1
2
3
4
list类型强转
使用list强转迭代器可以瞬间拿到迭代器中所有数据
listvar = ["1","2","3","4"]
from collections import Iterator,Iterable
it = map(int,listvar)
lst = list(it)
print(lst)
执行
[root@node10 python]# python3 test.py
[1, 2, 3, 4]
[“5”,“4”,“9”,“9"] 转换为5499
使用字符串拼接
lst=["5","4","9","9"]
res=''.join(lst)
print(res)
执行
[root@node10 python]# python3 test.py
5499
[1,2,3,4] 转为[2,4,6,8]
lst = []
for i in [1,2,3,4]:
res = i * 2
lst.append(res)
print(lst)
使用map
如果使用自定义方法,切记要加上return 返回值
from collections import Iterator,Iterable
lst = [1,2,3,4]
def func(n):
return n * 2
it = map(func,lst)
print(isinstance(it,Iterator))
print(list(it))
执行
[root@node10 python]# python3 test.py
True
[2, 4, 6, 8]
{97:'a',98:'b',99:'c'} ['a','b','c'] =>[97,98,99]
打印出键
dic = {97:"a",98:"b",99:"c"}
for i in dic:
print (i)
执行
[root@node10 python]# python3 test.py
97
98
99
使用item打印键值对,并反转
dic = {97:"a",98:"b",99:"c"}
dic2={}
for a,b in dic.items():
dic2[b] =a
print(dic2)
执行
[root@node10 python]# python3 test.py
{'a': 97, 'b': 98, 'c': 99}
正常顺序
使用map
lst = ['a','b','c']
def func(n):
dic = {97:"a",98:"b",99:"c"}
dic2={}
for a,b in dic.items():
dic2[b] =a
return dic2[n]
it = map(func,lst) #func是自定义函数,lst是可迭代对象
print (list(it)) #list(it)强制list转换
执行
root@node10 python]# python3 test.py
[97, 98, 99]
2.2 reduce函数
reduce(func,iterable)
功能:
计算
首先把iterable 当中的两个值拿到func当中进行运算,计算的结果在和iterable中的第三个值
拿到func中计算,依次类推.返回最终的结果
参数:
func 自定义函数 或者 内置函数
iterable 可迭代对象(常用:容器类型数据 range对象 迭代器)
返回值:
最终的计算结果
[5,4,9,9] 转换为5499
使用上个方式不能成功
lst=[5,4,9,9]
res=''.join(lst)
print(res)
执行报错

先取出转为字符串类型,合并在转
strvar = ''
for i in [5,4,9,9]:
strvar += str(i)
print(strvar,type(strvar))
print(int(strvar),type(int(strvar)))
执行
[root@node10 python]# python3 test.py
5499 <class 'str'>
5499 <class 'int'>
使用reduce实现
逻辑实现
5*10 +4 = 54
54*10+9 = 549
549*10+9 = 5499
普通示例
lst = [5,4,9,9]
it = iter(lst)
res1 = next(it)
res2 = next(it)
total = res1 * 10 + res2
print(total) for i in it:
#54
# 54 * 10 + 9 = 549
# 549 * 10 + 9 = 5499
total = total * 10 + i
print(total)
执行
[root@node10 python]# python3 test.py
54
5499
reduce实现
from functools import reduce
def func(x,y):
return x*10 + y
lst = [5,4,9,9]
res = reduce(func,lst)
print(res)
执行
[root@node10 python]# python3 test.py
5499
实现过程
先把列表中5和4拿出来放到func函数中用x,和 y来接收参数
x*10+y => 5*10+4 =54
第二次 拿54 和 9两个值扔到func当中进行运算
x*10+y => 54 * 10 + 9 => 549
第三次 拿549 和 9 两个值扔到func当中进行运算
x*10+y => 549 * 10 + 9 => 5499
到此所有计算完毕 ,返回5499
"534" => 534 不使用int强转实现
from functools import reduce
strvar = "534"
def func(x,y):
return x*10 + y 这里变成字符串拼接,而不是一个数字计算 res = reduce(func,list(strvar))
print(res)
执行
[root@node10 python]# python3 test.py
555555555535555555555355555555553555555555535555555555355555555553555555555535555555555355555555553555555555534
正确方式
from functools import reduce
strvar = "534"
def func(x,y):
return x*10 + y # res = reduce(func,list(strvar))
# print(res) error def func2(n):
dic = {'0':0,'1':1,'2':2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9} #定义一个字典,定义字符串和对应键值对
return dic[n] #遇到对应字符串的键,返回该键的值 it = map(func2,"534") #相当于吧字符串迭代取出,放进func执行
# print(list(it)) #这个使用list强转就是[5,3,4]
res = reduce(func,it) #取出it的迭代数据,使用func进行计算
print(res)
执行
[root@node10 python]# python3 test.py
534
018.Python迭代器以及map和reduce函数的更多相关文章
- Python自学笔记-map和reduce函数(来自廖雪峰的官网Python3)
感觉廖雪峰的官网http://www.liaoxuefeng.com/里面的教程不错,所以学习一下,把需要复习的摘抄一下. 以下内容主要为了自己复习用,详细内容请登录廖雪峰的官网查看. Python内 ...
- Python中的map和reduce函数简介
①从参数方面来讲: map()函数: map()包含两个参数,第一个是参数是一个函数,第二个是序列(列表或元组).其中,函数(即map的第一个参数位置的函数)可以接收一个或多个参数. reduce() ...
- Python 中的map、reduce函数用法
#-*- coding:UTF-8 -*- #map()函数接受两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回 def f(x): retu ...
- python Map()和reduce()函数
Map()和reduce()函数 map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函 ...
- Python map,filter,reduce函数
# -*- coding:utf-8 -*- #定义一个自己的map函数list_list = [1,2,4,8,16] def my_map(func,iterable): my_list = [] ...
- python的map和reduce函数
map函数时python的高级内置函数 语法为:map(function, iterable, ...) 参数:function -- 函数iterable -- 一个或多个序列 将function作 ...
- Python之filter、map、reduce函数
简介三函数: 高阶函数:一个函数可以接收另一个函数作为参数,这种函数称之为高阶函数. filter.map.reduce三个函数都是高阶函数,且语法都一致:filter/map/reduce(func ...
- python中的map、reduce、filter、sorted函数
map.reduce.filter.sorted函数,这些函数都支持函数作为参数. map函数 map() 函数语法:map(function, iterable, ...) function -- ...
- Python中map和reduce函数??
①从参数方面来讲: map()函数: map()包含两个参数,第一个是参数是一个函数,第二个是序列(列表或元组).其中,函数(即map的第一个参数位置的函数)可以接收一个或多个参数. reduce() ...
随机推荐
- 可读性友好的JavaScript:两个专家的故事
每个人都想成为专家,但什么才是专家呢?这些年来,我见过两种被称为"专家"的人.专家一是指对语言中的每一个工具都了如指掌的人,而且无论是否有帮助,都一定要用好每一点.专家二也知道每一 ...
- 北航OO第一单元作业总结(Retake)
前言:当我写这篇博客的时候,我的心情是复杂的,因为这实际上是我第二次写这篇博客--我今年重修的这门课.我对去年的成绩心有不甘--在激烈的竞争下,我虽然尽可能完成了所有作业(仅一次作业未通过弱测),但爆 ...
- 安装mmdetection,运行报错Segmentation fault
具体安装过程详见https://github.com/open-mmlab/mmdetection/blob/master/docs/INSTALL.md 在安装完成mmdetection后运行tes ...
- (十)Docker-V 详解
1. 作用 挂载宿主机的一个目录. 2. 案例 譬如我要启动一个centos容器,宿主机的/test目录挂载到容器的/soft目录,可通过以下方式指定: # docker run -it -v /te ...
- 浅入Kubernetes(10):控制节点的部署,选择器、亲和性、污点
目录 标签和nodeSelector 标签选择 亲和性和反亲和性 污点和容忍度 系统默认污点 容忍度 DaemonSet 在前面的学习中,我们学到了 Deployment 部署,以及副本数(Repli ...
- SQL Server 审计(Audit)
审计(Audit)用于追踪和记录SQL Server实例,或者单个数据库中发生的事件(Event),审计运作的机制是通过捕获事件(Event),把事件包含的信息写入到事件日志(Event Log)或审 ...
- 使用Tensorflow Object Detection进行训练和推理
整体流程(以PASCAL VOC为例) 1.下载PASCAL VOC2012数据集,并将数据集转为tfrecord格式 2.选择并下载预训练模型 3.配置训练文件configuration(所有的训练 ...
- java语言写一个建议的五子棋
经过16天的java学习,也学得了不少关于Java方面的知识,我想分享一下我用java写的一个简单的五子棋. 游戏规则: (1)对局双方各执一色棋子.(2)空棋盘开局.(3)白先.黑后,交替下子,每次 ...
- Windows PE 第十三章 PE补丁技术
PE补丁技术 这章很多东西之前都见过,也单独总结过,比如动态补丁里说的远程代码注入,还有hijack什么的.之前整理过的这里就不细说了,大体说下思路.这里总结一些之前没总结过的东西. 资料中把补丁分为 ...
- JVM虚拟机-运行时数据区概述
目录 运行时数据区域 总览 概念扫盲 什么是栈帧(Stack Frame) JVM常见出现两种错误 程序计数器 虚拟机栈 结构 局部变量表 方法是如何调用的 本地方法栈 堆 浅堆和深堆 堆的细分 方法 ...