Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If the returned order does not matter, you can try using <code>groupby</code> from <code>itertools</code> to group the items by their first element (after sorting by the first element), and then pulling out the maximum value with the <code>max</code> function (also, it should be noted that this returns a new list as opposed to modifying in place):</p> <pre><code>In [1]: from itertools import groupby In [2]: l = [[['a'],[24],214,1] ,[['b'],[24],312,1] ,[['a'],[24],3124,1] , [['c'],[24],34,1]] In [3]: result = [] In [4]: for k, g in groupby(sorted(l, key=lambda x: x[0]), key=lambda x: x[0]): ...: result.append(max(g, key=lambda m: m[2])) ...: ...: In [5]: result Out[5]: [[['a'], [24], 3124, 1], [['b'], [24], 312, 1], [['c'], [24], 34, 1]] </code></pre> <p>Extending this a bit, if you want to maintain original order, you can modify <code>l</code> by including only those items the are in <code>results</code>, which will maintain order:</p> <pre><code>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]] </code></pre> <p>And to combine that into a true abomination of a one-liner, you could (but probably shouldn't :) ) do this:</p> <pre><code>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]] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload