Home  >  Article  >  Backend Development  >  Python program to merge two arrays

Python program to merge two arrays

王林
王林forward
2023-08-18 21:13:202177browse

Python program to merge two arrays

The process of merging elements of a given array is called merging. This operation can be accomplished in many ways using many techniques. Let us discuss all the techniques that help in merging given arrays in Python. Before getting into these techniques, let's understand how array merging works through a simple input and output scenario.

Input and output scenarios

Consider two arrays arr1 and arr2.

arr1 = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
arr2 = [ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]

Now, the merged array is the resulting array after merging arrays arr1 and arr2.

merged_array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ].

Use " " operator

The operator " " is used to add values ​​in general mathematics. However, in the case of arrays, its usage differs significantly from other applications. It can be used to merge and merge arrays containing merge operations.

grammar

The syntax for merging given arrays using the operator " " is as follows:

merged_array = arr1 + arr2 + arr3 + arr4 + arr5 + arr6 + arr7 + arr8 + . . . . . . . . . . . . . . . . + arrN

Here, arr1, arr2, arr3, arr4, arr5,..., arrN are the arrays to be merged.

Example

In this example, we will discuss the process of merging arrays using the " " operator.

arr1 = [1, 2, 3, 4]
arr2 = [5, 6, 7, 8]
arr3 = [9, 10, 11, 12]
arr4 = [13, 14, 15, 16]
arr5 = [17, 18, 19, 20]
arr6 = [21, 22, 23, 24]
arr7 = [25, 26, 27, 28]
merged_array = arr1 + arr2 + arr3 + arr4 + arr5 + arr6 + arr7 
print("The first array is: ")
print(arr1)

print("The second array is: ")
print(arr2)

print("The third array is: ")
print(arr3)

print("The fourth array is: ")
print(arr4)

print("The fifth array is: ")
print(arr5)

print("The sixth array is: ")
print(arr6)

print("The seventh array is: ")
print(arr7)

print("The merged array of the given arrays after performing merge operation: ")
print(merged_array)

Output

The output of the above program is as follows:

The first array is: 
[1, 2, 3, 4]
The second array is:
[5, 6, 7, 8]
The third array is:
[9, 10, 11, 12]
The fourth array is:
[13, 14, 15, 16]
The fifth array is:
[17, 18, 19, 20]
The sixth array is:
[21, 22, 23, 24]
The seventh array is:
[25, 26, 27, 28]
The merged array of the given arrays after performing merge operation:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28]

Use "naive" methods or "naive techniques"

This technology relies entirely on variables declared within the program for processing. If there are two arrays that need to be merged, a new array will be created in which the elements of both arrays will be stored. Finally, this array will be treated as the merged result array.

Similarly, if you want to merge three arrays, then the elements of all three arrays will be stored in a newly created fourth array. Let us discuss the algorithm followed by this technique and then build the program.

Example

In the following example, we will discuss the process of merging two or more arrays using naive methods.

  • Step 1 - Declare two or more arrays to be merged.

  • Step 2 - Create a new array to store the elements of the initial array.

  • Step 3 - Traverse all elements of the initial array and store these elements into the newly created array at the same time.

  • Step 4 − After iterating through all elements, print the newly created array.

def merge_arrays(arr1, arr2, size1, size2, arr3):
   i = 0
   j = 0
   k = 0
   while(i < size1):
      arr3[k] = arr1[i]
      k = k + 1
      i = i + 1
   while(j < size2):
      arr3[k] = arr2[j]
      k = k + 1
      j = j + 1
   arr3.sort()

if __name__ == '__main__':

   arr1 = [1, 3, 5, 7, 9, 11]
   size1 = len(arr1)

   arr2 = [0, 2, 4, 6, 8, 10]
   size2 = len(arr2)
   arr3 = [0 for i in range(size1 + size2)]
   merge_arrays(arr1, arr2, size1, size2, arr3)
   print("The first array before merging is: ")
   print(arr1)
   print("The second array before merging is: ")
   print(arr2)

   print("The array after being merged and sorted: ")
   print(arr3)

Output

The output of the above program is as follows:

The first array before merging is: 
[1, 3, 5, 7, 9, 11]
The second array before merging is:
[0, 2, 4, 6, 8, 10]
The array after being merged and sorted:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

The above is the detailed content of Python program to merge two arrays. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete