首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将% formatting更改为f-string

将% formatting更改为f-string
EN

Stack Overflow用户
提问于 2018-07-10 04:15:51
回答 1查看 94关注 0票数 3
代码语言:javascript
复制
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 S
black M
black L
white S
white M
white L

因此,我正在尝试删除这些%s %s,而不是使用f字符串格式。有没有人能告诉我这是怎么做的。谢谢

EN

回答 1

Stack Overflow用户

发布于 2018-07-10 04:17:36

代码语言:javascript
复制
>>> colors = ['black', 'white']
>>> sizes = ['S', 'M', 'L']
>>> for c in colors:
...    for s in sizes:
...        print(f'{c} {s}')

另一种方法是使用itertools.product

代码语言:javascript
复制
>>> for c, s in itertools.product(colors, sizes):
...     print(f'{c} {s}')   
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51253372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档