我想根据元组的第二个元素在元组列表中找到具有前5个最大值的所有元组项目。 例如,我有一个元组列表
x1 = [(a, 5), (b, 5), (c, 4), (d, 3), (e, 8), (f, 9), (g, 2), (h, 1)]
我想获得以下列表:
x2 = [(a, 5), (b, 5), (c, 4), (d, 3), (e, 8), (f, 9)]
由于第二个元素的前5个唯一值分别为9、8、5、4、3,并且a,b都具有值5,因此它们都应包含在列表中。
关于如何实现这一点的任何想法? 谢谢!
答案 0 :(得分:2)
找到排名前5的第二个元素:
i = set(list({x[1] for x in x1})[-5:])
过滤列表:
x2 = list(filter(lambda x: x[1] in i, x1))
甚至更好:
ss = {x[1] for x in x1}
if len(ss) > 5:
i = list(ss)[-5]
x2 = list(filter(lambda x: x[1] >= i, x1))
else:
x2 = x1
输出:
[('a', 5), ('b', 5), ('c', 4), ('d', 3), ('e', 8), ('f', 9)]
答案 1 :(得分:1)
x1 = [('f', 9), ('e', 8), ('a', 5), ('b', 5), ('c', 4), ('d', 3), ('g', 2), ('h', 1)]
x1.sort(key=lambda x: x[1], reverse=True)
max5set = set()
i = 0
for _, num in x1:
max5set.add(num)
i += 1
if (len(max5set) == 6):
break
print(x1[:i-1])
输出:
[('f', 9), ('e', 8), ('a', 5), ('b', 5), ('c', 4), ('d', 3)]
如果要按字母顺序获取该元组列表,请执行
print(sorted(x1[:i-1], key=lambda x: x[0]))
输出将为
[('a', 5), ('b', 5), ('c', 4), ('d', 3), ('e', 8), ('f', 9)]
答案 2 :(得分:0)
使用sorted
和itertools.groupby
:
import itertools
func = lambda x:x[1]
res = []
n_max = 5
group_by = itertools.groupby(sorted(x1, key=func, reverse=True), key=func)
for _ in range(n_max):
res.extend(list(next(group_by)[1]))
输出:
[('f', 9), ('e', 8), ('a', 5), ('b', 5), ('c', 4), ('d', 3)]
如果要对最终输出进行排序,请再次使用sorted
:
sorted(res, key=lambda x:x[0])
输出:
[('a', 5), ('b', 5), ('c', 4), ('d', 3), ('e', 8), ('f', 9)]
答案 3 :(得分:0)
使用numpy:
def my_fun(x1, k):
import numpy as np
x2 = np.asarray(x1) # Convert to numpy array
val = np.unique(np.sort(x2[:,1]))[-k:] # Sort index 1 & find top 'k' unique values
idx = np.isin(x2[:,1], val) # Indices of rows to retain
x2 = x2[idx].tolist()
x2 = list(map(tuple, x2)) # Convert back to list of tuples
return x2
>>> x1 = [('a', 5), ('b', 5), ('c', 4), ('d', 3), ('e', 8), ('f', 9), ('g', 2), ('h', 1)]
>>> my_fun(x1, 5)
[('a', '5'), ('b', '5'), ('c', '4'), ('d', '3'), ('e', '8'), ('f', '9')]
>>> my_fun(x1, 3)
[('a', '5'), ('b', '5'), ('e', '8'), ('f', '9')]