colors = ['black', 'white']sizes = ['S', 'M', 'L']for tshirt in ('%s %s' % (c, s) for c in colors for s in sizes): print(tshirt)black Sblack Mblack Lwhite Swhite Mwhite L所以我试图删除那些%s%s,而是使用af字符串格式。有人可以表现出如何做到这一点吗?谢谢
2 回答
SMILET
TA贡献1796条经验 获得超4个赞
>>> colors = ['black', 'white']
>>> sizes = ['S', 'M', 'L']
>>> for c in colors:
... for s in sizes:
... print(f'{c} {s}')
另一种方法是使用itertools.product:
>>> for c, s in itertools.product(colors, sizes):
... print(f'{c} {s}')
添加回答
举报
0/150
提交
取消