2

arr=['one','two','three']

Result must be like this: onetwo,twothree,onethree

itertools.permutations will not work in this situation.

we can do this by simply adding for loops and appending them ,that works for small arrays but takes time for big arrays.
I was wondering is there any way (like itertools.permutations)this can be achieved?

2 Answers 2

4

Perhaps what you wanted was the itertools.combinations?

>>> [''.join(comb) for comb in (itertools.combinations(arr, 2))]
['onetwo', 'onethree', 'twothree']
Sign up to request clarification or add additional context in comments.

1 Comment

Is there any way applying this to two different arrays.?I mean arr1=['name1','name2'];arr2=['name3','name4'].output should be name1name3,name1name4,name2name3,name3name4
1

for two lists

  • create a list with equal length compare with other list
  • zip new list with other list
  • put all sublist together
  • join list
from itertools import permutations

arr1=['name1','name2']
arr2=['name3','name4']

set( map(lambda x: ''.join(x),reduce( lambda x,y:x+y, [  zip(i,arr1) for i in permutations(arr2,len(arr1)) ] ) ) )

output:

set(['name3name1', 'name3name2', 'name4name1', 'name4name2'])

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.