object, we can use like a list of slices to get a 100~300 of the contents of the row file generator?
Solution
Using the standard library itertools.islice , it can return a builder for an iterator object slice
From itertools import islice f = open (' Access.log ') # Top 500 lines # islice (f, 500) # 100 # islice (f, None) for line In
are iteratable objects. can we use a list slicing method to get a 100 ~ Is there a 300-line file content generator?
Solution
Useitertools.isliceIt can return the generator of an iterator object slicing.
From itertools import islice f = open ('Access. log') # First 500 rows # islice (f, 500) # islice (f, 100, None) for line in
), where I1,i2...in comes from the iterator iter1,iter2 ... itern, if the function is None, returns (I1, I2, ..., in As long as one of the provided iterators no longer generates a value, the iteration stops.
>>> from itertools import *
>>> d = IMAP (POW, (2,3,10), (5,2,3))
>>> to I in D:print I
9
1000
#
>>> d = IMAP (POW, (2,3,10), (5,2)) >>> for i in
d:print i
32
9
#
>>> d = IMAP (None, (2,3,10), (5,2))
>>> for i in D:print I
(2, 5) c15/> (3, 2)
certain range, such as the content between and lines, the text file in python is an iteratable object, can I use a list slicing method to obtain a-row file content generator?
Itertools. islice in the standard library can be used to return a generator for iterative object slicing.
F = open ('/var/log/dmesg') from itertools import islice # Slice the file content between 100 and 300 rows. A generator object i
Useitertools.isliceIt can return the generator of an iterator object slicing.
From itertools import islice f = open ('access. log') # first 500 rows # islice (f, 500) # islice (f, 100, None) for line in islice (f, 100 ): print (line)
Islice consumes the previous iteration o
)) --> 0 2 4 6 8
Imap (function, iter1, iter2, iter3,..., iterN)Create an iterator to generate the function (i1, i2 ,..., iN), where i1, i2... iN comes from iter1, iter2... iterN. if function is None, return (i1, i2 ,..., iN), as long as the provided iterator does not generate a value, the iteration stops.
>>> from itertools import * >>> d = imap(pow, (2,3,10), (5,2,3)) >>> for i in d: print i 32 9 1000 #### >>> d = imap(pow, (2,3,10), (5,2)) >>> for i in d: print i 32 9 #### >>> d = imap(
#-*-Coding:utf-8-*-# python:2.x__author__ = ' Administrator '#生成器表达式和itertools模块Parentheses can be used instead of brackets in the #yielditer0= (x**2 for x in range (ten) if x%2==0)For Iter1 in Iter0:Print Iter1#结果"""04163664"""#这样的表达式被称为生成器或者genexp, they use a similar list derivation to reduce the total number of sequence codes, and when you create a simple loop on a yield expression, you use itImport Itertools#特点是: Efficient, most interesting: Islice
;>> from Itertools Import count>>> counter = count (start=13)>>> Next (counter)13>>> Next (counter)14
To generate an infinite sequence from a finite sequence:
>>> from Itertools Import cycle>>> colors = cycle ([' Red ', ' white ', ' Blue '])>>> Next (colors)' Red '>>> Next (colors)' White '>>> Next (colors)' Blue '>>> Next (colors)' Red '
To generate a finite sequence from an infinite sequence:
>>> from Itertools import islice>>> colors = cycle ([
for this category include:
accumulate() chain()/chain.from_iterable() compress() dropwhile()/filterfalse()/takewhile() groupby() islice() starmap() tee() zip_longest()
Here we will not illustrate all the methods one by one. If you want to know the usage of a method, print (method. _ doc _). After all, the itertools module provides a shortcut without any esoteric algorithms. Here we will illustrate the following methods that I find interest
Itertools import count >>>wmyl88.com counter = count (start=13) >>> Next (counter) 13 >>> Next (counter) 14To generate an infinite sequence from a finite sequence:>>> from itertools import cycle >>> colors = cycle ([' Red ', ' white ', ' Blue ') >>> Next (colors) ' Red ' >>> Next (colors) ' White ' >>> Next (colors) ' Blue ' >>> Next (colors) ' red 'To generate a finite sequence from an infinite sequence:>>> from itertools import islice >>> colors =
#利用生成器生成一个可迭代对象#需求: Generates an iterative object, outputs a prime number within a specified range, and uses the generator to produce an iterative object#生成器: It is iterative in itself, but yield is like return, yield returns, function freezes state when re-tuned, starting from frozen state1 classPrintnumbers (object):2 """docstring for Printnumbers"""3 def __init__(self, start,end):4Self.start =Start5Self.end =End6 defIsprimenum (self,num):7 ifnum==2:8 returnFals
operation. The most common zip function belongs to this category, only zip is a built-in function. Complete methods for this category include:
accumulate() chain()/chain.from_iterable() compress() dropwhile()/filterfalse()/takewhile() groupby() islice() starmap() tee() zip_longest()
Here we will not illustrate all the methods one by one. If you want to know the usage of a method, print (method. _ doc _). After all, the itertools module p
what the function returns are iterator objects.Generate an infinite sequence:>>> from itertools import count>>> counter = count (start=13) >>> Next (counter) 13>>> Next (counter) 14To generate an infinite sequence from a finite sequence:>>> from itertools import cycle>>> colors = cycle ([' Red ', ' white ', ' Blue ']) >>> next (colors) ' Red ' >>> Next (colors) ' White ' >>> Next (colors) ' Blue ' >>> Next (colors) ' red 'To generate a finite sequence from an infinite sequence:>>> from itertool
returned iterator is an entry that returns an input iterator based on the index selectedCreates an iterator that generates an item in a way similar to the slice return value: iterable[start:stop:step], skips the previous start item, iterates over the stop at the specified stop, and step specifies the stride used to skip the item. Unlike slices, negative values are not used for any start,stop and step, and if Start is omitted, the iteration will start at 0 and if step is omitted, the stride will
When programming python, it is often necessary to skip the first line to read the contents of the file. It is easier to think of setting a line_num for each row, and then determining whether Line_num is 1, and if not equal to 1, reads. The corresponding Python code is as follows:[Python]View PlainCopy
Input_file = open ("c:\\python34\\test.csv")
Line_num = 0
For line in Islice (Input_file, 1, None):
Line_num + = 1
if (line_
function returns are iterator objects.Generate an infinite sequence: from Import count>>> counter = count (start=13)>>> Next (counter)13>>> Next (counter)14To generate an infinite sequence from a finite sequence:>>> fromItertoolsImportCycle>>> colors = Cycle (['Red',' White','Blue'])>>>Next (colors)'Red'>>>Next (colors)' White'>>>Next (colors)'Blue'>>>Next (colors)'Red'To generate a finite sequence from an infinite sequence:>>> fromItertoolsImportIslice>>> colors = Cycle (['Red',' White','Blue'
In Python programming, you often need to skip the first line to read the contents of a file. It is easy to think of setting a line_num for each line, and then determining if Line_num is 1, and if not equal to 1, read the operation. The corresponding Python code is as follows:
Input_file = open ("Test.csv")
line_num = 0
for line in Islice (Input_file, 1, None):
line_num + = 1
if (line _num!= 1):
do_readline () However, thi
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.