python 3-5 如何对迭代器做切片操作itertools.islice(open("test.txt"),0,10),迭代器消耗
来源:互联网 发布:炉石传说大数据2016 编辑:程序博客网 时间:2024/05/21 10:05
文件对象无法进行切片操作
islice用于切片操作islice(iterable, [start,] stop [, step]) --> islice objectislice(f,0,10) 取文件的前10行islice(f,100,300) #生成 文件 100 到 300 行的生成器,不包含第300行islice(f,500) #生成500行以内的生成器islice(f,500,None) #生成500行以后的生成器for line in islice(f,0,10): print line
迭代器消耗
首先la 通过列表解析生成一个list,然后将其迭代器赋值给lailai赋值给islice后消耗掉前9个index,islice之后再次遍历lai时候,会从index=10开始迭代la = [x for x in xrange(20)]>>> lai = iter(la)>>> for item in islice(lai,5,9):... print item... 5678>>> for x in lai:... print x... 910111213141516171819
islice帮助信息
islice实现了iter 和 next()表明islice既是可迭代对象又是迭代器
>>> help(islice)Help on class islice in module itertools:class islice(__builtin__.object) | islice(iterable, [start,] stop [, step]) --> islice object | | Return an iterator whose next() method returns selected values from an | iterable. If start is specified, will skip all preceding elements; | otherwise, start defaults to zero. Step defaults to one. If | specified as another value, step determines how many values are | skipped between successive calls. Works like a slice() on a list | but returns an iterator. | | Methods defined here: | | __getattribute__(...) | x.__getattribute__('name') <==> x.name | | __iter__(...) | x.__iter__() <==> iter(x) | | next(...) | x.next() -> the next value, or raise StopIteration | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __new__ = <built-in method __new__ of type object> | T.__new__(S, ...) -> a new object with type S, a subtype of T
0 0
- python 3-5 如何对迭代器做切片操作itertools.islice(open("test.txt"),0,10),迭代器消耗
- python-9-如何对迭代器做切片操作?
- 如何对迭代器做切片操作
- Python高级编程-如何对迭代器进行切片操作?
- python如何对list进行切片操作
- 3-5对迭代器做切片操作
- [Python] 对字符串,列表的切片操作
- python迭代器itertools
- python对字符串切片
- 使用python对txt,excel批量操作
- Python 切片操作
- python切片操作
- 【Python】切片操作符
- python-切片操作符
- Python切片操作
- python 切片操作
- python切片操作
- python中的切片操作
- UILabel的高度和宽度自适应
- 测试小故事14:密码中可以使用空格吗?
- linux 挂载
- 欢迎使用CSDN-markdown编辑器
- Android编译jdk版本错误
- python 3-5 如何对迭代器做切片操作itertools.islice(open("test.txt"),0,10),迭代器消耗
- 图的遍历--使用邻接表作为存储结构的遍历(DFS、BFS)C语言
- 深入分析 Java 中的中文编码问题
- LeetCode 258. Add Digits 题解
- Core OS层 LocalAuthentication框架
- FAILED: Execution Error, return code 2 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask
- Dubbo之服务在Linux操作系统上的部署
- C语言二分法查找数组
- github配置ssh的时候如何使用浏览器代理