在这种情况下,Python列表理解是有效的吗?
是python中的输入“脏”列表在这种情况下,Python列表理解是有效的吗?,python,Python,是python中的输入“脏”列表 input_list = [' \n ',' data1\n ',' data2\n',' \n','data3\n'.....] 每个列表元素都包含带有新行字符的空格或带有新行字符的数据 使用以下代码将其清除 cleaned_up_list = [data.strip() for data in input_list if data.strip()] 给予 在上述列表理解过程中,python是否在内部调用了两次strip()?或者,如果我关心效
input_list = [' \n ',' data1\n ',' data2\n',' \n','data3\n'.....]
每个列表元素都包含带有新行字符的空格或带有新行字符的数据
使用以下代码将其清除
cleaned_up_list = [data.strip() for data in input_list if data.strip()]
给予
在上述列表理解过程中,python是否在内部调用了两次strip()
?或者,如果我关心效率的话,我是否必须使用循环迭代和strip()
一次
for data in input_list
clean_data = data.strip()
if(clean_data):
cleaned_up_list.append(clean_data)
使用列表comp strip调用两次,如果只想调用strip一次并保持理解,请使用gen exp:
input_list[:] = [x for x in (s.strip() for s in input_list) if x]
输入:
input_list = [' \n ',' data1\n ',' data2\n',' \n','data3\n']
输出:
['data1', 'data2', 'data3']
input\u list[:]
将更改原始列表,该列表可能是您想要的,也可能不是您想要的,如果您确实想要创建一个新列表,只需使用cleaned\u list=…
我总是发现在Python2中使用itertools.imap
,在Python3中使用map
,而不是使用生成器,对于更大的输入来说效率最高:
from itertools import imap
input_list[:] = [x for x in imap(str.strip, input_list) if x]
采用不同方法的一些时间安排:
In [17]: input_list = [choice(input_list) for _ in range(1000000)]
In [19]: timeit filter(None, imap(str.strip, input_list))
10 loops, best of 3: 115 ms per loop
In [20]: timeit list(ifilter(None,imap(str.strip,input_list)))
10 loops, best of 3: 110 ms per loop
In [21]: timeit [x for x in imap(str.strip,input_list) if x]
10 loops, best of 3: 125 ms per loop
In [22]: timeit [x for x in (s.strip() for s in input_list) if x]
10 loops, best of 3: 145 ms per loop
In [23]: timeit [data.strip() for data in input_list if data.strip()]
10 loops, best of 3: 160 ms per loop
In [24]: %%timeit
....: cleaned_up_list = []
....: for data in input_list:
....: clean_data = data.strip()
....: if clean_data:
....: cleaned_up_list.append(clean_data)
....:
10 loops, best of 3: 150 ms per loop
In [25]:
In [25]: %%timeit
....: cleaned_up_list = []
....: append = cleaned_up_list.append
....: for data in input_list:
....: clean_data = data.strip()
....: if clean_data:
....: append(clean_data)
....:
10 loops, best of 3: 123 ms per loop
最快的方法实际上是itertools.ifilter
与itertools.imap
相结合,然后是filter
与imap
相结合
无需重新评估函数引用列表。append
每次迭代都更有效,如果您遇到循环问题,需要最有效的方法,那么它是一种可行的替代方法。您可以自己找到,通过编写一个自定义函数,每次调用时递增计数器和/或打印出来。这比调用strip()
两次轻吗?对于很多数据,是的,因为您只设置了一次理解,但是strip会在列表中的每个元素上调用。@PythonNut,它更改原始列表/对象。它可以在保持列表编译速度的同时更改原始列表,使用循环的替代方法可能是枚举(列表)中的i,ele的;list[ind]=ele.strip()
如果OP确实想更新原始列表。@wolfgang。尝试a=[1,2,3];b=a;a=[4,5,6]
,然后打印a和b,您将看到旧列表仍然挂起,因为b仍然保留对的引用it@PadraicCunningham是的,绝对有道理
In [17]: input_list = [choice(input_list) for _ in range(1000000)]
In [19]: timeit filter(None, imap(str.strip, input_list))
10 loops, best of 3: 115 ms per loop
In [20]: timeit list(ifilter(None,imap(str.strip,input_list)))
10 loops, best of 3: 110 ms per loop
In [21]: timeit [x for x in imap(str.strip,input_list) if x]
10 loops, best of 3: 125 ms per loop
In [22]: timeit [x for x in (s.strip() for s in input_list) if x]
10 loops, best of 3: 145 ms per loop
In [23]: timeit [data.strip() for data in input_list if data.strip()]
10 loops, best of 3: 160 ms per loop
In [24]: %%timeit
....: cleaned_up_list = []
....: for data in input_list:
....: clean_data = data.strip()
....: if clean_data:
....: cleaned_up_list.append(clean_data)
....:
10 loops, best of 3: 150 ms per loop
In [25]:
In [25]: %%timeit
....: cleaned_up_list = []
....: append = cleaned_up_list.append
....: for data in input_list:
....: clean_data = data.strip()
....: if clean_data:
....: append(clean_data)
....:
10 loops, best of 3: 123 ms per loop