在 Python 中打印集合时删除集合标识符 [英] Removing set identifier when printing sets in Python
问题描述
我试图打印出一个集合的内容,当我这样做时,我在打印输出中得到了集合标识符.例如,这是我的输出 set(['a', 'c', 'b', 'e', 'd', 'f', 'gg', 'ff', 'jk'])
"为下面的代码.我想去掉set
这个词.我的代码很简单,在下面.
I am trying to print out the contents of a set and when I do, I get the set identifier in the print output. For example, this is my output set(['a', 'c', 'b', 'e', 'd', 'f', 'gg', 'ff', 'jk'])
" for the code below. I want to get rid of the word set
. My code is very simple and is below.
infile = open("P3TestData.txt", "r")
words = set(infile.read().split())
print words
这里是我的输出以供参考: set(['a', 'c', 'b', 'e', 'd', 'f', 'gg', 'ff','jk'])
Here is my output again for easy reference: set(['a', 'c', 'b', 'e', 'd', 'f', 'gg', 'ff', 'jk'])
推荐答案
您可以将集合转换为列表,仅用于打印:
You could convert the set to a list, just for printing:
print list(words)
或者你可以使用 str.join()
用逗号连接集合的内容:
or you could use str.join()
to join the contents of the set with a comma:
print ', '.join(words)
这篇关于在 Python 中打印集合时删除集合标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!