Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python return combinations of a list of ranges?

I'm working on returning all the combinations of a list based on a list of ranges.

So if I have:

test_list = [range(0,11,1),range(0,11,2),range(0,11,5)]

And I want to return a list with all the possible combinations based on the ranges. For Example:

output_list[[0,0,5],[0,0,10],[0,2,0],[0,4,0],[0,6,0].......]

But all I have been able to do is:

import itertools

test_list = [range(0,11,1),range(0,11,2),range(0,11,5)]
output_list = []
for i in itertools.permutations(test_list):
    if i not in output_list:
        output_list.append(i)

Which returns each range permuted,(a list of ranges again)?

like image 226
tijko Avatar asked Oct 19 '25 08:10

tijko


1 Answers

output_list = list(itertools.product(*test_list))
like image 117
dave Avatar answered Oct 21 '25 23:10

dave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!