【问题标题】:Printing lists with brackets in Python 3.6在 Python 3.6 中打印带括号的列表
【发布时间】:2017-09-06 22:41:27
【问题描述】:
这个问题与其他问题不同,因为我试图打印带有圆括号的列表,而不是方括号。
例如;我有这个清单:
list_of_numbers = [1, 2, 3, 4, 5]
当你打印出列表时,你会得到:
[1, 2, 3, 4, 5]
我希望印刷版看起来像这样:
(1, 2, 3, 4, 5)
【问题讨论】:
标签:
python
python-3.x
list
tuples
【解决方案1】:
print(tuple(list_of_numbers))
或
print('(%s)' % ', '.join([str(i) for i in list_of_numbers]))
【解决方案2】:
list_of_number_strings = [str(number) for number in list_of_numbers]
list_string = "({})".format(", ".join(list_of_number_strings))
print(list_string)
应该做的伎俩
list_of_number_strings 使用简单的列表推导通过将 list_of_numbers 中的每个元素转换为字符串来创建字符串列表。
然后我们使用简单的字符串格式和连接来创建我们想要打印的字符串。
【解决方案3】:
元组将打印圆括号
print(tuple(list_of_numbers))