How to use the itertools module of Python standard library
Introduction
Official description: Functional tools for creating and using iterators. That is, functions for creating efficient iterators.
itertools.chain(*iterable)
Return multiple sequences as a single sequence.
For example:
import itertools for each in itertools.chain('i', 'love', 'python'): print each
Output:
i l o v e p y t h o n
itertools.combinations(iterable, r)
Returns "combinations" of the specified length
For example:
import itertools for each in itertools.combinations('abc', 2): print each
Output:
('a', 'b') ('a', 'c') ('b', 'c')
itertools.combinations_with_replacement(iterable, r)
Returns a "combination" of the specified length, and the elements in the combination can be repeated
For example:
import itertools for each in itertools.combinations_with_replacement('abc', 2): print each
Output:
('a', 'a') ('a', 'b') ('a', 'c') ('b', 'b') ('b', 'c') ('c', 'c')
itertools.product(*iterable[,repeat])
Returns all combinations of the specified length, which can be understood as the Cartesian product
For example:
import itertools for each in itertools.product('abc', repeat=2): print each
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'b')
('b', 'c')
('c', 'a')
('c', 'b')
( 'c', 'c')
itertools.premutations(iteravle[,r])
Return a permutation of length r
For example:
import itertools for value in itertools.permutations('abc', 2): print value
Output:
('a', 'b') ('a', 'c') ('b', 'a') ('b', 'c') ('c', 'a') ('c', 'b')
itertools.compress(data,selector)
Returns the corresponding element of data whose selector is True
For example:
import itertools for each in itertools.compress('abcd', [1, 0, 1, 0]): print each
Output:
a c
itertools.count(start=0, step=1)
Returns a sequence starting with start and increasing step, infinitely increasing
For example:
import itertools for each in itertools.count(start=0, step=2): print each
Output:
1 2 3 . .
itertools.cycle(iterable)
Iterate the iterator infinitely
For example:
import itertools for each in itertools.cycle('ab'): print each
Output:
a b a b . .
itertools.dropwhile(predicate, iterable)
Until predicate is true, return iterable subsequent data, otherwise drop it
For example:
import itertools for each in itertools.dropwhile(lambda x: x<5, [2,1,6,8,2,1]): print each
Output:
6 8 2 1
itertools.groupby(iterable[,key])
Return a group (key, itera), key is the value of iterable, itera is all items equal to key
For example:
import itertools for key, vale in itertools.groupby('aabbbc'): print key, list(vale)
Output:
a ['a', 'a'] b ['b', 'b', 'b'] c ['c']
itertools. ifilter(predicate, iterable)
Returns an element iterator whose predicate result is True. If predicate is None, returns all items that are True in iterable
For example:
import itertools for value in itertools.ifilter(lambda x: x % 2, range(10)): print value
Output:
1 3 5 7 9
itertools.ifilterfasle(predicate,iterable)
Returns elements whose predicate is False. If predicate is None, returns all items in iterable that are False.
For example:
import itertools for value in itertools.ifilterfalse(lambda x: x % 2, range(10)): print value
Output:
0 2 4 6 8
itertools.imap(function,*iterables)
Equivalent to map() in iterator mode
For example:
import itertools for value in itertools.imap(lambda x, y: x+y, (1,2,3), (4,5,6)): print value
Output :
5 7 9
itertools.islice(iterable, start,stop[,step])
Equivalent to iterator mode slicing operation
For example:
import itertools for value in itertools.islice('abcdefg', 1, 4, 2): print value
Output:
b d
itertools.repeat(object,[,times])
Continuously return the object object. If times is specified, return times times
For example:
import itertools for value in itertools.repeat('a', 2): print value
Output:
a a
itertools.starmap(function,iterable)
Returns the value of function(iter), iter is the element of iterable
For example:
import itertools for value in itertools.starmap(lambda x, y: x * y, [(1, 2), (3, 4)]): print value
Output:
2 12
itertools.takewhile(predicate,iterable)
If predicate is true, return the iterable element, if it is false, it will not return, break.
For example:
import itertools for value in itertools.takewhile(lambda x: x < 5, [1, 3, 5, 6]): print value
Output:
1 3
The above is the detailed content of How to use the itertools module of Python standard library. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
