Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: Intersect 2 lists mucisians with excluding the ratings from the list with basic tools

Trying to intersect 2 lists of musicians with their rating in each specific list. Want to find similarities: whose musicians who are in both lists, but to do so I have to exclude the rating.
First I tried to write the code to find the same musicians for lists without ratings, however got empty output [].
May be someone can edit the code and write how to exclude rating from the list.

musical_groups_Rock = [
    ["Ad", "Rock", "1"],
    ["John", "Lennon", "2"],
    ["Rivers", "Cuomo", "3"],
    ["Patrick", "Wilson", "4"],
]

musical_groups_Folk = [
    ["John", "Lennon", "1"],
    ["Ad", "Rock", "2"],
    ["Axl", "Rose", "3"],
    ["Jam", "Master", "Jay", "4"],
]

def intersect(list_of_names1, list_of_names2):
    list1 = [" ".join(line) for line in list_of_names1]
    list2 = [" ".join(line) for line in list_of_names2]   
    list3 = [value for value in list1 if value in list2]
    return list3
    pass

output = intersect(musical_groups_Rock, musical_groups_Folk)
print(output[0:3])

I was thinking to convert lists to string and then compare these strings.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Modify your intersection function as such

def intersect(list_of_names1, list_of_names2):
    list1 = [" ".join(line[:-1]) for line in list_of_names1]
    list2 = [" ".join(line[:-1]) for line in list_of_names2]   
    list3 = list(set(list1) & set(list2))
    return list3
    pass
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading