How Do i merge 2 Lists in an array to one Sorted Array in Python
Question:
I am trying to merge 2 Lists in python into one sorted List.
Please how is this possible?
My code is looking thus :
def MergeSortedArray(arr1, arr2):
for i in range(len(arr1)):
for j in range(len(arr2)):
a = zip(i,j)
return a
arr1 = [3,5,6,8]
arr2 = [1,2,9,7]
print(MergeSortedArray(arr1, arr2))
How do i get this to work? Please Help. I am getting this as error :
File "<string>", line 6
return a
^
TabError: inconsistent use of tabs and spaces in indentation
>
Answers:
import typing
def merge_sorted_array(a1: typing.List[int], a2: typing.List[int]) -> typing.List[int]:
return sorted(a1 + a2)
The error you are getting has nothing to do with your problem. It means you are mixing tabs and spaces in your code, and the interpreter can not figure out the indentation because of it.
arr1 = sorted(arr1)
arr2 = sorted(arr2)
# Merging 2 sorted arrays
finalSortedarray = arr1 + arr2
# Sorting 2 arrays and Merging them
finalSortedarray = sorted(arr1 + arr2)
Hope this helps!
I am trying to merge 2 Lists in python into one sorted List.
Please how is this possible?
My code is looking thus :
def MergeSortedArray(arr1, arr2):
for i in range(len(arr1)):
for j in range(len(arr2)):
a = zip(i,j)
return a
arr1 = [3,5,6,8]
arr2 = [1,2,9,7]
print(MergeSortedArray(arr1, arr2))
How do i get this to work? Please Help. I am getting this as error :
File "<string>", line 6
return a
^
TabError: inconsistent use of tabs and spaces in indentation
>
import typing
def merge_sorted_array(a1: typing.List[int], a2: typing.List[int]) -> typing.List[int]:
return sorted(a1 + a2)
The error you are getting has nothing to do with your problem. It means you are mixing tabs and spaces in your code, and the interpreter can not figure out the indentation because of it.
arr1 = sorted(arr1)
arr2 = sorted(arr2)
# Merging 2 sorted arrays
finalSortedarray = arr1 + arr2
# Sorting 2 arrays and Merging them
finalSortedarray = sorted(arr1 + arr2)
Hope this helps!