簡體   English   中英

傳遞列表和字符串時不需要的輸出格式

[英]Unwanted output formatting when passing lists and strings

我受命用python編寫一個程序以適合給定的(且不可更改的)“驅動程序”程序,該程序使Set類由一個稱為“成員”的列表的單個變量組成。

我的問題是方法“ 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類來刪除此不需要的輸出來創建輸出?

這與Set類無關—問題在於您如何打印內容。

我懷疑您使用的是Python 2.x?

如果是這樣,那么當您執行print(third, "is a subset of", second) ,您實際上是在告訴Python打印元組(包含3個元素)。 Python強制執行並打印括號和逗號,因為這是打印元組的一部分。 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中添加這一行from __future__ import print_function以包含Python 3打印語義,這確實print(...)轉換為函數並滿足您的要求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM