for tshirt in ('%s %s' % (c, s) for c in colors for s in sizes):
print(tshirt)
black S
black M
black L
white S
white M
white L
所以我试图删除那些%s%s,而是使用af字符串格式。有人可以表现出如何做到这一点吗?谢谢
蛊毒传说
浏览 150回答 2
2回答
SMILET
>>> 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}')