Python OOP的挑战,对象和方法的问题


class Song:
def __init__(self, title, artist):
self.title = title
self.artist = artist

def how_many(self, listener):
print(listener) 


obj_1 = Song("Mount Moose", "The Snazzy Moose")
obj_1.how_many(['John', 'Fred', 'Bob', 'Carl', 'RyAn'])
obj_1.how_many(['Luke', 'AmAndA', 'JoHn']) here

#监听器包含两个列表,我所做的任何事情都会产生两个列表在侦听器中分离列表,而不更改调用how_many的对象方法。非常感谢。提前

  • 我不确定第二波中的输入JoHn是打字错误,或者您需要将所有输入大写。我想你需要把它大写
  • 您可以使用set来处理多个输入中的重复消除

示例代码:

class Song:
def __init__(self, title, artist):
self.title = title
self.artist = artist
self.linstener = set()
def how_many(self, listener):
listener = [ele.capitalize() for ele in listener]
print(len((self.linstener | set(listener)) ^ self.linstener))
self.linstener.update(listener)
# print(listener) 
obj_1 = Song("Mount Moose", "The Snazzy Moose")
obj_1.how_many(['John', 'Fred', 'Bob', 'Carl', 'RyAn'])
obj_1.how_many(['Luke', 'AmAndA', 'JoHn'])

结果:

5
2