I'd like to iterate over two lists simultaneously.
The first list is just a regular list of elements.
The second list is a list containing multiple lists, each containing elements which are in fact 3-tuples.
I want to perform some function on every element of every 3-tuple in every list inside the second list (if that's understandable). This function will be dependant on an element in the first list, and I'd like it all to run in order.
ie: If I have some list such as [0,1,2,3,1,5,6,7,1,2,3,5,1,1,2,3,5,6] as my first list, and [[(13,12,32),(11,444,25)],[(312,443,12),(123,4,123)],[(545,541,1),(561,112,560)]] as my second list, I'd like to perform such an operation such that it involves the first element of the first list and the first element of the first tuple of the first list of the second list. I'd like this operation to be iterated using the nth element of the first list, and the nth element of the the tuple bla bla
So if we're looking at what elements will be used together for the operation:
- 0 goes with 13
- 1 goes with 12
- 2 goes with 32
- 3 goes with 11
- 1 goes with 444
- 5 goes with 25
- 6 goes with 312
and so on.
What's happening is that my function will take the element from the second list, its paired element from list 1, use these bits somehow, and output a new value to replace its original second list counterpart. I then want to a construct a replacement/new 'second list' which is the second list after running this function (which uses the information as described above)
Hope this was semi-cohesive, I tried to be as informative as possible. Please inquire if anything is unclear.
Cheers, and thank you for any help you can provide!
Edit:
I want to take list 1 and list 2, then flatten list 2. I then want to iterate over both lists, using some function. I then want to construct a new list, which uses the outputs from my function, which will just be some 'adjusted' values for my flattened second list, but in the format of how it was originally, tuples in a list of list.
ie: my output could be something like:
[[(13,13,32),(11,444,24)],[(313,443,12),(123,4,123)],[(546,542,1),(561,112,561)]]
with some of the values either changing a little bit or remaining the same, as a result of being 'passed through' my function.
Edit#2:
The ticked solution shows exactly what was needed for this problem!
Edit#3:
A few fixes that are needed to the top solution:
How would I fix the code such that if the first list is empty, the output will just be the original second list?
And if the second list is empty, the output should just be an empty list.
Also if len(list 1) > len(list 2), it will just iterate until the end of list 2, then ignore the excess in list 1, outputting a new list of same format as the original list 2.
And if len(list 2) > len (list 1), it will just iterate for where list 1 can be matched up, but leave the remainder of list 2 intact, rather than chopping it off.
Can I perform all these fixes with some if/elseif/else loops?
EDIT: I'm still looking to fix these issues, which could be separated/postulated to be a separate query. I've posed a new question here https://stackoverflow.com/questions/19118641/making-a-function-only-run-for-certain-conditions-in-python if anyone is interested in helping out.
ziptogether with the first.