Merging arrays with for loop in python
Question:
I want to merge two arrays in to one array in an alternating sequence (arr1,arr2,arr1,arr2,….). After two hours i got this far by trying different methods like concatenate or append or two interlocked "for loops".
As i want to use this regardless of the format (string or integer arrays) i wanted to use a "for loop". My try gives me the correct order, but has some elements missing as the counter isnĀ“t perfect. So what can i do correct that?
Example:
arr1 = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29]
arr2 = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30]
merged = [None]*2*len(arr1)
v = range(0,len(arr1))
for i in v[::2]:
merged[i] = arr1[i]
merged[i+1]=arr2[i]
print(merged)
gives
[1, 2, 5, 6, 9, 10, 13, 14, 17, 18, 21, 22, 25, 26, 29, 30, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
Answers:
You can simply merge array by using addition operator for example
result_array=array1+array2
Then you can sort it to get your result
result_array.sort()
To merge them such that the values alternate you can simply zip the two lists and iterating over the zipped object will return a tuple with an item from one list and an item at the same index from the other list (order of passing arrays to zip
is important) and you can extend your list by that returned tuple:
arr1 = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]
arr2 = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
merged = []
for values in zip(arr1, arr2):
merged.extend(values)
Alternatively you can use a simple list comprehension:
merged = [x for tpl in zip(arr1, arr2) for x in tpl]
This could easily be achieved like this:
result = [];
arr1 = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29]
arr2 = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30]
for i in range(len(arr1)):
result.append(arr1[i])
result.append(arr2[i])
print(result)
Keeping with your solution you need to iterate up to the size of the target list, not the smaller one. You also need to divide i
by 2 to fit arr
indices
arr1 = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]
arr2 = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
merged = [None] * 2 * len(arr1)
for i in range(0, len(merged) - 1, 2):
merged[i] = arr1[i // 2]
merged[i + 1] = arr2[i // 2]
print(merged)
# [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, 29, 30]
Above solutions works;
Another way to get desired result is;
arr1 = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29]
arr2 = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30]
merged = []
for i in range(0,len(arr1)):
merged.append(arr1[i])
merged.append(arr2[i])
print(merged)
arr1 = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29]
arr2 = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30]
merged = []
[merged.extend([a1, a2]) for a1, a2 in zip(arr1, arr2)]
print(merged)
You can also use numpy, combination of vstack and flatten
import numpy as np
arr1 = np.array([1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29])
arr2 = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30])
result = np.vstack((arr1, arr2)).flatten("F")
print(result)
Result:
[ 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 29 30]
I want to merge two arrays in to one array in an alternating sequence (arr1,arr2,arr1,arr2,….). After two hours i got this far by trying different methods like concatenate or append or two interlocked "for loops".
As i want to use this regardless of the format (string or integer arrays) i wanted to use a "for loop". My try gives me the correct order, but has some elements missing as the counter isnĀ“t perfect. So what can i do correct that?
Example:
arr1 = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29]
arr2 = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30]
merged = [None]*2*len(arr1)
v = range(0,len(arr1))
for i in v[::2]:
merged[i] = arr1[i]
merged[i+1]=arr2[i]
print(merged)
gives
[1, 2, 5, 6, 9, 10, 13, 14, 17, 18, 21, 22, 25, 26, 29, 30, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
You can simply merge array by using addition operator for example
result_array=array1+array2
Then you can sort it to get your result
result_array.sort()
To merge them such that the values alternate you can simply zip the two lists and iterating over the zipped object will return a tuple with an item from one list and an item at the same index from the other list (order of passing arrays to zip
is important) and you can extend your list by that returned tuple:
arr1 = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]
arr2 = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
merged = []
for values in zip(arr1, arr2):
merged.extend(values)
Alternatively you can use a simple list comprehension:
merged = [x for tpl in zip(arr1, arr2) for x in tpl]
This could easily be achieved like this:
result = [];
arr1 = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29]
arr2 = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30]
for i in range(len(arr1)):
result.append(arr1[i])
result.append(arr2[i])
print(result)
Keeping with your solution you need to iterate up to the size of the target list, not the smaller one. You also need to divide i
by 2 to fit arr
indices
arr1 = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]
arr2 = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
merged = [None] * 2 * len(arr1)
for i in range(0, len(merged) - 1, 2):
merged[i] = arr1[i // 2]
merged[i + 1] = arr2[i // 2]
print(merged)
# [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, 29, 30]
Above solutions works;
Another way to get desired result is;
arr1 = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29]
arr2 = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30]
merged = []
for i in range(0,len(arr1)):
merged.append(arr1[i])
merged.append(arr2[i])
print(merged)
arr1 = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29]
arr2 = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30]
merged = []
[merged.extend([a1, a2]) for a1, a2 in zip(arr1, arr2)]
print(merged)
You can also use numpy, combination of vstack and flatten
import numpy as np
arr1 = np.array([1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29])
arr2 = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30])
result = np.vstack((arr1, arr2)).flatten("F")
print(result)
Result:
[ 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 29 30]