Python-将元组列表转换为字符串
将元组列表转换为字符串的最Pythonic 方法是什么?
我有:
[(1,2), (3,4)]
并且我想要:
"(1,2), (3,4)"
我对此的解决方案是:
l=[(1,2),(3,4)]
s=""
for t in l:
s += "(%s,%s)," % t
s = s[:-1]
是否有更Pythonic的方法来做到这一点?
Which is the most pythonic way to convert a list of tuples to string?
I have:
[(1,2), (3,4)]
and I want:
"(1,2), (3,4)"
My solution to this has been:
l=[(1,2),(3,4)]
s=""
for t in l:
s += "(%s,%s)," % t
s = s[:-1]
Is there a more pythonic way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你可能想使用一些简单的东西,例如:
..这很方便,但不能保证正常工作
you might want to use something such simple as:
.. which is handy, but not guaranteed to work correctly
您可以尝试这样的操作(另请参阅 ideone.com):
You can try something like this (see also on ideone.com):
怎么样:
How about:
我认为最Pythonic的解决方案是
I think the most Pythonic solution is
怎么样
How about
我认为这非常巧妙:
尝试一下,它对我来说就像一个魅力。
I think this is pretty neat:
Try it, it worked like a charm for me.
还有三个:)
Three more :)