【问题标题】:I want to make a function that takes a list and returns the same list but without the duplicate elements, what's wrong with this program?我想创建一个函数,它接受一个列表并返回相同的列表但没有重复的元素,这个程序有什么问题?
【发布时间】:2021-10-31 20:34:21
【问题描述】:

我想创建一个函数,它接受一个列表并返回相同的列表但没有重复的元素

def func(list1):
    for i in range(len(list1)):
        list2 = [x for x in list1 if x != list1[i]]
        if list1[i] in list2:
            list1.remove(i)
            return list1
        

mylist = [1,2,3,4]
func(mylist)

【问题讨论】:

    标签: python list function


    【解决方案1】:

    已经有很多答案展示了如何正确实现这个任务,但没有一个讨论你的实际代码。

    解构你的代码

    希望这将有助于理解您的代码为何不起作用。这是您的代码,添加了 cmets:

    def func(list1):
        for i in range(len(list1)):
            list2 = [x for x in list1 if x != list1[i]]
    

    在这里您构造一个列表list2,其中不包含list1[i]。

            if list1[i] in list2:
    

    这个if 声明总是错误的,通过list2 的构造。

                list1.remove(i)
    

    即使您修复了if 语句的逻辑,list1.remove(i) 也会带来麻烦:尝试在迭代列表时删除列表元素会使其余迭代的索引无效。最好避免这样做,而是将结果构建到一个新列表中。

    此外,list1.remove(i) 正在 list1 中寻找值 i,并将删除它,无论它首先找到它。正如这段代码所期望的那样,它并没有删除第 i 个元素。

                return list1
    

    而且这个 return 语句可能并不意味着缩进这么深,它应该与顶部的 for 语句处于同一级别。

    工作代码更接近你的逻辑

    虽然我认为这不是最好的解决方案 - 其他答案提供了更好的解决方案 - 这是一个受您的代码启发的解决方案,它有效。

    def func(list1):
        list2 = []  # We'll append elements here
        for item in list1:  # more pythonic way to loop over a list
            if item not in list2:
                list2.append(item)
        return list2
    

    我的首选解决方案

    好的,所以现在我在其他答案中复制信息,但使用 Python 3.7(*) 或更新版本,dict 保证保持插入其中的元素的顺序,我会使用 @Pedro Maia 的解决方案:

    def func(list1):
        return list(dict.fromkeys(list1))
    

    事实上,我最近在我的代码库中使用了这种技术。

    在旧版本的 Python 中,字典没有顺序。但是,自 Python 2.7 以来,collections 库中一直存在 OrderedDict 类,因此此代码适用于任何版本的 Python >= 2.7:

    from collections import OrderedDict
    def func(list1):
        return list(OrderedDict.fromkeys(list1))
    

    如果您不熟悉它们,请查看collections library 和itertools library,它们为各种问题提供了非常有用的帮助。

    (*) 根据collections.OrderedDict 的手册,Python dicts 只保证从 Python 3.7 开始订购,即使它们实际上是在 Python 3.6 中开始订购的,至少对于 CPython 实现是这样。另见:Are dictionaries ordered in Python 3.6+?

    【讨论】:

      【解决方案2】:

      你可以这样做:

      def remove_duplicates(l):
          return list(set(l))
      

      现在remove_duplicates([1,1,2,3,3,4,5,6,6,7]) 将返回[1, 2, 3, 4, 5, 6, 7]。

      【讨论】:

        【解决方案3】:

        让我们假设这个输入l = [1,2,3,2,7,3,4,4,7,1]

        删除重复而不保持顺序

        使用 python set:

        list(set(l))
        

        输出:[1, 2, 3, 4, 7]

        维持秩序

        使用字典(或collections.Counter)

        from collections import Counter
        list(Counter(l))
        

        输出:[1, 2, 3, 7, 4]

        【讨论】:

          【解决方案4】:

          使用一组:

          mylist = [1,2,3,1,3,2,4]
          mylist = set(mylist)
          print(mylist)
          

          输出:

          {1, 2, 3, 4}
          

          为了保持顺序,你可以这样做:

          mylist = list(dict.fromkeys(mylist))
          

          不需要任何库

          【讨论】:

            【解决方案5】:

            在python中我们可以使用set来移除重复的元素:

            def removeDuplicates(l):
                return list(set(l))
            
            
            mylist = [1, 2, 3, 4, 2, 3, 4, 2, 3, 4]
            
            newList = removeDuplicates(mylist)
            
            print(newList)
            

            【讨论】:

              猜你喜欢
              • 2022-11-20
              • 2017-02-03
              • 2016-06-21
              • 1970-01-01
              • 1970-01-01
              • 2015-05-07
              • 2011-05-12
              • 1970-01-01
              • 2020-07-24
              相关资源
              最近更新 更多