我的任务是用python编写一个程序来适应一个给定的(且不可更改的)“driver”程序,该程序生成一个Set类,该类由一个名为“members”的列表中的单个变量组成
我的问题是方法“has\u subset()”和“intersect()”的输出不能正确显示。输出中有不必要的括号、逗号和撇号。你知道吗
下面是set类:
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类来删除不需要的输出来创建输出?你知道吗
这与你设置的类无关,问题在于你如何打印你的内容。你知道吗
我怀疑你在用Python2.x?你知道吗
如果是这样,那么在执行
print(third, "is a subset of", second)
时,实际上是在告诉Python打印元组(包含3个元素)。Python同意,并打印paren和逗号,因为这是打印元组的一部分。print(...)
不是python2中的函数。你知道吗要解决此问题,您可以手动将每个零件添加到一起:
…或使用字符串格式:
…或者在驱动程序文件的顶部添加一行
from __future__ import print_function
,以包含python3打印语义,它将print(...)
转换为函数并执行您想要的操作。你知道吗相关问题 更多 >
编程相关推荐