Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从列表中删除唯一的元素_Python_List_Unique - Fatal编程技术网

Python 从列表中删除唯一的元素

Python 从列表中删除唯一的元素,python,list,unique,Python,List,Unique,我在解决下一个问题时遇到了一些问题: 我们有一个元素(整数)列表,我们应该返回一个只包含该列表中非唯一元素的列表。不改变列表的顺序 我认为最好的方法是删除或删除所有唯一的元素 请注意,我刚刚开始学习python,只想要最简单的解决方案 这是我的密码: def checkio(data): for i in data: if data.count(i) == 1: #if element seen in the list just ones, we delet thi

我在解决下一个问题时遇到了一些问题:

我们有一个元素(整数)列表,我们应该返回一个只包含该列表中非唯一元素的列表。不改变列表的顺序 我认为最好的方法是删除或删除所有唯一的元素

请注意,我刚刚开始学习python,只想要最简单的解决方案

这是我的密码:

def checkio(data):
    for i in data:
        if data.count(i) == 1:    #if element seen in the list just ones, we delet this el
            ind = data.index(i)
            del data[ind]
    return data

您可以实现一个
OrderedCounter
,例如:

from collections import OrderedDict, Counter

class OrderedCounter(Counter, OrderedDict): 
    pass

data = [1, 3, 1, 2, 3, 5, 8, 1, 5, 2]

duplicates = [k for k, v in OrderedCounter(data).items() if v > 1]
# [1, 3, 2, 5]
因此,计算每个值的出现次数,然后如果其频率超过一个,则进行过滤。继承自
orderedict
意味着保留原始元素的顺序


通过注释,您希望保留所有重复的元素,所以您可以预先构建一组重复条目,然后重新迭代原始列表,例如:

from collections import Counter

data = [1, 3, 1, 2, 3, 5, 8, 1, 5, 2]
duplicates = {k for k, v in Counter(data).items() if v > 1}
result = [el for el in data if el in duplicates]
# [1, 3, 1, 2, 3, 5, 1, 5, 2]
试试这个:

>>> a=[1,2,3,3,4,5,6,6,7,8,9,2,0,0]
>>> a=[i for i in a if a.count(i)>1]
>>> a
[2, 3, 3, 6, 6, 2, 0, 0]
>>> a=[1, 2, 3, 1, 3]
>>> a=[i for i in a if a.count(i)>1]
>>> a
[1, 3, 1, 3]
>>> a=[1, 2, 3, 4, 5]
>>> a=[i for i in a if a.count(i)>1]
a
[]

通过反向迭代列表,可以使函数正常工作:

def checkio(data):
    for index in range(len(data) - 1, -1, -1):
        if data.count(data[index]) == 1:
            del data[index]
    return data

print(checkio([3, 3, 5, 8, 1, 4, 5, 2, 4, 4, 3, 0]))
[3, 3, 5, 4, 5, 4, 4, 3]
print(checkio([1, 2, 3, 4]))
[]

这是可行的,因为它只删除列表中已经迭代过的部分中的数字。

按照您已经开始的步骤,迭代整数列表,但不计算或删除元素,尝试只测试元素是否已被看到,将其附加到重复元素列表中:

def checkio(data):
    elements = []
    duplicates = []
    for i in data:
        if i not in elements:
            elements.append(i)
        else:
            if i not in duplicates:
                duplicates.append(i)
    return duplicates

d = [1, 3, 1, 2, 3, 5, 8, 1, 5, 2]

print (checkio(d))
#[1, 3, 5, 2]

只是我用了列表理解法

def checkio(data):
    a=[i for i in data if data.count(i)>1]
    return a
print checkio([1,1,2,2,1,1,1,3,4,5,6,7,8]) 
是的,现在发布这个帖子有点晚了,但我只是想把它放到网上供其他人使用

 numbers = [1, 1, 1, 1, 3, 4, 9, 0, 1, 1, 1]
 x=set(numbers)
 print(x)

您也可以使用set关键字来获得所需的解决方案。

每次在while循环中修改列表时,我都使用整数和bool进行检查

rechecks = 1
runscan = True
while runscan == True:
    for i in data:
         if data.count(i) <2:
             data.remove(i)
             rechecks+=1
            #need to double check now
    if rechecks >0:
        runscan = True
        rechecks-=1
    else:
        runscan = False       
return data
重新检查=1
runscan=True
当runscan==True时:
对于数据中的i:
如果数据计数(i)为0:
runscan=True
重新检查-=1
其他:
runscan=False
返回数据

生成新列表不是更容易吗

def unique_list(lst):
    new_list = []
    for value in lst:
        if value not in new_list:
            new_list.append(value)
    return new_list

lst = [1,2,3,1,4,5,1,6,2,3,7,8,9]
print(unique_list(lst))

打印[1,2,3,4,5,6,7,8,9]

而不是删除项目,创建一个具有唯一值的新列表怎么样?@JoErNanO我需要一个具有非唯一元素的列表(所有元素都具有原始顺序)只要使用set()函数将列表转换为set,您就会得到所需的输出。如果您想要列表中的最终答案,只需使用list()函数将该集合转换为列表。我定义了所有非唯一元素。示例1:[1,2,3,1,3]-->>1和3个非唯一元素和结果将是[1,3,1,3]。示例2:[1,2,3,4,5]->>非唯一元素和结果将为[]d=[1,3,1,2,3,5,8,1,5,2]结果应为-->>>[1,3,1,2,3,5,1,5,2](仅删除唯一的“8”),这具有二次运行时复杂性。更好的(
O(n)
)解决方案是。
def unique_list(lst):
    new_list = []
    for value in lst:
        if value not in new_list:
            new_list.append(value)
    return new_list

lst = [1,2,3,1,4,5,1,6,2,3,7,8,9]
print(unique_list(lst))