Function to merge sorted lists
Edward C. Jones
edcjones at erols.com
Fri Aug 31 01:08:48 EDT 2001
Ignacio Vazquez-Abrams wrote:
> On Thu, 30 Aug 2001, Edward C. Jones wrote:
>
>
>>Has anyone written in C a merge function for sorted Python sequences?
> Sounds interesting, but... and don't take this the wrong way... I can barely
> understand what you're trying to say. An example might help.
Here is a Python program that does the same thing:
def merge(seq1, seq2):
n1 = len(seq1)
n2 = len(seq2)
i = j = 0
merged = []
while i < n1 and j < n2:
if seq1[i] < seq2[j]:
merged.append(seq1[i])
i += 1
else:
merged.append(seq2[j])
j += 1
if i == n1:
tail1 = []
tail2 = seq2[j:]
else:
tail2 = []
tail1 = seq1[i:]
return merged, tail1, tail2
More information about the Python-list
mailing list