I have following list that contains string
data=[]
data.append('BBBB')
data.append('AAAA')
data.append('CCCC')
How can i make my code print the following result? Each line cannot have repeated values like BBBBBBBB
.
BBBB
AAAA
CCCC
BBBBAAAA
BBBBCCCC
AAAABBBB
... snippet ...
tersrth
8891 gold badge7 silver badges19 bronze badges
3 Answers 3
Here is a one line solution:
>>> from itertools import chain, permutations
>>> print(*chain(*(map("".join, permutations(data, i)) for i in range(1, 3))), sep="\n")
BBBB
AAAA
CCCC
BBBBAAAA
BBBBCCCC
AAAABBBB
AAAACCCC
CCCCBBBB
CCCCAAAA
answered Jun 12, 2020 at 13:05
Sign up to request clarification or add additional context in comments.
Comments
This code might help you out,
from itertools import permutations as per
input_nos = int(input('the number of strings')) #for specifying the input iterations
strings = []
for i in range(input_nos):
strings.append(input())
strings_per = per(strings, input_nos -1 ) # as specified
for i in strings_per:
print(''.join(i))
answered Jun 12, 2020 at 13:03
Comments
strings_list = ['AAAA', 'BBBB', 'CCCC', 'DDDD', 'EEEE']
def permute_string(a):
b = []
b.extend(a)
for i in range(len(a)-1):
for j in range(i+1, len(a)):
b.append(a[i] + a[j])
return b
permute_string(strings_list)
Based on what information you have give, this should do your work.
Comments
lang-py