Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61688

Re: min max from tuples in list

From Peter Otten <__peter__@web.de>
Subject Re: min max from tuples in list
Date 2013-12-12 10:03 +0100
Organization None
References <f78a11dc-efdd-4108-8d1f-59386f020fd0@googlegroups.com> <l8bsgb$jln$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.3977.1386839016.18130.python-list@python.org> (permalink)

Show all headers | View raw


Peter Otten wrote:

> Robert Voigtländer wrote:
> 
>> Hi,
>> 
>> I have a list like this:
>> 
>> a = [(52, 193), (52, 193), (52, 192), (51, 193), (51, 191), (51, 190),
>> (51, 189), (51, 188), (50, 194), (50, 187), (50, 186), (50, 185), (50,
>> 184), (49, 194), (49, 183), (49, 182), (49, 181), (48, 194), (48, 180),
>> (48, 179), (48, 178), (48, 177), (47, 194), (47, 176), (47, 175), (47,
>> 174), (47, 173), (46, 195), (46, 172), (46, 171), (46, 170), (46, 169),
>> (45, 195), (45, 168), (45, 167), (45, 166), (44, 195), (44, 165), (44,
>> 164), (44, 163), (44, 162), (43, 195), (43, 161), (43, 160), (43, 159),
>> (43, 158), (42, 196), (42, 157), (42, 156), (42, 155), (41, 196), (41,
>> 154), (41, 153), (41, 152), (41, 151), (40, 196), (40, 150), (40, 149),
>> (40, 148), (40, 147), (39, 196), (39, 146), (39, 145), (39, 144), (39,
>> 143), (38, 196), (38, 142), (38, 141), (38, 140), (37, 197), (37, 139),
>> (37, 138), (37
>>  , 137), (37, 136), (36, 197), (36, 135), (36, 134), (36, 133)]
>> 
>> 
>> I need to find a -performant- way to transform this into a list with
>> tuples (a[0],[a[0][1]min],[a[0][1]max]).
>> 
>> Hard to explaint what I mean .. [0] of the first three tuples is 52. [1]
>> is 193,193 and 192. What I need as result for these three tuples is:
>> (52,192,193).
>> 
>> For the next five tuples it is (51,188,193).
>> 
>> 
>> Extra challenges:
>> - This list is sorted. For performance reasons I would like to keep it
>> unsorted. - There may be tuples where min=max.
>> - There my be tupples where [0] only exists once. So mix is automatically
>> max
>> 
>> 
>> I hope I was able to explain what I mean.
> 
> I have a hunch that sorting the list might be quite efficient. You should
> at least try
> 
> import operator
> import itertools
> 
> a = ...
> a.sort()
> result = []
> for key, group in itertools.groupby(a, key=operator.itemgetter(0)):
>     minpair = maxpair = next(group)
>     for maxpair in group:
>         pass
>     result.append((key, minpair[1], maxpair[1]))
> 
> for item in result:
>     print(item)
> 
> to see whether it is good enough.

On second thought -- Chris Angelico may be right ;)
So here's my dict-based variant: 

def keyminmax(items):
    d = collections.defaultdict(list)
    for key, value in items:
        d[key].append(value)
    for key, values in d.items(): # d.iteritems() in python 2
        yield key, min(values), max(values)

for item in keyminmax(a):
    print(item)

It uses a bit more memory than Chris' code, but has fewer min()/max() calls.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

min max from tuples in list Robert Voigtländer <r.voigtlaender@gmail.com> - 2013-12-11 23:25 -0800
  Re: min max from tuples in list Chris Angelico <rosuav@gmail.com> - 2013-12-12 19:18 +1100
  Re: min max from tuples in list Robert Voigtländer <r.voigtlaender@gmail.com> - 2013-12-12 00:34 -0800
    Re: min max from tuples in list Chris Angelico <rosuav@gmail.com> - 2013-12-12 19:43 +1100
  Re: min max from tuples in list Peter Otten <__peter__@web.de> - 2013-12-12 09:35 +0100
  Re: min max from tuples in list Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-12-12 10:52 +0200
  Re: min max from tuples in list Peter Otten <__peter__@web.de> - 2013-12-12 10:03 +0100
  Re: min max from tuples in list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-12 11:44 +0000
    Re: min max from tuples in list Tim Chase <python.list@tim.thechases.com> - 2013-12-12 06:04 -0600
    Re: min max from tuples in list MRAB <python@mrabarnett.plus.com> - 2013-12-12 12:36 +0000
      Re: min max from tuples in list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-12 23:25 +0000
    Re: min max from tuples in list Peter Otten <__peter__@web.de> - 2013-12-12 13:54 +0100
      Re: min max from tuples in list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-13 02:36 +0000
    Re: min max from tuples in list Roy Smith <roy@panix.com> - 2013-12-12 10:02 -0500
      Re: min max from tuples in list Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-12 15:13 +0000
        Re: min max from tuples in list Robert Voigtländer <r.voigtlaender@gmail.com> - 2013-12-12 22:28 -0800
          Re: min max from tuples in list rusi <rustompmody@gmail.com> - 2013-12-13 10:06 -0800
            Re: min max from tuples in list Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-13 19:30 -0500
              Re: min max from tuples in list Tim Roberts <timr@probo.com> - 2013-12-14 19:41 -0800
                Re: min max from tuples in list Chris Angelico <rosuav@gmail.com> - 2013-12-16 23:08 +1100
                Re: min max from tuples in list rusi <rustompmody@gmail.com> - 2013-12-16 07:49 -0800
                Re: min max from tuples in list Ned Batchelder <ned@nedbatchelder.com> - 2013-12-16 10:59 -0500
                Re: min max from tuples in list Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-12-17 11:49 +1300
  Re: min max from tuples in list Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-13 01:33 +0000

csiph-web