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
提交
取消