【问题标题】:If element is repeated in list remove element from that list [duplicate]如果元素在列表中重复从该列表中删除元素[重复]
【发布时间】:2021-09-25 13:20:39
【问题描述】:
my_list = [1,2,3,4,2,3]

所以我想要的列表应该是这样的

new_list = [1,4]

重复值应与原始值一起删除

尝试-

output = [] 
for x in my_list: 
    if x not in output: 
        output.append(x) 
print(output)

【问题讨论】:

  • 很高兴看到您的尝试以及您遇到的问题。
  • output = [] for x in my_list: if x not in output: output.append(x) print(output)
  • @RahulMaurya - 将该尝试添加到问题本身

标签: python list


【解决方案1】:

一种选择是使用collections.Counter,然后过滤掉多个出现的项目:

[v for v, c in collections.Counter([1, 2, 3, 4, 2, 3]).items() if c == 1] 

Counter 将创建一个包含以下项目的字典:Counter({1: 1, 2: 2, 3: 2, 4: 1})

【讨论】:

    【解决方案2】:

    您可以使用collections.Counter,如下所示:

    >>> from collections import Counter
    >>> my_list = [1,2,3,4,2,3]
    >>> dct_cnt = Counter(my_list)
    >>> [k for k,v in dct_cnt.items() if v ==1]
    [1, 4]
    
    

    【讨论】:

    • O(n) 空间和时间。应该工作。
    【解决方案3】:

    您可以使用列表的.count() 方法以及列表解析。

    例如:

    my_list = [1,2,3,4,2,3]
    new_list = [x for x in my_list if l.count(x) == 1]
    print(new_list) # Prints [1,4]
    

    【讨论】:

    • n^2 复杂度,不好。
    【解决方案4】:

    所有方法都很好 我想添加一种不同的方法

    def duplicate_deleter(list_object):
        set_obj=set(list_object)
        result=[]
        for i in set_obj:
            if list_object.count(i)==1:
                result.append(i)
        return result
    

    【讨论】:

      【解决方案5】:

      如果没有集合,您可以使用字典来计算列表中项目的频率,然后只保留频率足够低的项目。此示例在 O(n+m) 中运行,其中 n 是原始列表中元素的数量,m 是唯一元素的数量。

      lst = [1,2,2,3,4,5,4]
      freq = dict()
      for el in lst:
          if el in freq:
              freq[el] += 1
          else:
              freq[el] = 1
      print([el for el, f in freq.items() if f < 2])
      

      【讨论】:

        【解决方案6】:

        您可以创建一个程序,为每个重复元素弹出 x 次列表,其中 x 等于列表中重复元素的计数。

        my_list = [1,2,3,4,2,3]
        #Sort the list so the index matches the count arrays index
        my_list.sort()
        #Create the count array using set to count each unique element
        count = [my_list.count(x) for x in set(my_list)]
        #Create another count to cycle through the count array
        count1 = 0
        #Create a popped variable to keep record of how many times the list has been altered
        popped = 0
        while (count1 < len(count)):
            if count[count1] > 1:
                #Create a variable to pop r number of times depending on the count
                r = 0
                while (r < count[count1]):
                    #If the list has already been popped, the index to be examined has to be the previous one.
                    #For example, after the 2's have been popped we want it to check the first 3
                    #which is now in the count1-1 index.
                    my_list.pop((count1 - popped))
                    r = r+1
                popped = popped + 1
                count1 += 1
                print(my_list)
            else:
                count1 += 1
        

        【讨论】:

          猜你喜欢
          • 2015-01-05
          • 1970-01-01
          • 2012-05-09
          • 1970-01-01
          • 2018-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-19
          相关资源
          最近更新 更多