others - Python for循环使用多个列表

我有两个元素列表:


column_width = ["3","3","6","8","4","4","4","4"]


fade = ["100","200","300"]



我要实现的是创建for循环,提供以下输出:


column-3-fade-100


column-3-fade-200


column-6-fade-300


column-8-fade-100


column-4-fade-200


...



嵌套的for循环对我不起作用:


for i in fade:


 for c in column_width_a:


 print("column-{0}-fade-{1}".format(c, i))



还有其他方法来生成这个输出?

时间:

使用itertools.cycle。

例如:


from itertools import cycle



column_width = ["3","3","6","8","4","4","4","4"]


fade = cycle(["100","200","300"])



for i in column_width:


 print("column-{}-fade-{}".format(i, next(fade)))



输出:


column-3-fade-100


column-3-fade-200


column-6-fade-300


column-8-fade-100


column-4-fade-200


column-4-fade-300


column-4-fade-100


column-4-fade-200



尝试以下方法:


from itertools import cycle


print('n'.join("column-{}-fade-{}".format(x, y) for x, y in zip(column_width, cycle(fade))))



...