我想檢查範圍的名單是在接下來的列表的範圍,這裏有一個例子:檢查清單範圍是在另一個列表範圍
pos = [[50, 100], [50, 200], [250, 1500], [300, 2000], [300, 3300]]
正如我們所看到的,在pos[0]
處於pos[1]
([50, 100]
包含在[50, 200]
中)的範圍內,pos[2]
與pos[3]
和pos[4]
與pos[5]
相同。
要做到這一點,我創建了一個返回boolean值的函數:
def in_list(i): # index of the list
if i < len(pos)-2 and pos[i][0] >= pos[i+1][0] and pos[i][0] <= pos[i+1][1]
and pos[i][1] >= pos[i+1][0] and pos[i][1] <= pos[i+1][1]:
return True
else:
return False
它看起來這麼難看,任何人可以提出另一種解決辦法?
編輯:
由於@MKesper的建議,我也應該遍歷使用循環列表條目(和可能得到的名單真/假每個位置)。
你可能想用'for pos in pos:' – MKesper
@MKesper遍歷你的列表條目,是的,你是對的。 – Bilal