私信  •  关注

user15051990

user15051990 最近回复了
4 年前
回复了 user15051990 创建的主题 » 在Python3.x中某些情况下获取超时错误

我用左中等分和左中等分来解决这个问题。这是一种优化的方法。

from bisect import bisect_left, insort_left
count = 0
listD = sorted(my_list[:d])

def median():
   return listD[d//2] if d%2 == 1 else ((listD[d//2] + listD[d//2-1])/2)

for i in range(d,n):
   if my_list[i] >= 2*median(): 
      count += 1
   del listD[bisect_left(listD, my_list[i-d])]
   insort_left(listD, my_list[i])
print(count)
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
                   'B': 'one one two three two two one three'.split(),
                   'C': np.arange(8), 'D': np.arange(8) * 2})
df[df['A']=='foo']

OUTPUT:
   A      B  C   D
0  foo    one  0   0
2  foo    two  2   4
4  foo    two  4   8
6  foo    one  6  12
7  foo  three  7  14