zoukankan      html  css  js  c++  java
  • python中删除列表中多次重复的元素

    1、

    >>> test1 = ["aa","bb","aa","cc","aa","cc","dd","ee"]
    >>> test1
    ['aa', 'bb', 'aa', 'cc', 'aa', 'cc', 'dd', 'ee']
    >>> "aa" in test1
    True
    >>> test1.count("aa")
    3
    >>> while "aa" in test1:
        test1.remove("aa")
        print(1)
    
        
    1
    1
    1
    >>> test1
    ['bb', 'cc', 'cc', 'dd', 'ee']

    2、

    >>> test1 = ["aa","bb","aa","cc","aa","cc","dd","ee"]
    >>> test2 = list(set(test1))
    >>> test2
    ['bb', 'dd', 'ee', 'aa', 'cc']
    >>> for i in test2:
        if test1.count(i) == 1:
            test2.remove(i)
    
            
    >>> test2
    ['dd', 'aa', 'cc']    ## ???????? 
    
    

    >>> test2 = list(set(test1)) >>> test3 = test2.copy() >>> test2 == test3 True >>> test2 ['bb', 'dd', 'ee', 'aa', 'cc'] >>> test3 ['bb', 'dd', 'ee', 'aa', 'cc'] >>> for i in test2: if test1.count(i) == 1: test3.remove(i) >>> test3 ['aa', 'cc']

    >>> test3
    ['aa', 'cc']
    >>> test1
    ['aa', 'bb', 'aa', 'cc', 'aa', 'cc', 'dd', 'ee']
    >>> test3
    ['aa', 'cc']
    >>> for i in test3:
        while i in test1:
            test1.remove(i)
    
            
    >>> test1
    ['bb', 'dd', 'ee']
  • 相关阅读:
    Python 6 socket编程
    Python 5 面向对象进阶
    Python 4 面向对象
    Python 3 常用模块
    Python基础 2
    Python基础 1
    Django之会议室预预订
    vscode 修改快捷键 (回到上一处光标位置,下一处光标位置)
    C 库函数
    C 库函数
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14223614.html
Copyright © 2011-2022 走看看