I have 2 arrays which contains some numbers:
RA=['20 12 40.0319', '15 15 48.4459'...'10 57 3.0215']
DEC=['-02 08 39.97', '-37 09 16.03'...' 22 42 39.07']
I want to merge these arrays as one, like:
POS=['20 12 40.0319 -02 08 39.97','15 15 48.4459 -37 09 16.03','10 57 3.0215 22 42 39.07']
So basically i want to merge i-th element of both one at ith element of new array with a tab between them. How can i do that?
CodePudding user response:
The
zip()function returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together etc. If the passed iterators have different lengths, the iterator with the least items decides the length of the new iterator.
POS = [i ' ' k for i, k in zip(RA,DEC)]
CodePudding user response:
Use zip
>>> POS = [(i '\t' j) for (i,j) in zip(RA, DEC)]
>>> for i in POS:
print(i)
20 12 40.0319 -02 08 39.97
15 15 48.4459 -37 09 16.03
10 57 3.0215 22 42 39.07
CodePudding user response:
I could think of two ways for merging those arrays together, the one approach is to concatenate the ith elements using operator as provided by others.
Alternatively, You can use f-strings.
Solution
def merge_arrays(arr1, arr2):
return [f"{i}\t{j}" for i, j in zip(arr1, arr2)]
RA=['20 12 40.0319', '15 15 48.4459', '10 57 3.0215']
DEC=['-02 08 39.97', '-37 09 16.03', ' 22 42 39.07']
print("\n".join(merge_arrays(RA, DEC)))
Output
20 12 40.0319 -02 08 39.97
15 15 48.4459 -37 09 16.03
10 57 3.0215 22 42 39.07
