繁体   English   中英

如何在python列表中打印此格式

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM