|
1 | 1 | # Itertools.permutation()
|
2 |
| -# Itertools.permutation() function falls under the Combinatoric Generators. |
3 |
| -# The recursive generators that are used to simplify combinatorial constructs such as permutations, combinations, |
| 2 | +# Itertools.permutation() function falls under the Combinatoric Generators. |
| 3 | +# The recursive generators that are used to simplify combinatorial constructs such as permutations, combinations, |
4 | 4 | # and Cartesian products are called combinatoric iterators
|
5 | 5 |
|
6 |
| -# The word “Permutation” it refers to all the possible combinations in which a set or string can be ordered or arranged. |
7 |
| -# Similarly here itertool.permutations() method provides us with all the possible arrangements |
8 |
| -# that can be there for an iterator and all elements are assumed to be unique on the basis of their position |
9 |
| -# and not by their value or category. |
| 6 | +# The word “Permutation” it refers to all the possible combinations in which a set or string can be ordered or arranged. |
| 7 | +# Similarly here itertool.permutations() method provides us with all the possible arrangements |
| 8 | +# that can be there for an iterator and all elements are assumed to be unique on the basis of their position |
| 9 | +# and not by their value or category. |
10 | 10 |
|
11 |
| -# All these permutations are provided in lexicographical order. |
12 |
| -# The function itertool.permutations() takes an iterator |
13 |
| -# and ‘r’ (length of permutation needed) as input and |
14 |
| -# assumes ‘r’ as default length of iterator |
15 |
| -# if not mentioned and returns all possible permutations of length ‘r’ each. |
| 11 | +# All these permutations are provided in lexicographical order. |
| 12 | +# The function itertool.permutations() takes an iterator |
| 13 | +# and ‘r’ (length of permutation needed) as input and |
| 14 | +# assumes ‘r’ as default length of iterator |
| 15 | +# if not mentioned and returns all possible permutations of length ‘r’ each. |
16 | 16 |
|
17 | 17 | # Syntax:
|
18 | 18 | # Permutations(iterator, r)
|
19 | 19 |
|
20 | 20 | # Example:
|
21 |
| -from itertools import permutations |
22 |
| -name="HASH" |
23 |
| -p=permutations(name) # it return permutation object <itertools.permutations object at 0x000001DED0B63100> |
| 21 | +from itertools import permutations |
| 22 | + |
| 23 | +name = "HASH" |
| 24 | +p = permutations( |
| 25 | + name |
| 26 | +) # it return permutation object <itertools.permutations object at 0x000001DED0B63100> |
24 | 27 | for i in list(p):
|
25 |
| - print(i) # print each value inside the permutation object |
| 28 | + print(i) # print each value inside the permutation object |
26 | 29 |
|
27 | 30 |
|
28 | 31 | # Example:
|
29 |
| -print ("All the permutations of the given list is:") |
30 |
| -print (list(permutations([1, 'Spider'],2))) |
31 |
| -print() |
| 32 | +print("All the permutations of the given list is:") |
| 33 | +print(list(permutations([1, "Spider"], 2))) |
| 34 | +print() |
32 | 35 |
|
33 | 36 | # All the permutations of the given list is:
|
34 | 37 | # [(1, 'Spider'), ('Spider', 1)]
|
35 |
| - |
36 | 38 |
|
37 |
| -print ("All the permutations of the given string is:") |
38 |
| -print (list(permutations('PT'))) |
39 |
| -print() |
| 39 | + |
| 40 | +print("All the permutations of the given string is:") |
| 41 | +print(list(permutations("PT"))) |
| 42 | +print() |
40 | 43 |
|
41 | 44 | # All the permutations of the given string is:
|
42 | 45 | # [('P', 'T'), ('T', 'P')]
|
43 | 46 |
|
44 | 47 |
|
45 |
| -print ("All the permutations of the given container is:") |
46 |
| -print(list(permutations(range(3), 2))) # 2 is r -> refers to the length or dimension |
| 48 | +print("All the permutations of the given container is:") |
| 49 | +print(list(permutations(range(3), 2))) # 2 is r -> refers to the length or dimension |
47 | 50 |
|
48 | 51 | # If r and length is mentioned here the output is
|
49 | 52 | # All the permutations of the given container is (range(3), 2):
|
|
56 | 59 |
|
57 | 60 | # Example:
|
58 | 61 |
|
59 |
| -lst=["a","b","c"] |
| 62 | +lst = ["a", "b", "c"] |
60 | 63 |
|
61 | 64 | print("\nCombination of letters: ")
|
62 | 65 | for i in permutations(lst):
|
|
0 commit comments