开发者社区 问答 正文

Python删除列表列表中具有重复字段的列表

当列表中的字段在python3中重复时,我希望能够从列表中删除元素。

IE浏览器:

当第二个字段重复时,从以下列表中删除。从

`[[[“ John”,“ France”],[“ Mike”,“ France”],[“ Ana”,“意大利”]]

`[[[“ John”,“ France”],[“ Ana”,“意大利”]]

编辑:我尝试了以下循环,但是我期待有一个更有效的方法。

for element in consult_array:
    for other_elements in consult_array:
        if element[1] == other_elements[1]:
            if element != other_elements:
                consult_array.pop(element)

问题来源:stackoverflow

展开
收起
is大龙 2020-03-25 09:17:10 2918 分享 版权
2 条回答
写回答
取消 提交回答
  • 代码改变世界,我们改变代码

    提供个思路:

    为什么不充份利用,set集合的,去重功能呢?

    2020-04-14 09:18:41
    赞同 展开评论
  • data = [["John", "France"], ["Mike", "France"], ["Ana", "Italy"]]
    
    output = []
    already_seen_countries = set()
    
    for item in data:
        country = item[1]
        if country not in already_seen_countries:
            output.append(item)
            already_seen_countries.add(country)
    
    print(output)  # [['John', 'France'], ['Ana', 'Italy']]
    

    回答来源:stackoverflow

    2020-03-25 09:17:17
    赞同 展开评论
问答分类:
问答地址: