簡體   English   中英

在Python 3.6中打印帶有括號的列表

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM