相关疑难解决方法(0)

Python的itertools.repeat的目的是什么?

我可以想到的Python itertools.repeat()类的每一个用途,我可以想到另一个同样(可能更多)可接受的解决方案来实现相同的效果.例如:

>>> [i for i in itertools.repeat('example', 5)]
['example', 'example', 'example', 'example', 'example']
>>> ['example'] * 5
['example', 'example', 'example', 'example', 'example']

>>> list(map(str.upper, itertools.repeat('example', 5)))
['EXAMPLE', 'EXAMPLE', 'EXAMPLE', 'EXAMPLE', 'EXAMPLE']
>>> ['example'.upper()] * 5
['EXAMPLE', 'EXAMPLE', 'EXAMPLE', 'EXAMPLE', 'EXAMPLE']
Run Code Online (Sandbox Code Playgroud)

在任何情况下,它是最合适的解决方案吗?如果是这样,在什么情况下呢?

python python-itertools python-3.x

23
推荐指数
3
解决办法
1万
查看次数

标签 统计

python ×1

python-3.x ×1

python-itertools ×1