基本上,在我有如下代码之前,我试图避免循环遍历大数组:
for rows in book:
bs = []
as = []
trdsa = []
trdsb = []
for ish in book:
var = (float(str(ish[0]).replace(':',"")) - float(str(book[0]).replace(':',"")))
if var < .1 and var > 0 :
bs.append(int(ish[4]))
as.append(int(ish[5]))
trdsa.append(int(ish[-2]))
trdsb.append(int(ish[-1]))
time = ish[0]
bflow = sum(numpy.diff(bs))
aflow = sum(numpy.diff(as))
OFI = bflow - aflow - sum(trdsb) + sum(trdsa)
OFIlist.append([time,bidflow,askflow,OFI])
我不想循环遍历列表两次,因为它消耗了太多时间。我在想我可以做一个列表理解,但我不确定我是否走在正确的轨道上
OFIcreate(x,y):
bs = []
as = []
trdsa = []
trdsb = []
var = (float(str(y[0]).replace(':',"")) - float(str(x[0]).replace(':',"")))
if var < .1 and var >= 0 :
bs.append(int(ish[4]))
as.append(int(ish[5]))
trdsa.append(int(ish[-2]))
trdsb.append(int(ish[-1]))
time = ish[0]
bflow = sum(numpy.diff(bs))
aflow = sum(numpy.diff(as))
OFI = bflow - aflow - sum(trdsb) + sum(trdsa)
OFIlist.append([time,bidflow,askflow,OFI])
return OFIlist
OFIc = [ OFIcreate(x,y) for x in book for y in book)
问题是我想遍历列表并将 var >=0 和 var <.1 的所有实例分组,然后将值附加到新列表中。我现在拥有它的方式我不认为它会这样做,因为它只会继续创建长度为 1 的列表。关于我如何做到这一点的任何想法?或者更确切地说,我怎样才能使第一段代码更有效率?