[英]How can I print this format in the list of python
假設我在python中有此列表
A = ["(a,1)", "(b,2)", "(c,3)", "(d,4)"]
所以我該如何以以下格式打印出來:
(a,1), (b,2), (c,3), (d,4)
僅使用一行,而不使用for循環更好
當A是str的列表時:
print(', '.join(A))
或更籠統:
print(', '.join(map(str, A)))
在您的情況下,以下代碼將起作用
print(', '.join(A))
您還可以使用正則表達式:
>>> A = ["(a,1)", "(b,2)", "(c,3)", "(d,4)"]
>>> import re
>>> re.sub(r'[\[\]\"]', r"",",".join(A))
'(a,1),(b,2),(c,3),(d,4)'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.