Is there a way to distribute more similarly than the method below? I'm also curious about how to distribute it from more than one list. The list can contain only five numbers.
intList = []
for i in range(10):
intList.append(random.randint(10,200))
listX = []
listY = []
intList.sort(reverse=True)
for i in range(len(intList)):
if sum(listX) >= sum(listY):
if len(listY) != 5:
listY.append(intList[i])
else:
listX.append(intList[i])
else:
if len(listX) != 5:
listX.append(intList[i])
else:
listY.append(intList[i])
print(f"listx = {listX} \nlisty = {listY}\n sum x = {sum(listX)}, y = {sum(listY)}")
You should sort the list of integers in descending order and then iterate through it, adding each number to the list with the smaller current sum, like the following:
Edit
To ensure that both lists have exactly 5 elements and maintain balanced lengths, you can modify the algorithm to distribute the numbers based on the lengths of the lists. If one list has fewer than 5 elements, prioritize it adding numbers to that list until it reaches 5 elements.
Try this: