I'm trying to merge two sorted arrays into a sorted array. I know how to do that in general (so the question itself isn't about merging lists), but for learning purposes I want to write it with a list comprehension. Ideally, I'd write this:
i=0; j=0
[a[i++] if j >= len(b) or a[i] < b[j] else b[j++]
for tmp in range (len(a)+len(b))]
(I know a has larger last element, so the above wouldn't go out of bound). However, python doesn't have an ++ operator. I'd try to write it myself:
def inc (x): x += 1; return x-1
But this doesn't work since python doesn't pass by reference. I'd use iterators if I could look at the top element without advancing it, but I can't.
So, is there a way to write it elegantly in one statement, instead of like this:
i=0; j=0
while i<len(a) or j<len(b):
if j >= len(b) or a[i]<b[j]: res.append(a[i]); i += 1
else res.append(b[j]); j += 1