How to combine/merge a number list and a string list into a new list in ascending order in python using recursion?
I want to combine a number list and a string list in ascending order.
Example a = [1, 2, 3], b = [a, b, c, d]
combine a and b and the answer should be [1, a, 2, b, 3, c, d]
or a = [1,2,3], b = [b, d]
combine a and b and the answer should be [1, 2, b, 3, d]
def combine(a, b):
a = [str(int) for int in a]
b = [str(int) for int in b]
if a and b:
if a[0] > b[0]:
a, b = b, a
return [a[0]] + combine(a[1:], b)
return a + b
a = [1, 2, 3] b = ['a', 'b', 'c', 'd']
combine(a, b)
But I got this
['1', '2', '3', 'a', 'b', 'c', 'd']
source https://stackoverflow.com/questions/73806537/how-to-combine-merge-a-number-list-and-a-string-list-into-a-new-list-in-ascendin
Comments
Post a Comment