Issue
My code
cummulative_num = [['1', '4', '5', '7'], ['2', '5', '6', '9']]
if len(cumulative_num)==1:
print(*cumulative_num[0])
out=""
if len(cumulative_num)==2:
for i in cumulative_num[0]:
for j in cumulative_num[1]:
out+= i+j+ " "
print(out)
out=""
if len(cumulative_num)==3:
for i in cumulative_num[0]:
for j in cumulative_num[1]:
for k in cumulative_num[2]:
out+= i+j+k+ " "
print(out)
output : 12 15 16 19 42 45 46 49 52 55 56 59 72 75 76 79
The output order should be each first array element is added to every element in the second array
Please provide me a better approach when the array in cumulative_num increases.
Solution
Something like this could work:
The *
unpacks the lists into product, no matter how many sublists you may have.
from itertools import product
cummulative_num = [['1', '4', '5', '7'], ['2', '5', '6', '9']]
' '.join([''.join(x) for x in product(*cummulative_num)])
Output
'12 15 16 19 42 45 46 49 52 55 56 59 72 75 76 79'
Answered By - Chris
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.