[英]Printing lists with brackets in Python 3.6
This question is different from others because I am trying to print lists that have round brackets, not the square ones. 这个问题与其他问题不同,因为我试图打印带有圆括号而不是方括号的列表。
For example; 例如; I have this list:
我有这个清单:
list_of_numbers = [1, 2, 3, 4, 5]
When you print out the list, you get this: 当您打印出列表时,将得到以下信息:
[1, 2, 3, 4, 5]
I want the printed version to look something like this: 我希望印刷版看起来像这样:
(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)
Should do the trick 应该做的把戏
The list_of_number_strings uses a simple list comprehension create a list of strings by casting every element in list_of_numbers to a string. list_of_number_strings使用简单的列表理解,通过将list_of_numbers中的每个元素转换为字符串来创建字符串列表。 Then we use simple string formatting and a join to create the string we want to print.
然后,我们使用简单的字符串格式设置和联接来创建要打印的字符串。
元组将打印圆括号
print(tuple(list_of_numbers))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.