このアルゴリズムを実行するより良い方法があるのかどうか疑問に思っていました。私はこのタイプの操作をかなり頻繁に行う必要があることを知っています。現在、私はそれが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)
私は15,000の文字列をそれぞれ含む2つのCSVファイルを持っている、と私はそれぞれの列を比較するために探していますし、それらが一致した場合、最初の終わりに2つ目のCSVファイルの終わりを連結します。どんな助けでも大歓迎です!
ありがとうございます!
Pro:ネイティブライブラリで動作し、外部依存関係はありません。 Con:おそらくPandasで(以下に述べるように)はるかに簡単かつ迅速に行うことができます。比較と追加の両方(コードの3行か4行だと思います) – Kelvin