Python 打印不带括号的集合列表 [英] Python print list of sets without brackets
问题描述
我有一个集合列表(使用 Python).有没有办法在没有set([])"的情况下打印它,而只输出它们所持有的实际值?
I have a list of sets (using Python). Is there a way to print this without the "set([])" stuff around it and just output the actual values they are holding?
现在我为列表中的每个项目得到这样的东西
Right now I'm getting somthing like this for each item in the list
set(['blah', 'blahh' blahhh')]
我希望它看起来更像这样
And I want it to look more like this
blah,blahh,blahhh
推荐答案
方法很多,但我第一个想到的是:
Lots of ways, but the one that occurred to me first is:
s = set([0,1])
", ".join(str(e) for e in s)
将集合中的所有内容转换为字符串,并用逗号将它们连接在一起.显然,您对显示的偏好可能会有所不同,但您可以很高兴地将其传递给 print
.应该在 python 2 和 python 3 中工作.
Convert everything in the set to a string, and join them together with commas. Obviously your preference for display may vary, but you can happily pass this to print
. Should work in python 2 and python 3.
对于集合列表:
l = [{0,1}, {2,3}]
for s in l:
print(", ".join(str(e) for e in s))
这篇关于Python 打印不带括号的集合列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!