相关疑难解决方法(0)

Google Python风格指南

为什么Google Python样式指南更喜欢列表推导和for循环而不是filter,map和reduce?

不推荐使用的语言功能:..."使用列表推导和循环而不是过滤,映射和减少."

给出的解释是:"我们不使用任何不支持这些功能的Python版本,因此没有理由不使用新的样式."

python

15
推荐指数
3
解决办法
7590
查看次数

了解zip功能

所有讨论都是关于python 3.1.2; 请参阅Python文档以获取我的问题的来源.

我知道什么zip呢; 我只是不明白为什么它可以像这样实现:

def zip(*iterables):
    # zip('ABCD', 'xy') --> Ax By
    iterables = map(iter, iterables)
    while iterables:
        yield tuple(map(next, iterables))
Run Code Online (Sandbox Code Playgroud)

比方说我打电话zip(c1, c2, c3).如果我理解正确,iterables最初是元组(c1,c2,c3).

该行将其iterables = map(iter, iterables)转换为迭代器,如果迭代,它将返回iter(c1),iter(c2),iter(c3).

在循环中,map(next, iterables)是会返回一个迭代器next(iter(c1)),next(iter(c2))以及next(iter(c3))如果通过迭代.的tuple通话将其转换为(next(iter(c1)), next(iter(c2)), next(iter(c3)),用尽其参数(iterables据我可以告诉在第一个调用).我不明白while循环如何继续,因为它检查iterables; 如果它继续,为什么tuple调用不返回空元组(迭代器耗尽).

我确定我错过了很简单的东西..

python zip iterator python-3.x

14
推荐指数
2
解决办法
2万
查看次数

标签 统计

python ×2

iterator ×1

python-3.x ×1

zip ×1