itertools.permutations(itr, r=None)
-
The
permutationsfunction is used to:- Cycle through each element of an iterable
- Return successive
rlength permutations of elements
- This iterable is based on the
itrparameter - The number of permutations is based on the
rparameter - The
rparameter is optional and defaults to the length of the iterable permutationsreturns an iterator of successiverlength permutations of elements in the iterableitr
Illustrating the permutations Function
>>> from itertools import permutations
>>> itr = permutations('ABC', 2)
>>> for i in itr: print(i)
('A', 'B')
('A', 'C')
('B', 'A')
('B', 'C')
('C', 'A')
('C', 'B')
>>> itr = permutations(range(3))
>>> for i in itr: print(i)
(0, 1, 2)
(0, 2, 1)
(1, 0, 2)
(1, 2, 0)
(2, 0, 1)
(2, 1, 0)References
Previous
Next