【问题标题】:How to find unique elements in a list in python? (Without using set)如何在python的列表中查找唯一元素? (不使用集合)
【发布时间】:2016-02-21 07:11:27
【问题描述】:

编写一个接受输入列表并返回新列表的函数 仅包含唯一元素(元素应该只出现 列表中的一次,并且必须保留元素的顺序 作为原始列表。 )。

def unique_elements (list):
    new_list = []
    length = len(list)
    i = 0
    while (length != 0):
        if (list[i] != list [i + 1]):
            new_list.append(list[i])
        i = i + 1
        length = length - 1
    '''new_list = set(list)'''
    return (new_list)

#Main program
n = int(input("Enter length of the list: "))
list = []
for i in range (0, n):
    item = int(input("Enter only integer values: "))
    list.append(item)
print ("This is your list: ", list)
result = unique_elements (list)
print (result)

我被这个错误困住了:

IndexError: 列表索引超出范围

【问题讨论】:

  • 请注意,list 是关键字
  • @Obsidian no list 不是关键字
  • List 是内置函数的名称。可以重新定义,但不推荐。
  • @helloV 对不起。意味着一个内置函数
  • 您的列表索引范围错误是由于 i 正在遍历所有元素索引,因此当它位于最后一个元素上时,i+1 将超出范围。但更大的问题是您只检查相邻元素的重复项。如果您的列表是[1, 2, 1] 怎么办?你的算法将返回所有三个元素,我不相信你想要。

标签: python python-2.7 python-3.x


【解决方案1】:

这是最简单的方法:

a = [1, 2, 2, 3]
b = []
for i in a:
    if i not in b:
        b.append(i)
print (b)
[1, 2, 3]

【讨论】:

  • 问题是代码有什么问题?不要求新的解决方案。
  • @helloV 我对 OP 的理解是想出一个问题的答案,即:编写一个接受输入列表并返回一个仅包含唯一元素的新列表的函数(元素应该列表中只出现一次,并且元素的顺序必须保留为原始列表。)。
【解决方案2】:

您的代码的问题是您循环 length 次,但检查 list[i]list[i+1],因此访问超过输入列表末尾的元素(例如,在具有 6 个元素的列表中有 6- 1=5 对连续元素)。

您的代码的第二个问题是,只有一个元素 [1] 的输入应该作为输出 [1] 给出,即使该元素与其他元素没有什么不同。输入文本意味着您应该删除与已经存在的其他元素相同的元素,而不是保留与下一个不同的元素。

另一个问题是您只检查 consecutive 重复,即给定输入列表[1, 2, 1, 2],您的逻辑不会检测到任何重复...在这种情况下看起来像练习需要作为[1, 2] 的输出。

执行此操作的简单算法的跟踪是

for each element in input
   if the element has not been included in output
       add the element to the end of output

还要注意,要检查一个元素是否存在于列表中,Python 提供了in 运算符(例如if x in output: ...),它可以为您节省该部分的显式循环。

作为旁注,在 Python 中命名输入参数 list 被认为是不好的做法,因为 list 是预定义函数的名称,而您的参数隐藏了它。

【讨论】:

    【解决方案3】:

    不使用集合的O(n)解:

    >>> from collections import Counter, OrderedDict
    >>> class OrderedCounter(Counter, OrderedDict):
    ...     pass
    ... 
    >>> lst = [1, 2, 2, 3, 4, 5, 4]
    >>> [x for x,c in OrderedCounter(lst).items() if c==1]
    [1, 3, 5]
    

    【讨论】:

      【解决方案4】:

      单行实现:

      list = [100, 3232, 3232, 3232, 57, 57, 90]
      new_list = []
      
      [new_list.append(x) for x in list if x not in new_list]
      
      print(new_list)
      

      打印:

      [100, 3232, 57, 90]
      

      【讨论】:

      • 使用列表推导来避免常规 for 循环和 if 语句的额外行是一个坏主意。您构建了一个额外的 None 值列表,只是为了再次将其丢弃。
      【解决方案5】:

      有问题的行是 > if (list[i] != list [i + 1]):

      原因: 想象一下,您的列表有 4 个元素。

      例如:mylist = [ 1, 2,2,3]。

      mylist[i] != mylist [i + 1]

      在最后一次迭代中,'i' 将是 4 ,所以 i + 1 将是 5。

      该列表中没有这样的第 5 个索引,因为列表索引从零开始计数。

      我的列表[0] = 1

      我的列表[1] = 2

      我的列表[2] = 2

      我的列表[3] = 3

      mylist[4] = 无索引

      mylist[5] = 无索引

          def unique_elements (list):
              new_list = []
      
         # Replace the while with a for loop** 
      
              for i in list:
                if i not in new_list:
                  new_list.append(i)
      
      
              return (new_list)
      
          #Main program
          n = int(input("Enter length of the list: "))
          list = []
          for i in range (0, n):
              item = int(input("Enter only integer values: "))
              list.append(item)
          print ("This is your list: ", list)
          result = unique_elements (list)
          print (result)
      

      【讨论】:

        【解决方案6】:
        l = [1, 2, 2, 3,4,5,6,5,7,8]
        myList = []
        [ myList.append(item) for item in l if item not in myList]
        print(myList)
        

        【讨论】:

          【解决方案7】:

          使用集合是最好的 IMO, 接受的答案是最简单的, 使用 dict 添加另一种方法,您也可以在其中检查频率,

          
          text = [100, 3232, 3232, 3232, 57, 57, 90]
          # create empty dictionary
          freq_dict = {}
           
          # loop through text and count words
          for word in text:
              # set the default value to 0
              freq_dict.setdefault(word, 0)
              # increment the value by 1
              freq_dict[word] += 1
           
          unique_list = [key for key,value in freq_dict.items()]
          
          print(unique_list )
          
          print(freq_dict )
          
          [100, 3232, 57, 90]
          {100: 1, 3232: 3, 57: 2, 90: 1}
          
          [Program finished] 
          

          您还可以根据要求按值打印。

          【讨论】:

            【解决方案8】:

            因此,如果您位于最后一个元素,那么 if (list[i] != list [i + 1]): 将尝试从列表中取出一个不存在的值,这就是错误的原因显示列表超出范围。

            【讨论】:

            【解决方案9】:

            您可以使用集合来解决这个问题,而集合理解语法在大多数情况下会被忽略。就像列表集也可以使用理解生成一样。

            elements = [1, 2, 3, 3, 5, 7, 8, 7, 9]
            unique_elements = {element for element in elements}
            print(unique_elements)
            

            【讨论】:

              【解决方案10】:

              为使用 Python 3.7 的人找到了 easier approach

              使用a dictionary

              list(dict.fromkeys(mylist))
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2010-11-16
                • 2023-03-16
                • 1970-01-01
                • 2019-11-11
                • 1970-01-01
                • 2020-04-12
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多