我可以想到的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)
在任何情况下,它是最合适的解决方案吗?如果是这样,在什么情况下呢?