1

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
asked Jun 12, 2020 at 12:51

3 Answers 3

1

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

0

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

0
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.

answered Jun 12, 2020 at 13:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.