在 Python 中打印集合时删除集合标识符 [英] Removing set identifier when printing sets in Python

查看:46
本文介绍了在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆