“...从两个大型 CSV 文件中提取数据,一个是人们的日程安排,另一个是有关他们日程安排的信息。” 含糊不清,但我想我明白了。
“对数据进行挖掘和组合,最终为周一至周六的人们联系创建 pajek 格式图表,” 进行挖掘和组合。凉爽的。在哪里?在这个脚本中?在另一个应用程序中?通过一些第 3 方模块?通过一些网络服务?
这是一次一行的算法吗?一行输入是否会产生一个连接,该连接会发送到一个或多个每日图表?
这是一种必须先查看整个时间表才能产生任何结果的算法吗? [如果是这样,那可能是错误的,但我真的不知道,你的问题在这个中心细节上相当模糊。]
"...第七个图表,表示一周内的所有连接,用 1 和 0 的字符串表示连接是在一周中的哪几天建立的。" 不完整,但可能已经足够好了。
def makeKey2( row2 ):
return ( row2[1], row2[2] ) # Whatever the lookup key is for source2
def makeKey1( row1 ):
return ( row1[3], row1[0] ) # Whatever the lookup key is for source1
dayFile = [ open("day%d.pajek","w") for i in range(6) ]
combined = open("combined.dat","w")
source1 = open( schedules, "r" )
rdr1= csv.reader( source1 )
source2 = open( aboutSchedules, "r" )
rdr2= csv.reader( source2 )
# "Combine" usually means a relational join between source 1 and source 2.
# We'll assume that source2 is a small-ish dimension and the
# source1 is largish facts
aboutDim = dict( (makeKey2(row),row) for row in rdr2 )
for row in rdr1:
connection, dayList = mine_and_combine( row, aboutDim[ makeKey1(row) ] )
for d in dayList:
dayFile[d].write( connection )
flags = [ 1 if d is in dayList else 0 for d in range(6) ]
combined.write( connection, flags )
类似的东西。
要点是:
遍历每个数据源。没有嵌套循环。 O(n) 次处理。
尽可能少地保留内存以创建有用的结果。