[英]Printing lists with brackets in Python 3.6
这个问题与其他问题不同,因为我试图打印带有圆括号而不是方括号的列表。
例如; 我有这个清单:
list_of_numbers = [1, 2, 3, 4, 5]
当您打印出列表时,将得到以下信息:
[1, 2, 3, 4, 5]
我希望印刷版看起来像这样:
(1, 2, 3, 4, 5)
print(tuple(list_of_numbers))
要么
print('(%s)' % ', '.join([str(i) for i in list_of_numbers]))
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中的每个元素转换为字符串来创建字符串列表。 然后,我们使用简单的字符串格式设置和联接来创建要打印的字符串。
元组将打印圆括号
print(tuple(list_of_numbers))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.