I’ve looked around a bunch and found the itertools pacakge, but as far as I can tell, none of the functions do exactly what I need. I’ve started writing a function on my own, which I can do, but I wonder if anyone knows of a built-in function that could do all or part of this dual-list iterating?
What I want to do is iterate through two lists at once, compare a value from each of them, do something as a result of that comparison, and maybe stay on the same row of one list while moving on to the next row of the second list (based on how the comparison turned out). Here’s an example:
ListA= [[1, 7, 3],
[1, 12, 4],
[1, 9, 5]]
ListB= [[2, 2, 3],
[2, 2, 3],
[2, 5, 4]]
I want to go through each list row by row and compare the number in the final position. If they’re equal, I want to add the two second numbers. But I want to keep adding them as long as the third number in ListB is equal to the third number in ListA. Which means ListA may stay on the same row while ListB moves down a couple (which is why the itertools functions don’t work, because those all seem to chunk down each row of each list in tandem). So I want the output to look like this:
Iteration 1 ListOut= [[9, 3]]
Iteration 2 ListOut= [[11,3]]
Iteration 3 ListOut= [[11,3], [17, 4]]
Iteration 4 ListOut= [[11,3], [17, 4], [9, 5]]
You’d want to do something like this: