我對python比較新,所以我會盡我所能解釋我正在嘗試做什麼。我試圖遍歷兩個恆星列表(它們都包含記錄數組),試圖通過它們的座標與容差匹配(在這種情況下,Ra和Dec,這兩個記錄數組中的索引)。然而,似乎有一個列表中有多個明星與另一個明星相匹配。 *這是由於這兩個星星在配對內匹配。有沒有辦法來防止這種情況?這是我到目前爲止:防止列表迭代中的多個匹配
from __future__ import print_function
import numpy as np
###importing data###
Astars = list()
for s in ApStars:###this is imported but not shown
Astars.append(s)
wStars = list()
data1 = np.genfromtxt('6819.txt', dtype=None, delimiter=',', names=True)
for star in data1:
wStars.append(star)
###beginning matching stars between the Astars and wStars###
list1 = list()
list2 = list()
for star,s in [(star,s) for star in wStars for s in Astars]:
if np.logical_and(np.isclose(s["RA"],star["RA"], atol=0.000277778)==True ,
np.isclose(s["DEC"],star["DEC"],atol=0.000277778)==True):
if star not in list1:
list1.append(star) #matched wStars
if s not in list2:
list2.append(s) #matched Astars
我不能減少atol,因爲它超出了儀器的誤差。會發生什麼是:有多個Wstars匹配一個Astar。如果可能的話,我只想要一顆明星。
有什麼建議嗎?
題外話題,但你的「繼續」陳述不做任何事 –
你能澄清/修改你的代碼嗎?你正在初始化'list1'和'list2',但是這兩個都是你的'for star,s ...'循環時的空列表。 –
可能是我的編輯... @Thomas_Grier,你可以縮進「continue」語句到他們正確的位置嗎? (最後一個'continue'是多餘的,但是你可能需要第一個與if語句相同的水平。 – Alexander