Python 删除列表中共享类似功能的项目

Python 删除列表中共享类似功能的项目,python,list,Python,List,假设我有一个嵌套列表,如下所示: [[['a'],[24],214,1] ,[['b'],[24],312,1] ,[['a'],[24],3124,1] , [['c'],[24],34,1]] 假设我想从列表中删除所有项目,除了在项目[0]中共享同一字母的项目中项目[2]的最大值之外 例如,在前面的列表中,我在项[0]中有两个项共享同一个字母: [ ['a'],[24],214,1], [['a'],[24],3124,1] ] 我想删除前者,因为它的项[2]的值较低 然后,输出列表应为

假设我有一个嵌套列表,如下所示:

[[['a'],[24],214,1] ,[['b'],[24],312,1] ,[['a'],[24],3124,1] , [['c'],[24],34,1]]
假设我想从列表中删除所有项目,除了在
项目[0]
中共享同一字母的项目中
项目[2]
的最大值之外

例如,在前面的列表中,我在
项[0]
中有两个项共享同一个字母:

[ ['a'],[24],214,1], [['a'],[24],3124,1] ]
我想删除前者,因为它的
项[2]
的值较低

然后,输出列表应为:

[ [['b'],[24],312,1] ,[['a'],[24],3124,1] , [['c'],[24],34,1] ]

你能给我一个简洁的方法吗?

由于你的问题令人困惑,我已经给出了删除max和min元素的可能性

>>> def foo(some_list, fn = max):
    #Create a dictionary, default dict won;t help much as 
    #we have to refer back to the value for an existing key
    #The dictionary would have item[0] as key
    foo_dict = dict()
    #Iterate through the list
    for e in some_list:
            #Check if the key exist
        if e[0][0] in foo_dict:
                    #and if it does, find the max of the existing value and the 
                    #new element. The key here is the second item
            foo_dict[e[0][0]] = fn(foo_dict[e[0][0]], e, key = lambda e:e[2])
        else:
                    #else consider the new element as the current max
            foo_dict[e[0][0]] = e
    return foo_dict.values()

>>> foo(somelist)
[[['a'], [24], 3124, 1], [['c'], [24], 34, 1], [['b'], [24], 312, 1]]
>>> foo(somelist,min)
[[['a'], [24], 214, 1], [['c'], [24], 34, 1], [['b'], [24], 312, 1]]

如果返回的订单无关紧要,您可以尝试使用
itertools
中的
groupby
按项目的第一个元素对项目进行分组(按第一个元素排序后),然后使用
max
功能提取最大值(还应注意,这将返回一个新列表,而不是就地修改):

稍微扩展一下,如果您想保持原始顺序,可以修改
l
,只包括
结果中的项目,这将保持顺序:

In [6]: l = [i for i in l if i in result]

In [7]: l
Out[7]: [[['b'], [24], 312, 1], [['a'], [24], 3124, 1], [['c'], [24], 34, 1]]
要将这一点结合成真正令人厌恶的一行,你可以(但可能不应该:)这样做:

In [10]: l = [[['a'],[24],214,1] ,[['b'],[24],312,1] ,[['a'],[24],3124,1] , [['c'],[24],34,1]]

In [11]: [i for i in l if i in [max(g, key=lambda m: m[2]) for k, g in groupby(sorted(l, key=lambda x: x[0]), key=lambda x: x[0])]]
Out[11]: [[['b'], [24], 312, 1], [['a'], [24], 3124, 1], [['c'], [24], 34, 1]]

一些选项保留原始顺序,仅删除比较器值低于最大值的任何项目

def filter1(items):
    first = set(item[0][0] for item in items)
    compare = dict((f, max(item[2] for item in items if item[0][0] == f)) 
        for f in first)
    return  [item for item in items if item[2] >= compare[item[0][0]]]

def filter2(items):
    compare = {}
    for item in items:
        if ((item[0][0] in compare and item[2] > compare[item[0][0]])
            or (not item[0][0] in compare)):
            compare[item[0][0]] = item[2]
    return [item for item in items if item[2] >= compare[item[0][0]]]

def filter3(items):
    return [i for i in items if i[2] >= 
        max(j[2] for j in items if j[0][0]==i[0][0])]

如果你有一个大的列表,filter3是最短但最慢的。我想过滤器2是最快的。

对不起。我会马上去做的。很好地更正:)订单重要吗?高尔夫代码:
[max(v,key=lambda item:item[2]),对于groupby中的v(排序(l),lambda item:item[0])
@Boud哈哈,老实说,我正准备用免责声明键入这句话“为了让阅读你代码的人感到困惑,你可以试试……”。我将把荣耀留给你!
def filter1(items):
    first = set(item[0][0] for item in items)
    compare = dict((f, max(item[2] for item in items if item[0][0] == f)) 
        for f in first)
    return  [item for item in items if item[2] >= compare[item[0][0]]]

def filter2(items):
    compare = {}
    for item in items:
        if ((item[0][0] in compare and item[2] > compare[item[0][0]])
            or (not item[0][0] in compare)):
            compare[item[0][0]] = item[2]
    return [item for item in items if item[2] >= compare[item[0][0]]]

def filter3(items):
    return [i for i in items if i[2] >= 
        max(j[2] for j in items if j[0][0]==i[0][0])]