Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.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/3/arrays/13.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 如何删除列表中重复次数小于k的项目_Python_List_Counter - Fatal编程技术网

Python 如何删除列表中重复次数小于k的项目

Python 如何删除列表中重复次数小于k的项目,python,list,counter,Python,List,Counter,在python列表中,我想删除所有重复次数小于“k”的元素。 例如,如果k==3,那么如果我们的列表是: l = [a,b,c,c,c,a,d,e,e,d,d] 那么输出必须是: [c,c,c,d,d,d] 什么是快速的方法(我的数据很大),有什么好的建议吗 这是我编写的代码,但我不认为这是最快和最具Python风格的方式: from collections import Counter l = ['a', 'b', 'c', 'c', 'c', 'a', 'd', 'e', 'e', '

在python列表中,我想删除所有重复次数小于“k”的元素。 例如,如果k==3,那么如果我们的列表是:

l = [a,b,c,c,c,a,d,e,e,d,d]
那么输出必须是:

[c,c,c,d,d,d]
什么是快速的方法(我的数据很大),有什么好的建议吗

这是我编写的代码,但我不认为这是最快和最具Python风格的方式:

from collections import Counter

l = ['a', 'b', 'c', 'c', 'c', 'a', 'd', 'e', 'e', 'd', 'd']

counted = Counter(l)

temp = []
for i in counted:
    if counted[i] < 3:
        temp.append(i)

new_l = []
for i in l:
    if i not in temp:
        new_l.append(i)

print(new_l)
从集合导入计数器
l=['a','b','c','c','a','d','e','e','d','d']
计数=计数器(l)
温度=[]
就我而言:
如果计算[i]<3:
临时附加(i)
新的
对于l中的i:
如果我不是临时工:
新附录(一)
打印(新)

我会使用集合中的计数器:

from collections import Counter
count_dict = Counter(l)
[el for el in l if count_dict[el]>2]
可以使用构造将值映射到计数的字典。然后使用列表理解来筛选大于指定值的计数

from collections import Counter

L = list('abcccadeedd')
c = Counter(L)
res = [x for x in L if c[x] >=3]

# ['c', 'c', 'c', 'd', 'd', 'd']

蛮力选项是获取每个项目的出现次数,然后过滤该输出。
collections.Counter
对象在这里工作得很好:

l = [a,b,c,c,c,a,d,e,e,d,d]
c = Counter(l)

# Counter looks like {'a': 2, 'b': 1, 'c': 3...}

l = [item for item in l if c[item]>=3]
在引擎盖下,
计数器
充当字典,您可以像这样构建自己:

c = {}
for item in l:
    # This will check if item is in the dictionary
    # if it is, add to current count, if it is not, start at 0
    # and add 1
    c[item] = c.get(item, 0) + 1

# And the rest of the syntax follows from here
l = [item for item in l if c[item]>=3]

这个选项有什么缺点吗

l = ['a','b','c','c','c','a','d','e','e','d','d']

res = [ e for e in l if l.count(e) >= 3]

#=> ['c', 'c', 'c', 'd', 'd', 'd']

你试过什么?请给我们您的尝试,以便我们能更好地帮助您。多大是大?1k、1M、1B、1T?提示:使用
计数器
@JordanSinger我尝试过使用“Counter”并对其进行排序,然后找到小于“k”的元素,然后…@Peymamohsenikiasari谢谢你,你能用你的代码更新你的帖子吗?这会持续重建
计数器
的每个元素,但速度不快。它将计数“c”3次。