python : itertools 中的 islice : 获取迭代器结果的切片,消耗迭代器
islice(iterable, [start, ] stop [, step]):
创建一个迭代器,生成项的方式类似于切片返回值: iterable[start : stop : step],将跳过前start个项,迭代在stop所指定的位置停止,step指定用于跳过项的步幅。与切片不同,负值不会用于任何start,stop和step,如果省略了start,迭代将从0开始,如果省略了step,步幅将采用1.https://blog.csdn.net/larykaiy/article/details/82934527
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
示例:
step1:创建文本文档
my_file.txt
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2019 wzg16 <wzg16@wzg16-linux>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
step2:读取文档前三行
read1.py
with open("my_file.txt") as f:
lines = f.readlines()
print(lines[0:3])
"""
返回:
['#!/usr/bin/env python\n', '# -*- coding: utf-8 -*-\n', '#\n']
"""
read_islice.py
from itertools import islice
with open("my_files.txt") as f2:
for i in islice(f2,0,3):
print(i)
result = islice(f2,0,3)
print(result)
print(list(result))
"""
输出:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
<itertools.islice object at 0x7f52a94dedb8>
['# Copyright 2019 wzg16 <wzg16@wzg16-linux>\n', '# \n', '# This program is free software; you can redistribute it and/or modify\n']
"""
islice 会消耗迭代器:
示例:
from itertools import islice
l = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]
it = iter(l)
slice1 = islice(it,0,3)
slice2 = islice(it,0,3)
slice3 = islice(it,3,6)
slice4 = islice(it,0,3)
print("slice1 = ",list(slice1))
print("slice2 = ",list(slice2))
print("slice3 = ",list(slice3))
print("slice4 = ",list(slice4))
"""
输出:
slice1 = [1, 2, 3]
slice2 = [4, 5, 6]
slice3 = [10, 11, 12]
slice4 = [13, 14, 15]
————————————————
版权声明:本文为CSDN博主「wzg2016」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/strive_for_future/article/details/95388081
相关文章
- 提供给开发者10款最好的Python IDE
- python读写文件write和flush
- 【Python】python 日期操作
- Python使用设计模式中的责任链模式与迭代器模式的示例
- Python 三大利器:迭代器、生成器、装饰器
- Python语言学习:在python中,如何获取变量的本身字符串名字而非其值/内容及其应用(在代码中如何查找同值的所有变量名)
- Python:python语言中与时间有关的库函数简介、安装、使用方法(获取当前时间/计算程序块前后运行时间/模型训练时间或耗费时间)之详细攻略
- Python语言学习:在python中,如何获取变量的本身字符串名字而非其值/内容及其应用(在代码中如何查找同值的所有变量名)
- Python:python语言中与时间有关的库函数简介、安装、使用方法(获取当前时间/计算程序块前后运行时间/模型训练时间或耗费时间)之详细攻略
- Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(Listbox/Scrollbar)
- Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(Scale/Scale的Command)
- Python语言学习之图表可视化:python语言中可视化工具包的简介、安装、使用方法、经典案例之详细攻略
- 已解决2. Set PROTOCOL_BUPFERS_PYTHON_iMPLEMENTATION=python (but this will use pure-Python parsing and w
- 〖Python WEB 自动化测试实战篇③〗- python-selenium环境配置搭建
- 〖Python 数据库开发实战 - MySQL篇㉞〗- 综合案例 - 新闻管理系统数据库设计的基本属性
- Python学习25:迭代器
- Python编程:pyenv管理多个python版本环境
- python 不同集合上元素的迭代 chain()
- Python: 使用生成器创建新的迭代模式
- python 同时迭代多个序列
- python--迭代器
- 雅虎财经数据python 网络爬虫stock股票 用 Python 通过雅虎财经获取股票数据
- python基础===Python 迭代器模块 itertools 简介
- 多版本Python共存时pip给指定版本的python安装package的方法
- 【Leetcode刷题Python】162. 寻找峰值
- python常用函数技巧汇总
- 【Python】3.python实现图片上传到阿里云OSS