繁体   English   中英

这个循环可以实现Pythonic列表理解吗?

[英]Pythonic list comprehension possible with this loop?

我与列表理解有一种爱/恨的关系。 一方面我认为它们整洁优雅。 另一方面,我讨厌阅读它们。 (特别是那些我没写的)我通常遵循规则,让它可读,直到需要速度。 所以我的问题在这一点上确实是学术性的。

我想要一张表格中的电台列表,这些电台的字符串通常有额外的空格。 我需要剥去那些空间。 有时这些站是空白的,不应包括在内。

stations = []
for row in data:
    if row.strip():
        stations.append(row.strip())

这转化为这个列表理解:

stations = [row.strip() for row in data if row.strip()]

这种方法效果很好,但我发现我正在做两次剥离。 我猜测.strip()实际上并不需要两次,并且通常比分配变量慢。

stations = []
for row in data:
    blah = row.strip()
    if blah:
        stations.append(blah)

事实证明我是对的。

> Striptwice list comp 14.5714301669     
> Striptwice loop 17.9919670399
> Striponce loop 13.0950567955

Timeit显示在两个循环段之间,第二个(条带一次)更快。 这里没有真正的惊喜。 令我感到惊讶的是,列表理解只是略微慢一些,即使它正在做两次。

我的问题: 有没有办法写一个列表理解,只做一次剥离?



结果:

以下是建议的时间结果

# @JonClements & @ErikAllik
> Striptonce list comp 10.7998494348
# @adhie
> Mapmethod loop 14.4501044569

有 - 首先创建一个剥离字符串的生成器,然后使用:

stations = [row for row in (row.strip() for row in data) if row]

您也可以在没有comp的情况下编写它,例如(交换到imap并删除Python 2.x的list ):

stations = list(filter(None, map(str.strip, data)))

嵌套的理解可能很难阅读,所以我的首选是:

stripped = (x.strip() for x in data)
stations = [x for x in stripped if x]

或者,如果你内联stripped ,你会得到一个(嵌套的)列表理解:

stations = [x for x in (x.strip() for x in data) if x]

注意,第一个/内部理解是一个实际的生成器表达式,换句话说,它是一个惰性列表理解; 这是为了避免迭代两次。

使用map()将strip应用于所有元素,然后过滤。

[item for item in map(lambda x: x.strip(), list) if item]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM