All possible combinations for 3 digits of list never the same

前端 未结 3 674
温柔的废话
温柔的废话 2021-01-17 01:57

I have a list that looks like:

A
B
C
D
E
F
G

How do I solve this to find all combinations for 3 digits. The same letter cannot be used in

3条回答
  •  囚心锁ツ
    2021-01-17 02:34

    combinations works on strings not lists, so you should first turn it into a string using: ''.join(x)

    from itertools import combinations
    x = ['a', 'b', 'c', 'd', 'e']
    n = 3
    aa = combinations(''.join(x), n)
    for comb in aa:
        print(''.join(comb))
    

    OUTPUT

    abc
    abd
    abe
    acd
    ace
    ade
    bcd
    bce
    bde
    cde
    

    Or as a one-liner:

    [''.join(comb) for comb in combinations(''.join(x), n)]
    

提交回复
热议问题