如何合并数组的第 2 个元素
How to merge 2 i-th element of arrays
我有 2 个数组,其中包含一些数字:
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']
我想将这些数组合并为一个,例如:
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']
所以基本上我想在新数组的第 i 个元素处合并两个元素的第 i 个元素,并在它们之间添加一个制表符。我该怎么做?
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)]
使用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
我想出了两种将这些数组合并在一起的方法,一种方法是使用其他人提供的 +
运算符连接第 i 个元素。
或者,您可以使用 f-strings。
解决方案
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)))
输出
20 12 40.0319 -02 08 39.97
15 15 48.4459 -37 09 16.03
10 57 3.0215 +22 42 39.07
我有 2 个数组,其中包含一些数字:
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']
我想将这些数组合并为一个,例如:
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']
所以基本上我想在新数组的第 i 个元素处合并两个元素的第 i 个元素,并在它们之间添加一个制表符。我该怎么做?
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)]
使用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
我想出了两种将这些数组合并在一起的方法,一种方法是使用其他人提供的 +
运算符连接第 i 个元素。
或者,您可以使用 f-strings。
解决方案
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)))
输出
20 12 40.0319 -02 08 39.97
15 15 48.4459 -37 09 16.03
10 57 3.0215 +22 42 39.07