python - 如何使用 f{} 将列表转换为不同的格式并在它们之间添加字符串
问题描述
名单如下
second_list = ['C']
first_list = ['A', 'B']
third_list = ["D"]
预期结果如下
the firstlist is A,B the second_list is C and thirdlist is D
伪代码
print (f'the firstlist is {j for j in first_list } the second_list is {for j for j in second_list} and thirdlist is {for j for j in third_list')
解决方案
您的伪代码非常接近,但您最好使用string.join
它来创建逗号分隔的列表(f 字符串中的表达式可以任意复杂):
second_list = ['C']
first_list = ['A', 'B']
third_list = ["D"]
print (f'the firstlist is {",".join(first_list)} the second_list is {",".join(second_list)} and thirdlist is {",".join(third_list)}')
输出:
the firstlist is A,B the second_list is C and thirdlist is D
推荐阅读
- xamarin - Xamarin IOS NFCNdefReaderSession.ReadingAvailable 为假
- android - 如何防止阿拉伯语从右到左对齐?
- c# - 如何在 C# 中使用 SerialPort 的 dataReceived 事件
- mysql - 排序行以均匀分布公共记录
- javascript - 获取数据表中单元格的值
- javascript - 单个对象的时间刻度 - 条不居中(chart.js)
- node.js - 节点邮件程序在文本中嵌入链接
- css - 翻转 SVG 图形并添加边距
- class - C ++将类型ref类的变量列表发送到本机类
- java - 如何使用反射 API 调用 Setter 方法,但没有提及 FieldName 和 MethodName 之类的内容