【发布时间】:2014-03-27 13:08:45
【问题描述】:
我有一个列表,其中包含大约 177071007 项。 我正在尝试执行以下操作 a) 获取列表中唯一项的第一次和最后一次出现。 b) 出现次数。
def parse_data(file, op_file_test):
ins = csv.reader(open(file, 'rb'), delimiter = '\t')
pc = list()
rd = list()
deltas = list()
reoccurance = list()
try:
for row in ins:
pc.append(int(row[0]))
rd.append(int(row[1]))
except:
print row
pass
unique_pc = set(pc)
unique_pc = list(unique_pc)
print "closing file"
#takes a long time from here!
for a in range(0, len(unique_pc)):
index_first_occurance = pc.index(unique_pc[a])
index_last_occurance = len(pc) - 1 - pc[::-1].index(unique_pc[a])
delta_rd = rd[index_last_occurance] - rd[index_first_occurance]
deltas.append(int(delta_rd))
reoccurance.append(pc.count(unique_pc[a]))
print unique_pc[a] , delta_rd, reoccurance[a]
print "printing to file"
map_file = open(op_file_test,'a')
for a in range(0, len(unique_pc)):
print >>map_file, "%d, %d, %d" % (unique_pc[a], deltas[a], reoccurance)
map_file.close()
但是复杂度在 O(n) 的数量级。 是否有可能使 for 循环“运行得快”,我的意思是,你认为 yield 会使其更快吗?还是有其他方法?不幸的是,我没有 numpy
【问题讨论】: