I really need help with this one | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I really need help with this one

I have 4 lists like this a=[4,9,2] b=[7,8,10] c=[1,3,11] d=[0,5,6] From these 4 lists I want to create one that has one number from a, then one number from b, then one from c, and finally one from d firstnumber= a [random.randint(0,2)] secondnumer= b [random.randint(0,2)] thirdnumber=c[random.randint(0,2)] fourthnumber=d [random.randint(0,2)] randomlist=[firstnumber,secondnumber,thirdnumber,fourthnumber] Now, until this point I have no problem, everytime I run the program I get one possible combination of a list that meets these requirements. But what if I wanted to get all possible combinations of randomlist? How can I code that? Ive tried with combination within the itertools module but I had no success. If anyone could help me it would be great. Thanks

8th Apr 2019, 12:35 PM
Germán Chrystan
Germán Chrystan - avatar
2 Answers
0
Did you try permutations from itertools? It works better for getting all possible combinations than combinations itself. If I think that is what you meant. I wish you success!
8th Apr 2019, 12:46 PM
Luis Sepúlveda
Luis Sepúlveda - avatar
0
The way you're creating new list as [a,b,c,d] and by a,b,c,d, I mean one element from each at defined index, the easiest way I could think of is using nested for loops a= [4,9,2] b= [7,8,10] c= [1,3,11] d= [0,5,6] perm =[] for i in a: for j in b: for k in c: for l in d: perm.append([i,j,k,l]) for each in perm: print(each) But again, it's not feasible if lists are bigger or having more lists.
8th Apr 2019, 2:40 PM
Шащи Ранжан
Шащи Ранжан - avatar