python在列表末尾删除一个_从列表末尾删除连续出现的python

{{cd2>这样使用。在from itertools import dropwhile

np.fromiter(dropwhile(lambda x: x, ar[::-1]), dtype=bool)[::-1]

编辑

这是更快的方法。(只需使用itertools.takewhile)

^{pr2}$

时间:ar = np.array([True, False, True, True, True])

#mine

%timeit ar[:-sum(1 for i in takewhile(lambda x: x, reversed(ar)))]

1000000 loops, best of 3: 1.84 us per loop

#mine

%timeit np.fromiter(dropwhile(lambda x: x, ar[::-1]), dtype=bool)[::-1]

100000 loops, best of 3: 2.93 us per loop

#@Jaime

%timeit ar[np.bitwise_or.accumulate(~ar[::-1])[::-1]]

100000 loops, best of 3: 3.63 us per loop

#@askewchan

%timeit ar[:len(ar)-np.argmin(ar[::-1])]

100000 loops, best of 3: 6.24 us per loop

#@xbb

%timeit ar[:len(ar)-np.where(ar[::-1]==False)[0][0]] if np.where(ar[::-1]==False)[0].size>0 else ar[:0]

100000 loops, best of 3: 7.61 us per loop

附言:

这不是什么神奇的功能。在def no_magic_func(ar):

for i in xrange(ar.size-1, -1, -1):

if not ar[i]:

return ar[:i+1]

return ar[0:0]

时间:ar = np.array([True, False, True, True, True])

%timeit no_magic_func(ar)

1000000 loops, best of 3: 954 ns per loop

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值