0

So i have this code below that i created. This code will list every possible combination of a certain value, in this example is "a" "b" "c" "d". If i use this code, the result would be like this: a, aa, ab, ac, ad, aaa, aab, aac, aad, aba, abb, abc, etc. How to simplify this for loop code, so that i can input more values without createing more for loop?

n = "abcd"

for c in n:
    print(c)
    for c1 in n:
        print(c+c1)
        for c2 in n:
            print(c+c1+c2)
            for c3 in n:
                print(c+c1+c2+c3)
5

4 Answers 4

2

https://docs.python.org/2/library/itertools.html

Itertools permutations and combinations is what you're after.

itertools.permutations([1, 2, 3])
Sign up to request clarification or add additional context in comments.

Comments

2
from itertools import combinations
n = "abcd"
print ([''.join(l) for i in range(len(n)) for l in combinations(n, i+1)])

output:

['a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'bc', 'bd', 'cd', 'abc', 'abd', 'acd', 'bcd', 'abcd']

edit:

from itertools import combinations_with_replacement

n = "abcd"
comb = []

for i in range(1, len(n)+1):
    comb += list(combinations_with_replacement(n, i))
print([''.join(c) for c in comb])

output:

['a', 'b', 'c', 'd', 'aa', 'ab', 'ac', 'ad', 'bb', 'bc', 'bd', 'cc', 'cd', 'dd', 'aaa', 'aab', 'aac', 'aad', 'abb', 'abc', 'abd', 'acc', 'acd', 'add', 'bbb', 'bbc', 'bbd', 'bcc', 'bcd', 'bdd', 'ccc', 'ccd', 'cdd', 'ddd', 'aaaa', 'aaab', 'aaac', 'aaad', 'aabb', 'aabc', 'aabd', 'aacc', 'aacd', 'aadd', 'abbb', 'abbc', 'abbd', 'abcc', 'abcd', 'abdd', 'accc', 'accd', 'acdd', 'addd', 'bbbb', 'bbbc', 'bbbd', 'bbcc', 'bbcd', 'bbdd', 'bccc', 'bccd', 'bcdd', 'bddd', 'cccc', 'cccd', 'ccdd', 'cddd', 'dddd']

5 Comments

But it has wrong output where aaaa, bbbb, cccc, dddd, etc.
I think OP is asking for permutations instead of combinations, as he expects 'aba' and 'aab'
Yeah, he is looking for permutations with repetition.
Sorry, my bad...yeah i mean permutaions with repetition
Almost there, but it seems combinations_with_replacement does not include results like "ba" "ca" "cb" etc
1

To get the same result of 340 elements you can use itertools.product:

product(n, repeat=1)
product(n, repeat=2)
...
product(n, repeat=4)

To print the result you can use the following loop:

from itertools import product
n = "abcd"

for i in range(1, 5):  
    for prod in product(n, repeat=i): 
        print(''.join(prod)) 

To get an extra level you can easily increase the 5 in range. Note: the order of printing is slightly different than in your code.

Comments

1

You can use itertools module to solve your problem. You need combinations_with_replacement function with all possible lengths:

import itertools as it

n = "abcd"
result = []

for l in range(len(n)):
    result += list(it.combinations_with_replacement(n, l+1))
print(result)

[('a',), ('b',), ('c',), ('d',), ('a', 'a'), ('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'b'), ('b', 'c'), ('b', 'd'), ('c', 'c'), ('c', 'd'), ('d', 'd'), ('a', 'a', 'a'), ('a', 'a', 'b'), ('a', 'a', 'c'), ('a', 'a', 'd'), ('a', 'b', 'b'), ('a', 'b', 'c'), ('a', 'b', 'd'), ('a', 'c', 'c'), ('a', 'c', 'd'), ('a', 'd', 'd'), ('b', 'b', 'b'), ('b', 'b', 'c'), ('b', 'b', 'd'), ('b', 'c', 'c'), ('b', 'c', 'd'), ('b', 'd', 'd'), ('c', 'c', 'c'), ('c', 'c', 'd'), ('c', 'd', 'd'), ('d', 'd', 'd')]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.