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

Python OOP challenge, problem with object and method

 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
 

#监听器里面有2个列表,我做的任何事情都会产生两个列表,有没有办法 在不更改调用 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