1

I have 4 arrays like this:

temp1 = ['a' , 'b' , 'c']
temp2 = ['d' , 'e' ,'' ]
temp3 = ['f']
temp4 = ['g']

I want output:

adfg
aefg
afg
bdfg
befg
bfg
cdfg
cefg
cfg

and I solved it using following:

temp1 = ['a' , 'b' , 'c']
temp2 = ['d' , 'e' ,'' ]
temp3 = ['f']
temp4 = ['g']
for list_1 in temp1:
    for list_2 in temp2:
        for list_3 in temp3:
            for list_4 in temp4:
                temp_list = ''
                if list_1: temp_list += list_1
                if list_2: temp_list += list_2
                if list_3: temp_list += list_3
                if list_4: temp_list += list_4
                print "%s " %(temp_list)

but I think my code isn't efficient.

How to make good algorithm and make it efficient.

and how about if temp3 is null for example :

temp1 = ['a' , 'b' , 'c']
temp2 = ['d' , 'e' ,'' ]
temp3 = []
temp4 = ['g']

3 Answers 3

5

You can use itertools.product:

>>> from itertools import product
>>> result = product(temp1, temp2, temp3, temp4)
>>> ["".join(item) for item in result]
['adfg', 'aefg', 'afg', 'bdfg', 'befg', 'bfg', 'cdfg', 'cefg', 'cfg']

UPDATE:

If temp3 is empty as in the updated question, I think you would like to skip it while generating the results. If that's the case, you can only use the list which have some items:

>>> input_lists = [arr for arr in (temp1, temp2, temp3, temp4) if arr]
>>> result = product(*input_lists)
>>> ["".join(item) for item in result]
['adg', 'aeg', 'ag', 'bdg', 'beg', 'bg', 'cdg', 'ceg', 'cg']
Sign up to request clarification or add additional context in comments.

5 Comments

how about if the array null ??? temp1 = ['a' , 'b' , 'c'] temp2 = ['d' , 'e' ,'' ] temp3 = [] temp4 = ['g'] output will be []
Yes, it will return [] and that is the expected behavior of this function. What do you expect the output to be if there is an empty array?
I updated the answer in case of empty temp3 if that's the output which is expected.
yes , i think it must be filter with 'if' but thank you
The answer you have accepted would result in [] if temp3 is empty.
1

Use itertools.product().

>>> import itertools
>>> result = [''.join(i) for i in itertools.product(temp1,temp2,temp3,temp4)]
>>> result
['adfg', 'aefg', 'afg', 'bdfg', 'befg', 'bfg', 'cdfg', 'cefg', 'cfg']

Comments

0

Use Python Comprehensions. They are cleaner and more elegant.

temp1 = ['a' , 'b' , 'c']
temp2 = ['d' , 'e' ,'' ]
temp3 = ['f']
temp4 = ['g']

templist = [i+j+k+l for i in temp1 for j in temp2 for k in temp3 for l in temp4] 
# Result - ['adfg', 'aefg', 'afg', 'bdfg', 'befg', 'bfg', 'cdfg', 'cefg', 'cfg']

i+j+k+l helps to concatenate strings. Whenever you intend to use items from lists, try using comprehensions.

You can check this out http://www.pythonforbeginners.com/basics/list-comprehensions-in-python

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.