Choose without repetition:
You can use random.sample
to avoid repetition:
indexes = random.sample(range(12), 12)
This will give you a list of 12 random indexes (from 0 to 11)
Adding to list:
Use append
to add an item to list. (Whether item is a string or an integer)
group1.append(z[xy])
group1.append(x[xy]))
This will add z[xy]
and x[xy]
to the group 1 (As separate elements).
Formatting string:
If you want to have both student name and his/her level as one item, use string formatting.
group1.append("{} {}".format(z[xy], x[xy]))
"{} {}".format(z[xy], x[xy])
part will create a string by replacing {}
s with items inside format
input, respectively.
For example one result could be "Samuel 4"
Working code examples
Grouping with fixed size:
If size of each group should be exactly half (6), with fair skills, you can skip randomness. You can sort the students by their level and then starting from bottom add one to group one and one to group two and so one. (Or assign even numbers to group 1 and odd ones to group 2.
import random
class student:
def __init__(self, lvl, name):
self = lvl
self.name = name
Omer = student(3, "omer")
Michael = student(1, "Michael")
Sami = student(4, "sami")
Umar = student(5, "Umar")
Samuel = student(4, "Samuel")
Badmos = student(5, "Badmos")
Nabil = student(2, "Nabil")
Inacio = student(1, "Inacio")
Jesse = student(2, "student")
Ameerah = student(2, "Ameerah")
Bilal = student(2, "Bilal")
Joseph = student(2, "Joseph")
students = [Omer, Michael, Sami, Umar, Samuel, Badmos, Nabil,
Inacio, Jesse, Ameerah, Bilal, Joseph]
# Sort students by their level
students_sorted = sorted(students, key=lambda stu: stu.lvl)
# Create a list where each element contains a string of student name and level
students_with_names = ["{}: {}".format(stu.name, stu.lvl) for stu in students]
# Choose even ones for group1 and odd ones for group2
group1 = students_with_names[0::2]
group2 = students_with_names[1::2]
print(group1)
print(group2)
Grouping with variant size:
If group size is not important, you can shuffle students, then going over each student assign him/her to the group with lower sum of level.
import random
class student:
def __init__(self, lvl, name):
self = lvl
self.name = name
Omer = student(3, "omer")
Michael = student(1, "Michael")
Sami = student(4, "sami")
Umar = student(5, "Umar")
Samuel = student(4, "Samuel")
Badmos = student(5, "Badmos")
Nabil = student(2, "Nabil")
Inacio = student(1, "Inacio")
Jesse = student(2, "student")
Ameerah = student(2, "Ameerah")
Bilal = student(2, "Bilal")
Joseph = student(2, "Joseph")
# Create a list of students
students = [Omer, Michael, Sami, Umar, Samuel, Badmos, Nabil,
Inacio, Jesse, Ameerah, Bilal, Joseph]
number_of_students = len(students)
# Create random indexes
random_indexes = random.sample(range(number_of_students), number_of_students)
group1 = []
group2 = []
# Keep sum of each group skills
group1_skill = 0
group2_skill = 0
for index in random_indexes:
# If sum of group skills are the same or sum of group 1 is lower
if group1_skill == group2_skill or group1_skill < group2_skill:
group1.append("{}:{}".format(students[index].name, students[index].lvl))
# Update group1 sum of skills
group1_skill = group1_skill + students[index].lvl
else:
group2.append("{}:{}".format(students[index].name, students[index].lvl))
# Update group2 sum of skills
group2_skill = group2_skill + students[index].lvl
print(group1)
print(group2)