我想清除'a'
的前导出现的列表。也就是说,['a', 'a', 'b', 'b']
应该变为['b', 'b']
,同时['b', 'a', 'a', 'b']
应该保持不变。
def remove_leading_items(l):
if len(l) == 1 or l[0] != 'a':
return l
else:
return remove_leading_items(l[1:])
有更多的pythonic方式吗?
答案 0 :(得分:8)
是。你应该立即使用for循环。递归通常不是Pythonic。其次,使用内置工具:
from itertools import dropwhile
def remove_leading_items(l, item):
return list(dropwhile (lambda x: x == item, l))
或者
return list(dropwhile(item.__eq__, l))
出于好奇,我用不同的方法做了一些实验来解决这个问题:
from itertools import dropwhile
from functools import partial
from operator import eq
def _eq_drop(l, e):
return dropwhile(e.__eq__, l)
def lam_drop(l, e):
return dropwhile(lambda x:x==e, l)
def partial_drop(l, e):
return dropwhile(partial(eq, e), l)
首先,使用完全删除的列表:即(1, 1, 1, ...)
In [64]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: list(_eq_drop(t0, 1))
...:
1000 loops, best of 3: 389 µs per loop
In [65]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: list(lam_drop(t0, 1))
...:
1000 loops, best of 3: 1.19 ms per loop
In [66]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: list(partial_drop(t0, 1))
...:
1000 loops, best of 3: 893 µs per loop
所以__eq__
显然是这种情况下最快的。我喜欢它,但它直接使用dunder方法,有时不赞成。 dropwhile(partial(eq...
方法(罗嗦而又明确)介于两者之间,而缓慢,笨拙的lambda
方法最后出现。不足为奇。
现在,当一半被删除时,即(1, 1, 1, ..., 0, 0, 0)
:
In [52]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: list(_eq_drop(t2, 1))
...:
1000 loops, best of 3: 245 µs per loop
In [53]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: list(lam_drop(t2, 1))
...:
1000 loops, best of 3: 652 µs per loop
In [54]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: list(partial_drop(t2, 1))
...:
1000 loops, best of 3: 487 µs per loop
差异并不明显。
至于为什么我说递归不是Pythonic,请考虑以下内容:
In [6]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: remove_leading_items(t0, 1)
...:
1 loop, best of 3: 405 ms per loop
In [7]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: remove_leading_items(t1, 1)
...:
10000 loops, best of 3: 34.7 µs per loop
In [8]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: remove_leading_items(t2, 1)
...:
1 loop, best of 3: 280 ms per loop
除了退出0(嗯,1项)的堕落情况之外,它在灾难性的情况下表现得更糟。
现在,如果你知道你总是想要一个列表,那么请考虑一种高度迭代的非常方法:
def for_loop(l, e):
it = iter(l)
for x in it:
if x != e:
break
else:
return []
return [x, *it]
它比使用内置插件更好!
In [33]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: for_loop(t0, 1)
...:
1000 loops, best of 3: 270 µs per loop
In [34]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: for_loop(t1, 1)
...:
10000 loops, best of 3: 50.7 µs per loop
In [35]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: for_loop(t2, 1)
...:
10000 loops, best of 3: 160 µs per loop
保持灵活性的良好折衷方案可能是使用基于生成器的方法:
In [5]: def gen_drop(l, e):
...: it = iter(l)
...: for x in it:
...: if x != e:
...: break
...: yield x
...: yield from it
...:
In [6]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: list(gen_drop(t0, 1))
...:
1000 loops, best of 3: 287 µs per loop
In [7]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: list(gen_drop(t1, 1))
...:
1000 loops, best of 3: 359 µs per loop
In [8]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: list(gen_drop(t2, 1))
...:
1000 loops, best of 3: 324 µs per loop
最后,deque
方法:
In [1]: from collections import deque
...:
...: def noLeadingZero(l, e):
...: d = deque(l)
...: for x in l:
...: if e == x:
...: d.popleft()
...: else:
...: break
...: return list(d)
...:
In [2]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: noLeadingZero(t0, 1)
...:
1000 loops, best of 3: 873 µs per loop
In [3]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: noLeadingZero(t1, 1)
...:
10000 loops, best of 3: 121 µs per loop
In [4]: %%timeit n = 10000; t0 = (1,)*n; t1 = (1,) + (0,)*(n-1); t2 = (1,)*(n//2) + (0,)*(n//2);
...: noLeadingZero(t2, 1)
...:
1000 loops, best of 3: 502 µs per loop
答案 1 :(得分:0)
def remove_leading(a_list, to_remove):
i = 0
while i < len(a_list) and a_list[i] == to_remove:
i += 1
return a_list[i:]
print(remove_leading(list('aabb'), 'a'))
print(remove_leading(list('baab'), 'a'))
print(remove_leading([], 'a'))
['b', 'b']
['b', 'a', 'a', 'b']
[]
答案 2 :(得分:0)
您也可以尝试: -
s = ['a', 'a', 'b', 'b','a','a','b']
def check(ls):
new_ls = ls
count = 0
while ls[0]=='a':
new_ls = ls[(count+1):]
ls = new_ls
return new_ls
print(check(s))
答案 3 :(得分:0)
代码:
my_list = ['a', 'a', 'b', 'b', 'a', 'b']
item_i_hate = 'a'
for index in range(len(my_list)):
if my_list[index] != item_i_hate:
my_list = my_list[index:]
break