Home > Software design >  Python: Intersect 2 lists mucisians with excluding the ratings from the list with basic tools
Python: Intersect 2 lists mucisians with excluding the ratings from the list with basic tools

Time:11-30

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.

CodePudding user response:

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
  • Related