python - Is there a way to insert a list into the .format method in python?
问题描述
I want to do something like:
list = [1,2,3]
valstring = "one: {}, two: {}, three: {}".format(list)
print(valstring)
result:
one: 1, two: 2, three: 3
Is something like this possible? I know I can achieve a similar result by using .format(list[0], list[1], list[2])
but I have a long list so I'd prefer not to.
解决方案
是的,使用星号( Python 中*
所谓的“解包”):
lst = [1,2,3]
valstring = "one: {}, two: {}, three: {}".format(*lst)
print(valstring)
印刷:
one: 1, two: 2, three: 3
推荐阅读
- css - 如何在 1 个 div 类中应用 2 个 CSS 样式?
- image - 替换独立 ML Kit SDK 的 FirebaseVisonImageMetadata
- apache-kafka - 我在 vm 上的 Kafka 代理集群无法从 kafka-console-producer 接收记录
- swift - iCloud中照片的Swift UIImagePickerController问题并允许编辑
- python - Pytesseract 错误 - 处理期间没有此类文件或目录错误
- css - 如何使用现有的材质 ui 图标构建自定义图标?
- django - 不显示字段 drf 序列化程序
- html - RSS/XML 解析
- javascript - 如何将 Material-table 与 Material-UI Dialog 结合起来?(反应JS)
- python - 有没有办法在 DJANGO 的模板中显示元素的计数器?