与えられる: 2つのcsvファイル(それぞれ1.8 MB):AllData_1、AllData_2。それぞれ〜8,000行あります。各行は8列で構成されています。 [txt_0、txt_1、txt_2、txt_3、txt_4、txt_5、txt_6、txt_7、txt_8]Python 2の比較最適化リスト
目標: txt_0の一致(または、AllData_1 [0] == AllData_2)に基づいて、内容を比較しますこれらの個々の行の次の4つの列。データが不一致の場合は、異なる列に基づいてリスト内の各データセットの行全体を入れ、リストを出力ファイルに保存します。 txt_0が1つのデータセットで他のデータセットでない場合は、出力ファイルに直接保存します。
例:
AllData_1列xが含まれています:[A1、B2、C3、D4、E5、F6、G7、H8]は AllData_2行yが含まれている:[A1、B2、c33cの、d44d、E5、F6を、g7、h8]
プログラムは、行xとyのすべてをListCol2とListCol3に対応するリストに保存します。すべての比較が終了すると、リストがファイルに保存されます。
コードを高速化したり、コードをより高速なアルゴリズムに変更するにはどうすればよいですか?
i = 0
x0list = []
y0list = []
col1_diff = col2_diff = col3a_diff = col3b_diff = col4_diff = []
#create list out of column 0
for y in AllData_2:
y0list.append(y[0])
for entry in AllData_1:
x0list.append(entry[0])
if entry[0] not in y0list:
#code to save the line to file...
for y0 in AllData_2:
if y0[0] not in x0list:
#code to save the line to file...
for yrow in AllData_2:
i+=1
for xrow in AllData_1:
foundit = 0
if yrow[0] == xrow[0] and foundit == 0 and (yrow[1] != xrow[1] or yrow[2] != xrow[2] or yrow[3] != xrow[3] or yrow[4] != xrow[4]):
if yrow[1] != xrow[1]:
col1_diff.append(yrow)
col1_diff.append(xrow)
foundit = 1
elif yrow[2] != xrow[2]:
col2_diff.append(yrow)
col2_diff.append(xrow)
foundit = 1
elif len(yrow[3]) < len(xrow[3]):
col3a_diff.append(yrow)
col3a_diff.append(xrow)
foundit = 1
elif len(yrow[3]) >= len(xrow[3]):
col3b_diff.append(yrow)
col3b_diff.append(xrow)
foundit = 1
else:
#col4 is actually a catch-all for any other differences between lines if [0]s are equal
col4_diff.append(yrow)
col4_diff.append(xrow)
foundit = 1
多分これはhttp://stackoverflow.com/questions/744256/reading-huge-file-in-pythonを助けることができる –
使用すると、メンバーシップ・テストのためにリストを設定していません。 – katrielalex
'foundit == 0'とは何でしょうか? 'foundit'がテストの直前に0に設定されているので、常に真です。 –