传递列表和字符串时不需要的输出格式

时间:2014-09-15 00:03:33

标签: python string list formatting output

我的任务是在python中编写一个程序来适应给定的(并且不可更改的)“驱动程序”程序,该程序使得Set类由一个名为“members”的列表的单个变量组成。

我的问题是方法“has_subset()”和“intersect()”的输出无法正确显示。输出中有不必要的括号,逗号和撇号。

这是集合类:

class Set:
def __init__(self):
    self.members = []

def add_element(self, integer):
    if integer not in self.members:
        self.members.append(integer)

def remove_element(self, integer):
    while integer in self.members: self.members.remove(integer)

def remove_all(self):
    self.members = []

def has_element(self, x):
    while x in self.members: return True
    return False

# probably doesnt work, __repr__
def __repr__(self):
    if len(self.members) == 0:
        return "{}"
    return "{" + ", ".join(str(e) for e in self.members) + "}"

#Same as above, probably doesnt work
def __str__(self):
    if len(self.members) == 0:
        return "{}"
    return "{" + ", ".join(str(e) for e in self.members) + "}"

def __add__(self, other):
    counter = 0
    while counter < len(other.members):
        if other.members[counter] not in self.members:
            self.members.append(other.members[counter])
        counter = counter + 1
    return self.members

def intersect(self, x):
    counter = 0
    answer = Set()
    while counter < len(x.members):
        if x.members[counter] in self.members: answer.members.append(x.members[counter])
        counter = counter + 1
    return answer

#No clue if this is what is intended
def has_subset(self, x):
    counter = 0
    while counter < len(x.members):
        if x.members[counter] not in self.members: return False
        counter = counter + 1
    return True

这是驱动程序文件:

    from Set import *

first = Set()
count = 0
while count < 10:
    first.add_element(count)
    count += 1
print(first)

second = Set()
count = 5
while count < 15:
    second.add_element(count)
    count += 1
print(second)    

third = Set()
third.add_element(2)
third.add_element(1)
third.add_element(8)
third.add_element(5)

#Tests has_subset with a set that is a subset
if first.has_subset(third):
    print(third, "is a subset of", first)
else:
    print(third, "is not a subset of", first)

#Tests has_subset with a set that is not a subset
if second.has_subset(third):
    print(third, "is a subset of", second)
else:
    print(third, "is not a subset of", second)

#Tests overloaded +
fourth = first + second
print(first, "+", second, "=\n", fourth)

#Tests intersect
fifth = first.intersect(second)
print(fifth, "is the intersection of", first, "and\n", second)

这是输出:

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
{5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
({2, 1, 8, 5}, 'is a subset of', {0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
({2, 1, 8, 5}, 'is not a subset of', {5, 6, 7, 8, 9, 10, 11, 12, 13, 14})
({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, '+', {5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, '=\n', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
({5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, 'is the intersection of', {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, 'and\n', {5, 6, 7, 8, 9, 10, 11, 12, 13, 14})

注意前两行是如何正确格式化的,没有括号,但是一旦组合了其他字符串,就会发挥不需要的标点符号。如何通过仅编辑Set类来删除此不需要的输出来创建输出?

1 个答案:

答案 0 :(得分:3)

这与您的Set类无关 - 问题在于您打印内容的方式。

我怀疑你使用的是Python 2.x?

如果是这样,当你执行print(third, "is a subset of", second)时,你实际上是在告诉Python打印元组(包含3个元素)。 Python需要并打印parens和逗号,因为这是打印元组的一部分。 print(...)不是Python 2中的函数。

要解决此问题,您可以手动将每个部分加在一起:

print str(third) + ' is a subset of ' + str(second)

...或使用字符串格式:

print '{} is a subset of {}'.format(third, second)

...或在驱动程序文件的顶部添加行from __future__ import print_function以包含Python 3打印语义, print(...)转换为函数并执行你想要的。