在 Python 中打印集合时删除集合标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15328788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:50:10  来源:igfitidea点击:

Removing set identifier when printing sets in Python

pythonpython-2.7set

提问by johns4ta

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.

我试图打印出一个集合的内容,当我这样做时,我在打印输出中得到了集合标识符。例如,这是我的输出set(['a', 'c', 'b', 'e', 'd', 'f', 'gg', 'ff', 'jk'])“对于下面的代码。我想摆脱这个词set。我的代码很简单,在下面。

infile = open("P3TestData.txt", "r")
words = set(infile.read().split())
print words

Here is my output again for easy reference: set(['a', 'c', 'b', 'e', 'd', 'f', 'gg', 'ff', 'jk'])

这是我的输出,以供参考: set(['a', 'c', 'b', 'e', 'd', 'f', 'gg', 'ff', 'jk'])

采纳答案by Martijn Pieters

You could convert the set to a list, just for printing:

您可以将集合转换为列表,仅用于打印:

print list(words)

or you could use str.join()to join the contents of the set with a comma:

或者您可以使用str.join()逗号连接集合的内容:

print ', '.join(words)

回答by slezica

The printstatement uses set's implementation of __str__(). You can:

print语句使用set的实现__str__()。你可以:

  1. Roll out your own printing function, instead of using print. A simple way to get a nicer formatting may be to use list's implementation of __str__()instead:

    print list(my_set)

  2. Override the __str__()implementation in your own setsubclass.

  1. 推出您自己的打印功能,而不是使用print. 获得更好格式的一种简单方法可能是使用list的实现__str__()

    print list(my_set)

  2. 覆盖__str__()您自己的set子类中的实现。

回答by dawg

You can do this if you want the curly braces:

如果你想要花括号,你可以这样做:

>>> s={1,2,3}
>>> s
set([1, 2, 3])
>>> print list(s).__str__().replace('[','{').replace(']','}')
{1, 2, 3}

Or, use format:

或者,使用格式:

>>> print '{{{}}}'.format(', '.join(str(e) for e in set([1,'2',3.0])))
{3.0, 1, 2}

回答by aaaantoine

If printing a set of numbers in Python 3, you can alternatively use slicing.

如果在 Python 3 中打印一组数字,您也可以使用切片。

Python 3.3.5
>>> s = {1, 2, 3, 4}
>>> s
{1, 2, 3, 4}
>>> str(s)[1:-1]
'1, 2, 3, 4'

This doesn't translate well when porting back to Python2...

移植回 Python2 时,这不能很好地转换...

Python 2.7.6
>>> s = {1, 2, 3, 4}
>>> str(s)[1:-1]
'et([1, 2, 3, 4]'
>>> str(s)[5:-2]
'1, 2, 3, 4'

On the other hand, to join()integer values you have to convert to string first:

另一方面,对于join()整数值,您必须先转换为字符串:

Python 2.7.6
>>> strings = {'a', 'b', 'c'}
>>> ', '.join(strings)
'a, c, b'
>>> numbers = {1, 2, 3, 4}
>>> ', '.join(numbers)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, int found
>>> ', '.join(str(number) for number in numbers)
'1, 2, 3, 4'

This is still more correct than slicing, however.

然而,这仍然比切片更正确。

回答by cs01

This subclass works for numbers and characters:

这个子类适用于数字和字符:

class sset(set):
    def __str__(self):
        return ', '.join([str(i) for i in self])

print set([1,2,3])
print sset([1,2,3])

outputs

产出

set([1, 2, 3])
1, 2, 3