我有一個很長的值列表x和y,按x值排序。我想輸出x和y值最長連續跨度的列表。這是有點難以付諸話但將有希望成爲用下面的例子清楚:作爲5768和6000之間的區域沒有被任何條目的覆蓋,上述應迷惑分區域列表中的覆蓋區域
0, 148
0, 145
0, 186
0, 5768
600, 2374
2376, 2415
3000, 4315
6000, 6616
6000, 6799
6000, 7262
輸出:
0, 5768
6000, 7262
在我看來,這應該是一個簡單的問題,但我一直沒有解決方案的工作一段時間。我已經在下面發佈了我的代碼。 我目前的努力存在的問題是,雖然對x值進行排序,但可能是第k行的x值超過第k-1行的y值,但不標記新連續字符串的開始。
lines = [line.strip('\n') for line in open('test')]
myarray=[]
for line in lines:
myarray.append(line.split(', '))
def findCoveredRegions(regionArray):
resultsContigs = []
j = regionArray[0][1]
i = regionArray[0][0]
for line in regionArray:
last_i = i
i = line[0]
if i <= j:
if line[1] > j:
j = line[1]
else:
resultsContigs.append([last_i,j])
resultsContigs.append([i,regionArray[len(regionArray)-1][1]])
return resultsContigs
print findCoveredRegions(myarray)
對不起,我不明白這個問題,即使是這個例子。如果你發現很難用文字表達,那麼你幾乎可以肯定地發現很難將其寫入代碼。也許首先就是這樣做的。 – Denziloe
您能否詳細說明您的意思:1)「x和y值的最長連續跨度」2)「5768和6000之間的區域未被覆蓋」 –
想象一下,您將連續序列中的0到7262之間的所有數字。我們可以將我的例子中的每一行看作0到148,0到145等所有數字的字符串。 我想要生成的是0到7262之間的區域列表,其數目至少會出現一次,知道有些數字根本不會出現。 5768和6000之間的數字不是任何子序列的一部分,但0和5768以及6000和7262之間的所有數字至少被其中一個區域「覆蓋」。 這有道理嗎? – Sigurgeir