G'day!
So I have a function which is taking the elements from two lists, the first of which is in a standard list format, the second being a list of lists, the inner lists containing elements in the form of 3-tuples. My output is a new list in the format of the the second list, containing the same number of elements in the same number of inner lists, with some of the values slightly adjusted as a result of being passed through the function.
Here is an example code, and an example function, where chain is being imported from itertools. first is some list such as [0,1,2,3,1,5,6,7,1,2,3,5,1,1,2,3,5,6]
whilst
second is some list such as [[(13,12,32),(11,444,25)],[(312,443,12),(123,4,123)],[(545,541,1),(561,112,560)]]
def add(x, y):
return x + y
foo = [add(x, y) for x, y in zip(first, chain(*(chain(*second))))]
bar = [foo[i:i+3] for i in range(0, len(foo), 3)]
second = [bar[i:i+2] for i in range(0, len(foo) / 3, 2)]
**Note: The Chain(chain()) part is for the following purpose: Because it's generally a bit harder to handle a list of list containing 3-tuples, The chain(chain()) is just flattening (into a traditional list of individual elements) that second list with the aforementioned 'odd format'. The rest of the code is just rebuilding the new list into the original format from the output of the function, which is already in flattened form.
The problems I'm having are as such:
I want the output to be of the exact same size and format as the original 'second' list. If both lists are empty, I want the empty list returned. If the first list is empty, I want the original second list returned. If the second list is empty, I want the empty list returned.
If the first list is shorter than the second list, I want the function to run for however elements can be matched between the two lists, then the 'excess' of the second list remaining unchanged.
If the second list is shorter than the first list, I want the function to run for however many elements there are in the second list, then just ignore the 'excess' elements from list 1, thus still outputting a new list that has the same dimensions and formatting as the original second list.
My problem is, that I have no idea how to implement these little nuances into my code. Any help would be appreciated.
Cheers, James
since
zip
only zips to the smaller of the two lists, it isn't too useful here. You could create your own algorithm that applies a function to two lists in the way you specify:Result:
I think this does everything you want, if I've understood all the requirements. The major difference from your code is that it also uses
izip_longest()
fromitertools
with a customfillvalue
, instead of plainzip()
. It also just checks for the special cases involving empty input lists at the beginning, which seemed easier than trying to devise list comprehensions or whatever to handle them.Could you pad the first list with None where its not long enough and trim it where its too long.
then only carry out the function where x is not None otherwise return y
i've tried to code an example