Python:Intersect 2 列出了使用基本工具从列表中排除评级的 mucisians

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

尝试将 2 个音乐家列表与他们在每个特定列表中的评分相交。想要找到相似之处:两个列表中都有谁的音乐家,但为此我必须排除评级。 首先,我尝试编写代码来为没有评级的列表找到相同的音乐家,但是输出为空 []。 可能有人可以编辑代码并编写如何从列表中排除评级。

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

我正在考虑将列表转换为字符串,然后比较这些字符串。

修改你的交集函数

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