我有一個包含不同值范圍的元組的參考串列。
[(1042, 1056), (895, 922), (966, 995), (692, 716), (667, 690),
(667, 690), (667, 690), (479, 508), (1112, 1578)]
我有以下串列串列,其中包含必須與參考串列進行比較的值元組。
[ [(450,470)],
[(100, 200), (500, 700)],
[(0, 29), (3827, 3856)],
[(820, 835), (1539, 1554)],
[(622, 635), (1286, 1299), (1585, 1598), (1607, 1620)],
[(637, 642), (780, 785), (1341, 1346), (1944, 1949), (2044, 2049),
(2158, 2163), (2594, 2599), (2643, 2648)] ]
我正在嘗試從每個串列中選擇一個位于參考串列中存在的元組范圍內的元組。
我考慮的條件是:
如果輸入串列不包含值在參考串列范圍內的元組,則可以采用任何元組。例如
[(0, 29), (3827, 3856)]不在參考串列的范圍內,所以我可以取任何元組。默認情況下,我將串列中的第一個元組附加到參考串列中。如果找到參考串列范圍內的元組,則將該元組附加到參考串列并停止在該回圈中的搜索。例子是
[(622, 635), (1286, 1299), (1585, 1598), (1607, 1620)]如果參考串列范圍內還存在多個元組,則將找到的第一個元組附加到參考串列中。例子是
[(637, 642), (780, 785), (1341, 1346), (1944, 1949), (2044, 2049), (2158, 2163), (2594, 2599), (2643, 2648)]元組中的值永遠不會相同,元組中的第二個值總是大于第一個值。
我用來查找范圍的邏輯是我在參考串列的元組的第一個位置取最小值和最大值。我做了簡單的迭代。
我使用的代碼是
tag_pos_refin = [(1042, 1056), (895, 922), (966, 995), (692, 716), (667, 690),
(667, 690), (667, 690), (479, 508), (1112, 1578)]
tag_pos_db = [ [(450,470)],
[(100, 200), (500, 700)],
[(0, 29), (3827, 3856)],
[(820, 835), (1539, 1554)],
[(622, 635), (1286, 1299), (1585, 1598), (1607, 1620)],
[(637, 642), (780, 785), (1341, 1346), (1944, 1949), (2044, 2049), (2158, 2163),
(2594, 2599), (2643, 2648)]
]
min_threshold = min(tag_pos_refin)[0]
max_threshold = max(tag_pos_refin)[0]
for tag_pos in tag_pos_db:
if len(tag_pos) == 1:
tag_pos_refin.extend(tag_pos)
for tag_pos in tag_pos_db:
if len(tag_pos) > 1:
for j in tag_pos:
if j[0] in range(min_threshold, max_threshold):
tag_pos_refin.append(j)
break
elif min(tag_pos)[0] not in range(min_threshold, max_threshold):
tag_pos_refin.append(j)
break
print(tag_pos_refin)
獲得的輸出
[(1042, 1056), (895, 922), (966, 995), (692, 716), (667, 690), (667, 690), (667, 690), (479, 508), (1112, 1578), (450, 470), (100, 200), (0, 29), (820, 835), (622, 635), (637, 642)]
期望的輸出
[(1042, 1056), (895, 922), (966, 995), (692, 716), (667, 690), (667, 690), (667, 690), (479, 508), (1112, 1578), (450, 470), (500, 700), (0, 29), (820, 835), (622, 635), (637, 642)]
我的疑問是
是否可以以更好的方式或更好的邏輯撰寫代碼來查找范圍,以便(100,200)最好的元組是(500,700).
(這個用例解釋起來有點復雜:但是元組的值可以被認為是文本中單詞或句子的索引點)
uj5u.com熱心網友回復:
您的代碼存在許多問題。首先,您需要檢查每個元組的兩個值,因為任何一個都可能在范圍內,但不一定都在。其次,不斷地重新創建一個range物件來進行簡單的邊界檢查是低效的,而且你的實作也有一個錯誤的錯誤(假設你想要一個包含范圍)。第三,在查找匹配項時,您不會檢查所有元組,這意味著可能會附加錯誤的元組。
在下面的解決方案中,我添加了一些額外的測驗來檢查邊界情況:
tag_pos_refin = [(1042, 1056), (895, 922), (966, 995), (692, 716), (667, 690),
(667, 690), (667, 690), (479, 508), (1112, 1578)]
tag_pos_db = [
[(200, 1500), (1112, 1200)], # test upper bound
[(100, 1600), (275, 479)], # test lower bound
[(450, 470)],
[(100, 200), (500, 700)],
[(0, 29), (3827, 3856)],
[(820, 835), (1539, 1554)],
[(622, 635), (1286, 1299), (1585, 1598), (1607, 1620)],
[(637, 642), (780, 785), (1341, 1346), (1944, 1949), (2044, 2049), (2158, 2163),
(2594, 2599), (2643, 2648)],
]
min_threshold = min(tag_pos_refin)[0]
max_threshold = max(tag_pos_refin)[0]
print(f'min/max: {min_threshold}-{max_threshold}\n')
for tag_pos in tag_pos_db:
if tag_pos:
print(f'checking {tag_pos}', end='')
for j in tag_pos:
if (min_threshold <= j[0] <= max_threshold or
min_threshold <= j[1] <= max_threshold):
print(' -> found match')
tag_pos_refin.append(j)
break
else:
print(' -> no matches')
tag_pos_refin.append(tag_pos[0])
print(f'APPENDED: {tag_pos_refin[-1]}\n')
print(f'RESULT: {tag_pos_refin}\n')
輸出:
min/max: 479-1112
checking [(200, 1500), (1112, 1200)] -> found match
APPENDED: (1112, 1200)
checking [(100, 1600), (275, 479)] -> found match
APPENDED: (275, 479)
checking [(450, 470)] -> no matches
APPENDED: (450, 470)
checking [(100, 200), (500, 700)] -> found match
APPENDED: (500, 700)
checking [(0, 29), (3827, 3856)] -> no matches
APPENDED: (0, 29)
checking [(820, 835), (1539, 1554)] -> found match
APPENDED: (820, 835)
checking [(622, 635), (1286, 1299), (1585, 1598), (1607, 1620)] -> found match
APPENDED: (622, 635)
checking [(637, 642), (780, 785), (1341, 1346), (1944, 1949), (2044, 2049), (2158, 2163), (2594, 2599), (2643, 2648)] -> found match
APPENDED: (637, 642)
RESULT: [(1042, 1056), (895, 922), (966, 995), (692, 716), (667, 690), (667, 690), (667, 690), (479, 508), (1112, 1578), (1112, 1200), (275, 479), (450, 470), (500, 700), (0, 29), (820, 835), (622, 635), (637, 642)]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/479550.html
