我只是想知道是否有更好的方法來做這個算法。我發現我需要經常進行這種類型的操作,而且我現在這樣做的方式需要幾個小時,因爲我相信它會被認爲是n^2算法。我會在下面附上。更有效的方法來做這個搜索算法?
import csv
with open("location1", 'r') as main:
csvMain = csv.reader(main)
mainList = list(csvMain)
with open("location2", 'r') as anno:
csvAnno = csv.reader(anno)
annoList = list(csvAnno)
tempList = []
output = []
for full in mainList:
geneName = full[2].lower()
for annot in annoList:
if geneName == annot[2].lower():
tempList.extend(full)
tempList.append(annot[3])
tempList.append(annot[4])
tempList.append(annot[5])
tempList.append(annot[6])
output.append(tempList)
for i in tempList:
del i
with open("location3", 'w') as final:
a = csv.writer(final, delimiter=',')
a.writerows(output)
我有一個包含每個15000要把兩個CSV文件,我期待從每列比較,如果它們匹配,拼接第二CSV年底到第一年底。任何幫助將不勝感激!
謝謝!
Pro:適用於本地庫並且沒有外部依賴關係。 Con:大熊貓可以做得更容易,速度更快(如下所述)。無論是比較還是追加(我認爲這將是3或4行代碼) – Kelvin