小编rod*_*on7的帖子

为什么“map”隐藏“StopIteration”?

map()我发现了一种使用不等同于列表理解的情况。next当用作第一个参数时会发生这种情况。

例如:

l1 = [1, 2]
l2 = ['hello', 'world']
iterators = [iter(l1), iter(l2)]

# list comprehension
values1 = [next(it) for it in iterators]
# values1 = [1, "hello"]
values2 = [next(it) for it in iterators]
# values2 = [2, "world"]
values3 = [next(it) for it in iterators]
# raise StopIteration
Run Code Online (Sandbox Code Playgroud)
l1 = [1, 2]
l2 = ['hello', 'world']
iterators = [iter(l1), iter(l2)]

# map
values1 = list(map(next, iterators))
# values1 = [1, "hello"]
values2 = list(map(next, …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

7
推荐指数
1
解决办法
182
查看次数

标签 统计

python ×1

python-3.x ×1