擅长:python、mysql、java
<p>2种股票的解决方案是</p>
<pre><code> from datetime import *
trades=[['AAA', '2011-01-03', '11:03:51', 21.5],['BBB', '2011-01-03','11:03:57', 31.5],
['AAA', '2011-01-03', '11:04:20', 21.55],
['BBB', '2011-01-03','11:04:19', 32.01], ['BBB', '2011-01-03','11:04:52', 31.7]]
stocknames = ['AAA','BBB']
A=[]
lastofeach=[]
for stock in stocknames:
for t in trades:
if t[0]==stock:
A.append(t)
A.sort(key=lambda e:(e[1], e[2]))
lastofeach.append(A[-1])
A[:]=[]
lastofeach.sort(key=lambda e:e[2])
lastofeach=[[i[0], datetime.strptime(i[1]+" "+i[2], "%Y-%m-%d %H:%M:%S"), i[3]] for i in lastofeach]
trades=[[i[0], datetime.strptime(i[1]+" "+i[2], "%Y-%m-%d %H:%M:%S"), i[3]] for i in trades]
A=lastofeach[0]
B=(sorted((i for i in trades if i[0]!=A[0]), key=lambda n: abs(n[1]-A[1]))[0])
C=[A,B]
print (C)
</code></pre>
<p>(同样的答案也适用日期时间.strftime(A[1],%Y-%m-%d%H:%m:%S”),将其拆分并保存。在</p>
<p>如能为n>;2提供解决方案,我们将不胜感激。任何大的O优化都是受欢迎的。在</p>
<p>谢谢</p>