2
 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
 

#the listener contains 2 lists inside, anything I do produces two lists, is there a way to separate the list inside the listener without changing the objects calling the how_many method simultaneously. Thank you!!! in advance

NewCoder
  • 51
  • 4

1 Answers1

1
  • I'm not sure the input JoHn in second wave is a typo or you need to capitalize all input. I assume you need capitalize it.
  • You can use set to deal with the remove the duplicate in mutiple input.

example code:

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'])

result:

5
2
leaf_yakitori
  • 2,232
  • 1
  • 9
  • 21
  • damn, it was too complicated for me to understand, but thanks man! I'll dissect it and try to analyze how it works! – NewCoder Nov 15 '21 at 03:56
  • 1
    @NewCoder if this solution too complicated for you, I could provide a simpler solution. Or you can try to learn `set` ,`|`(something like plus) and `^`(like minus). – leaf_yakitori Nov 15 '21 at 04:01
  • no, I think it's perfect, I'll analyze it so I can learn something from it. :'D sorry I'm just starting. – NewCoder Nov 15 '21 at 04:02