【发布时间】:2021-10-31 21:16:00
【问题描述】:
我有两个列表
List1 = ['foo', 'bar', '.txt']
List2 = [(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5)]
我希望输出是
output = ['foo1bar3.txt', 'foo1bar4.txt', 'foo1bar5.txt', 'foo2bar3.txt', 'foo2bar4.txt', 'foo2bar5.txt']
【问题讨论】:
-
使用列表推导 (docs.python.org/3/tutorial/…) 和 zip (docs.python.org/3/library/functions.html#zip)
-
zip实际上是这里的重点。 -
@schwobaseggl 但
itertools.zip_longest不是:D -
@timgeb hmm.. 我可以在这里使用任何压缩的唯一方法是一遍又一遍地循环通过可迭代的生成 List1。但是,我可以使用普通的
zip。 -
如果您不必在 5 分钟后盯着自己的代码,那您就做错了:D