Python iterator toolkit [recommended]

Source: Internet
Author: User
The iterator tool is very convenient and efficient when generating data. After mastering these basic methods, more iterator tools can be obtained through a simple combination. Original article: https://git.io/pytips

0x01 introduces the concept of iterator, that is, objects defining the _ iter _ () and _ next _ () methods, you can also use yield to simplify the definition of "iteratable objects". In some functional programming languages (see functional programming in Python 0x02, similar iterators are often used to generate lists (or sequences) of specific formats. In this case, the iterator is more like a data structure than a function (of course, in some functional programming languages, there is no essential difference between the two ). Python draws on some constructor methods in APL, Haskell, and SML for implementation in itertools (this module is implemented through C, source code:/Modules/itertoolsmodule. c ).

 The itertools module provides the following three types of iterator building tools:

Unlimited Iteration

Integrate two-series iterations

Combined Generator

 1. Infinite Iteration

The so-called infinite (infinite) means that if you iterate on it through the syntax of for... in..., it will fall into an infinite loop, including:

  

count(start, [step])  cycle(p)  repeat(elem [,n])

We can probably guess their usage by name. Since it is an infinite iteration, we naturally don't want To take out all its elements in an iterative manner, but usually it is combined with map/zip methods, use it as an inexhaustible Data Warehouse and combine it with finite-length iteratable objects:

  

from itertools import cycle, count, repeatprint(count.__doc__)  count(start=0, step=1) --> count object  Return a count object whose .__next__() method returns consecutive values.  Equivalent to:  def count(firstval=0, step=1):  x = firstval  while 1:  yield x  x += step  counter = count()  print(next(counter))  print(next(counter))  print(list(map(lambda x, y: x+y, range(10), counter)))  odd_counter = map(lambda x: 'Odd#{}'.format(x), count(1, 2))  print(next(odd_counter))  print(next(odd_counter))  0  1  [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]  Odd#1  Odd#3  print(cycle.__doc__)  cycle(iterable) --> cycle object  Return elements from the iterable until it is exhausted.  Then repeat the sequence indefinitely.  cyc = cycle(range(5))  print(list(zip(range(6), cyc)))  print(next(cyc))  print(next(cyc))  [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 0)]  1  2  print(repeat.__doc__)  repeat(object [,times]) -> create an iterator which returns the object  for the specified number of times. If not specified, returns the object  endlessly.  print(list(repeat('Py', 3)))  rep = repeat('p')  print(list(zip(rep, 'y'*3)))  ['Py', 'Py', 'Py']  [('p', 'y'), ('p', 'y'), ('p', 'y')]

  2. Integrate two-series iterations

The so-called integration of two sequences refers to taking two finite sequences as input and then returning them as an iterator after the integration 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 provides a shortcut without any esoteric algorithms. Here we will illustrate the following methods that I find interesting.

  

From itertools import cycle, compress, islice, takewhile, count # These three methods (if appropriate) can limit infinite iterations # print (compress. _ doc _) print (list (compress (cycle ('py'), [1, 0, 1, 0]) # image operation list l [start: stop: step] operate other sequences in the same way # print (islice. _ doc _) print (list (islice (cycle ('py'), 0, 2) # Restricted filter # print (takewhile. _ doc _) print (list (takewhile (lambda x: x <5, count () ['P', 'P'] ['P ', 'y'] [0, 1, 2, 3, 4] from itertools import groupby from operator import itemgetter print (groupby. _ doc _) for k, g in groupby ('aabbc'): print (k, list (g) db = [dict (name = 'python ', script = True), dict (name = 'C', script = False), dict (name = 'C ++ ', script = False ), dict (name = 'Ruby ', script = True)] keyfunc = itemgetter ('script') db2 = sorted (db, key = keyfunc) # sorted by 'script' for isScript, langs in groupby (db2, keyfunc): print (','. join (map (itemgetter ('name'), langs) groupby (iterable [, keyfunc])-> create an iterator which returns (key, sub-iterator) grouped by each value of key (value ). A ['A', 'a'] B ['B', 'B'] C ['C'] C, c ++ python, ruby from itertools import zip_longest # the built-in function zip is merged Based on the shorter sequence. # zip_longest is based on the longest sequence, in addition, the parameter fillvalue # izip_longest print (list (zip_longest ('abcd', '123', fillvalue = 0) in Python 2.7 is provided. [('A ', '1'), ('B', '2'), ('C', '3'), ('D', 0)]

  3. Combined Generator

Arrangement and combination of generators:

Product (* iterables, repeat = 1): Cartesian product permutations (iterable, r = None) of two input sequences: combinations (iterable, r ): ordered version combinations_with_replacement (iterable, r): Cartesian product of ordered version from itertools import product, permutations, combinations, combinations_with_replacement print (list (product (range (2 ), range (2) print (list (product ('AB', repeat = 2) [(0, 0), (0, 1), (1, 0), (1, 1)] [('A', 'A'), ('A', 'B'), ('B', 'A '), ('B', 'B')] print (list (combinations_with_replacement ('AB', 2) [('A', 'A'), ('A ', 'B'), ('B', 'B')] # horse racing question: Arrangement and combination of the first two of the four horses (A ^ 4_2) print (list (permutations ('abcde', 2) [('A', 'B'), ('A', 'C'), ('A ', 'd), ('A', 'E'), ('B', 'A'), ('B', 'C'), ('B', 'C'), ('B ', 'd), ('B', 'E'), ('C', 'A'), ('C', 'B'), ('C', 'B'), ('C ', 'D'), ('C', 'E'), ('D', 'A'), ('D', 'B'), ('D ', 'C'), ('D', 'E'), ('E', 'A'), ('E', 'B'), ('E ', 'C'), ('E', 'D')] # color ball problem: the four colors of the ball can be extracted from any two color combinations (C ^ 4_2) print (list (combinations ('abcd', 2) [('A', 'B'), ('A', 'C'), ('A ', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')]

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.