select elements randomly under a specific condition | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

select elements randomly under a specific condition

I am completely biggener. I've made a WebApp using django to organize a class room. I want to generate random work groups of 2 students where the two students don't talk the same first_languge. Evrey thing Works, but still have results don't match that condition. this is the code that I have done: from students.models import students import random #make a list of all students a =list(students.objects.all()) #the select function def group (x,y): res = [] for i in range(y): while len(res)<y: res = random.sample(x, k=y) while x[i].first_language == x[i-1].first_language: group(x,y) return None return res #multiple call for the function for n in range(10): p = group(a,2) print(p) if i call the function multiple times i get groups from students with the same mother language, and this is wrong. https://code.sololearn.com/c9XeVe9wknkq/?ref=app

16th Feb 2019, 6:14 PM
Ahmad Ali
Ahmad Ali - avatar
1 Answer
+ 3
Jay Matthews I've tried it, and the same results happened
16th Feb 2019, 6:58 PM
Ahmad Ali
Ahmad Ali - avatar