【问题标题】:Python: Intersect 2 lists mucisians with excluding the ratings from the list with basic toolsPython:Intersect 2 列出 mucisians 并使用基本工具从列表中排除评级
【发布时间】:2026-01-06 03:45:01
【问题描述】:

尝试将 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])

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

【问题讨论】:

    标签: python-3.x numpy intersect


    【解决方案1】:

    修改你的交集函数

    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
    

    【讨论】: