I have a problem with the joint use of for loops and chunks. I have a list of 300 origins and another list with 300 destinations (30 in the example) and I want to contrast each of the origins with each of the destinations. To do this, first of all, I created a loop of origins and nested the one with the destinations and until here everything works correctly.
origins = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29]
destinations = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29]
for o in origins:
for d in destinations:
print('Origin: ' + str(o) + 'destination: ' + str(d))
The problem is that for operational reasons I should be running the matrix in parts of no more than 10 origins and no more than 4 destinations. (That is, origins from 0 to 9, with destinations from 0 to 3, then from 4 to 7 ... etc. For this I have created origin_chunks by dividing the list by 10 origins, and destination chunks by dividing the list by 4 destinations. You can see in the following code.
def chunk(x,n):
for i in range(0,len(x),n):
yield x[i:i+n]
origins = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29]
destinations = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29]
origin_chunks = chunk(origins,10)
destination_chunks = chunk(destinations,4)
for oc in origin_chunks:
print ('Inside big chunk')
print ('oc is: ' + str(oc))
for dc in destination_chunks:
print('Inside little chunk')
print ('oc is: ' + str(oc) + 'dc is: ' + str(dc))
print ('little chunk broken')
print('big chunk broken')
The problem that arises is that I get to enter the outer loop (origins from 0 to 9) and contrast it with all the destinations (0-3), (4-7) ... of the nested loop. However, when iterating a second time with the origins (10-19) the code does not rerun the nested for loop. I'm new to programming, in fact this is my first visit, and this is something that surprises me because in the example above it does not happen.
I thank you very much if anyone can give me the solution.