当前位置 : 主页 > 网络编程 > 其它编程 >

35如何对迭代器进行切片操作

来源:互联网 收集:自由互联 发布时间:2023-07-02
fopen(C:\Mynetstat.LOG)linesf.readlines()lineslines[10:20]可以用切片的方式但缺 >>> f open(C:\Mynetstat.LOG)>>> lines f.readlines()>>> lines>>> lines[10:20] 可以用切片的方式但缺点是  f.readlines()是一次性读出文件所
fopen(C:\Mynetstat.LOG)linesf.readlines()lineslines[10:20]可以用切片的方式但缺

>>> f open(C:\Mynetstat.LOG)>>> lines f.readlines()>>> lines>>> lines[10:20]

可以用切片的方式但缺点是  f.readlines()是一次性读出文件所有内容按行保存到列表 中。如果文件过大会非常占用内存。f.read()返回的是字符串。

>>> help(f.read)Help on built-in function read:read(...)read([size]) -> read at most size bytes, returned as a string.If the size argument is negative or omitted, read until EOF is reached.Notice that when in non-blocking mode, less data than what was requestedmay be returned, even if no size parameter was given. help(f.read)

>>> help(f.readlines)Help on built-in function readlines:readlines(...)readlines([size]) -> list of strings, each a line from the file.Call readline() repeatedly and return a list of the lines so read.The optional size argument, if given, is an approximate bound on thetotal number of bytes in the lines returned. help(f.readlines)

读取文本的最好方式还是使用迭代协议。

>>> f.seek(0) #将文件指针指向头部>>> for line in f : #对文件描述符进行迭代打印输出print line>>> from itertools import islice>>> f.seek(0)>>> for x in islice(f,10,20): #打印 10到20行print x

输出结果

TCP    0.0.0.0:49152          0.0.0.0:0              LISTENING

 

  TCP    0.0.0.0:49153          0.0.0.0:0              LISTENING

 

  TCP    0.0.0.0:49154          0.0.0.0:0              LISTENING

 

  TCP    0.0.0.0:49155          0.0.0.0:0              LISTENING

 

  TCP    0.0.0.0:49156          0.0.0.0:0              LISTENING

 

  TCP    127.0.0.1:5037         0.0.0.0:0              LISTENING

 

  TCP    127.0.0.1:5939         0.0.0.0:0              LISTENING

 

  TCP    127.0.0.1:5939         127.0.0.1:55947        ESTABLISHED

 

  TCP    127.0.0.1:5939         127.0.0.1:59695        ESTABLISHED

 

  TCP    127.0.0.1:7475         0.0.0.0:0              LISTENING

>>> f.seek(0)>>> for x in islice(f,20): #打印0到20行print x

输出结果

活动连接

 

 

 

  协议  本地地址          外部地址        状态

 

  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING

 

  TCP    0.0.0.0:443            0.0.0.0:0              LISTENING

 

  TCP    0.0.0.0:445            0.0.0.0:0              LISTENING

 

  TCP    0.0.0.0:902            0.0.0.0:0              LISTENING

 

  TCP    0.0.0.0:912            0.0.0.0:0              LISTENING

 

  TCP    0.0.0.0:7275           0.0.0.0:0              LISTENING

 

  TCP    0.0.0.0:49152          0.0.0.0:0              LISTENING

 

  TCP    0.0.0.0:49153          0.0.0.0:0              LISTENING

 

  TCP    0.0.0.0:49154          0.0.0.0:0              LISTENING

 

  TCP    0.0.0.0:49155          0.0.0.0:0              LISTENING

 

  TCP    0.0.0.0:49156          0.0.0.0:0              LISTENING

 

  TCP    127.0.0.1:5037         0.0.0.0:0              LISTENING

 

  TCP    127.0.0.1:5939         0.0.0.0:0              LISTENING

 

  TCP    127.0.0.1:5939         127.0.0.1:55947        ESTABLISHED

 

  TCP    127.0.0.1:5939         127.0.0.1:59695        ESTABLISHED

 

  TCP    127.0.0.1:7475         0.0.0.0:0              LISTENING

>>> islice(f,20,None) #描述20行到最后

注不支持负索引读完文件之前不知道文件总共有多少行所以不支持负索引

islice的用法举例

>>> l [x for x in xrange(20)] #生成一个列表 >>> l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]>>> t iter(l) #生成一个迭代器对象>>> for x in islice(t,5,10): #islice做为切片消耗原迭代对象。T仍然从0开始迭代不过它把值扔掉直到匹配到start为止print x,

输出结果5 6 7 8 9

继续对t进行迭代执行

>>> for x in t :print x,

输出结果10 11 12 13 14 15 16 17 18 19

可以看出现在对t迭代是从序号0开始的之前的迭代序号在累积。与文件指针有点相似。

>>> 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__ | T.__new__(S, ...) -> a new object with type S, a subtype of T help (islice)

 

转:https://www.cnblogs.com/smulngy/p/8853641.html

上一篇:servlet_JavaWebServlet错误
下一篇:没有了
网友评论