0

Im a beginner in Python that trying to enhance my problem solving skill but Im stuck here

Here is my simple code

import itertools

a = [1, 2, 3] 
b = [2, 3, 1] 
c = [3, 1, 2] 

p = ["Number {x} ,number {y} and number {z}"]

for phrase in p:
  for (x, y, z) in zip(a, b, c):
    t = phrase.format(x=x, y=y, z=z)
    print(t)

and the output

Number 1, number 2 and number 3
Number 2, number 3 and number 1
Number 3, number 1 and number 2

my question is how I will get all possible combination of a one list and iterate it through x,y,z? instead of doing it manually

for example here is my one list

a = [1, 2, 3]

and the output is the combination of the list (through the x,y,z in variable p)

Number 1, number 2 and number 3
Number 1, number 3 and number 2
Number 2, number 1 and number 3
Number 2, number 3 and number 1
Number 3, number 1 and number 2
Number 3, number 2 and number 1

thank you in advance.

dendenqy
  • 13
  • 3
  • Does this answer your question? [How to generate all permutations of a list?](https://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list) – chash Jun 05 '20 at 16:49

4 Answers4

1

Those sequences would be generated by itertools.permutations

for x,y,z in itertools.permutations(a):
    print(f"Number {x}, number {y} and number {z}")

Output

Number 1, number 2 and number 3
Number 1, number 3 and number 2
Number 2, number 1 and number 3
Number 2, number 3 and number 1
Number 3, number 1 and number 2
Number 3, number 2 and number 1
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
1

Your question is mainly based on how to generate all the permutations of a list, independently of the type of elements in that list. In Python there is a standard-library tool for this called itertools. Following the structure of your own code, here's how to work it out:

from itertools import permutations

a = [1, 2, 3]
p = ["Number {x}, number {y} and number {z}"]

for phrase in p:
    for (x, y, z) in permutations(a):
        t = phrase.format(x=x, y=y, z=z)
        print(t)

And the output:

Number 1, number 2 and number 3
Number 1, number 3 and number 2
Number 2, number 1 and number 3
Number 2, number 3 and number 1
Number 3, number 1 and number 2
Number 3, number 2 and number 1

Remark: one thing one can notice from above function, itertools.permutations(), is the order matters. However, if you want to understand how permutation works, here is an implementation example, without any library, by leveraging simply the recursive nature of permutation:

def all_perms(items_list):
    if len(items_list) <=1:
        yield items_list
    else:
        for item in all_perms(items_list[1:]): # Recursivity
            for i in range(len(items_list)):
                yield item[:i] + items_list[0:1] + item[i:]

Here is the output (order doesn't matter):

Number 1, number 2 and number 3
Number 2, number 1 and number 3
Number 2, number 3 and number 1
Number 1, number 3 and number 2
Number 3, number 1 and number 2
Number 3, number 2 and number 1

Note: This function, all_perms(), as well as itertools.permutations(), works in both string and list contexts.

Djeutsch
  • 73
  • 5
0

Just a quick add-on to @Cory Kramer's answer, if you want to select more than one item from the list at once you can pass the number as second argument to the method permutations do something like this :

from itertools import permutations
a = [1, 2, 3]
selection = 2
for x in permutations(a, selection):
    print(x)

OUTPUT:

(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
0

If you want to write your own function to calculate permutations, you will probably end up with something like this, where you loop over all the possible items to put first, and then call back to the same function recursively to get the possible permutations for the remaining items.

def get_permutations(lst):
    n = len(lst)
    if n == 1:
        return [lst]
    perms = []
    for orig_index, value in enumerate(lst):
        others = lst[:orig_index] + lst[orig_index + 1:]
        for perm_of_others in get_permutations(others):
            perms.append([value] + perm_of_others)
    return perms

for a, b, c in get_permutations([1, 2, 3]):
    print(f"Number {a}, number {b} and number {c}")
alani
  • 12,573
  • 2
  • 13
  • 23