#-*-Coding:utf-8-*-
# python:2.x
__author__ = ' Administrator '
#生成器表达式和itertools模块
Parentheses can be used instead of brackets in the #yield
iter0= (x**2 for x in range (ten) if x%2==0)
For Iter1 in Iter0:
Print Iter1
#结果
"""
0
4
16
36
64
"""
#这样的表达式被称为生成器或者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 it
Import Itertools
#特点是: Efficient, most interesting: Islice,tee,groupby these 3 methods
print '-' *30
#islice窗口迭代器
"""
Itertools.islice (iterable, stop)
Itertools.islice (iterable, Start, stop[, step])
"""
# def Islice1 ():
# Value=raw_input (). Strip ()
# while value!= ':
# for El in Itertools.islice (Value.split (), 4,none): #表示从第5行开始每行元素 as long as this line exceeds 4 elements
# yield El
# Value=raw_input (). Strip ()
# Iter11=islice1 ()
# Print Iter11.next ()
#使用场景: When you need to extract specific location data in a stream, you can use islice, such as when you might be using a record-specific format file, or when you are performing metadata-enclosing traffic
#tee往返式迭代器
"""
Tee provides the ability to run multiple iterator patterns on top of a sequence, and if the first run information is provided, it can help us to run it again based on that data
"""
def tee1 (iters,headsize=1):
A,b=itertools.tee (Iters)
Return list (Itertools.islice (a,headsize)), b
Print tee1 (' abcdef ') # ([' A '], <itertools.tee object at 0x0000000002347c08>)
Print tee1 (' ABCD ', 4) # ([' A ', ' B ', ' C ', ' d '], <itertools.tee object at 0x0000000002427b88>)
#print tee1 (' abc ', -2) valueerror:indices for Islice () must is None or an integer:0 <= x <= maxint.
#当tee生成两个迭代器, then the first iterator gets the first headsize element of the iteration by Islice, and returns as a normal list, and the second iterator returns the element that is a new iterator that can work over the entire sequence
#groupby: Uniq iterators
#它可以对来自一个迭代器重复元素进行分组, as long as they are adjacent, you can also provide another function to perform an element comparison, otherwise the identifier will be used to compare
def groupbu1 (data):
Return (LEN (list data), name) \
For name,g in Itertools.groupby (data))
def groupbu2 (data):
Return (Car*size for size,car in data)
Print List (groupbu1 (' Get uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu '))
#[(A, ' G '), (A, ' E '), (A-V, ' t '), (the ","), (A, ' u ')
C=GROUPBU1 (' Get Uuuuuuuuuuuuuuuuuuuuuuuuuuup ')
print '. Join (GROUPBU2 (c)) #gggggggggggggggggggggggggggggggg Eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
#其他说明: Compression algorithm
"""
Rel compresses the data, and each set of contiguous strings in the string is replaced with the string itself and the number of repetitions, or 1 if the string is not duplicated.
If the compression algorithm has an algorithm, you can consider the LZ77 algorithm, which is the RLE enhanced version, will want to find the same pattern adjacent, not the same character,
The website is: http://en.wikipedia.org/wiki/LZ77
"""
"""
When you need to complete a summary on the data, you can use Groupty, this time, the built-in sorted function is particularly useful, you can pass in the data to believe the elements adjacent
"""
#itertools其他说明及文档
#文档: Https://docs.python.org/2/library/itertools.html?highlight=itertools#itertools