Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.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列表按每个唯一值获取前n个元素_Python_List_Unique - Fatal编程技术网

Python列表按每个唯一值获取前n个元素

Python列表按每个唯一值获取前n个元素,python,list,unique,Python,List,Unique,我有一个类似的清单 [('10', '100'), ('11', '100'), ('18', '108'), ('22', '100'), ('12', '102'), ('15', '104'), ('21', '100'), ('25', '108'), ('20', '102'), ('24', '104'), ('105', '108'), ('35', '100'),

我有一个类似的清单

    [('10', '100'),
     ('11', '100'),
     ('18', '108'),
     ('22', '100'),
     ('12', '102'),
     ('15', '104'),
     ('21', '100'),
     ('25', '108'),
     ('20', '102'),
     ('24', '104'),
     ('105', '108'),
     ('35', '100'),
     ('14', '104'),
     ('96', '100'),
     ('100', '104'),
     ('26', '100'),
     ('19', '100'),
     ('110', '108'),
     ('36', '102'),
     ('30', '104')]
所有项目的第二个值都是唯一的“100”、“102”、“104”和“108”

我想取'100','102','104','108'组的前3个值

输出应如下所示:

    [('10', '100'),
     ('11', '100'),
     ('22', '100'),
     ('18', '108'),
     ('25', '108'),
     ('105', '108'),
     ('12', '102'),
     ('20', '102'),
     ('36', '102'),
     ('15', '104'),
     ('24', '104'),
     ('14', '104')]

我不想将列表更改为dataframe并使dataframe正常工作。

您可以使用
itertools.groupby

from itertools import groupby
new_d = [(a, list(b)) for a, b in groupby(sorted(d, key=lambda x:int(x[-1])), key=lambda x:int(x[-1]))]
result = [b for _, c in new_d for b in c[:3]]
输出:

[('10', '100'), 
('11', '100'), 
('22', '100'), 
('12', '102'), 
('20', '102'), 
('36', '102'), 
('15', '104'), 
('24', '104'), 
('14', '104'), 
('18', '108'), 
('25', '108'), 
('105', '108')]

您可以迭代元组列表,并使用字典跟踪元组中的第二个元素出现了多少次。然后,如果第二个值出现的次数少于
3次,只需向结果列表中添加一个元组:

d = {}
n = 3
out = []
for i,j in l:
    if d.setdefault(j,0) < n:
        d[j]+= 1
        out.append((i,j))

另一种方法是在字典中保留键出现的简单计数,然后对其进行排序,例如(假设输入数据如问题中所示):


有关更多详细信息,请参阅和。

您在尝试此操作时忘记了包含您编写的代码。到目前为止,您在解决家庭作业方面做了哪些尝试?不要忘记您可以投票并接受答案,请参阅。谢谢
print(out)

[('10', '100'),
 ('11', '100'),
 ('18', '108'),
 ('22', '100'),
 ('12', '102'),
 ('15', '104'),
 ('25', '108'),
 ('20', '102'),
 ('24', '104'),
 ('105', '108'),
 ('14', '104'),
 ('36', '102')]
from collections import defaultdict
from itertools import count
from operator import itemgetter

counts = defaultdict(lambda: count(0))
result = [(value, key) for value, key in data if next(counts[key]) < 3]

print(sorted(result, key=itemgetter(1)))
[('10', '100'), ('11', '100'), ('22', '100'), ('12', '102'), ('20', '102'), ('36', '102'), ('15', '104'), ('24', '104'), ('14', '104'), ('18', '108'), ('25', '108'), ('105', '108')]