我确定这个简单的问题有答案,但我真的不知道如何用英语表达(惭愧),所以决定问问人类。
假设我有一个列表列表:
[['Sometimes'], [' '], ['I'], [' '], ['love', 'hate'], [' '], ['pizza', 'coke']]
获得以下输出的最pythonic方法是什么?
['Sometimes I love pizza', 'Sometimes I love coke', 'Sometimes I hate pizza', 'Sometimes I hate coke']
最佳答案
from itertools import product
options = [['Sometimes'],[' '],['I'],[' '],['love','hate'],[' '],['pizza','coke']]
for combo in product(*options):
print("".join(combo))
给
Sometimes I love pizza
Sometimes I love coke
Sometimes I hate pizza
Sometimes I hate coke