Path: csiph.com!usenet.pasdenom.info!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'transform': 0.07; '[0]': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'enough.': 0.16; 'itertools': 0.16; 'once.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sorting': 0.16; 'wrote:': 0.18; 'import': 0.22; 'header:User-Agent:1': 0.23; 'exists': 0.24; 'this:': 0.26; 'pass': 0.26; 'least': 0.26; 'header:X-Complaints- To:1': 0.27; '[1]': 0.29; 'mix': 0.30; 'robert': 0.30; 'subject:list': 0.30; 'group:': 0.31; 'tuples': 0.31; 'quite': 0.32; 'subject:from': 0.34; 'there': 0.35; 'next': 0.36; 'hi,': 0.36; 'should': 0.36; 'list': 0.37; 'performance': 0.37; 'to:addr :python-list': 0.38; 'explain': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'hope': 0.61; 'first': 0.61; 'mean.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: min max from tuples in list Date: Thu, 12 Dec 2013 09:35:57 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p50849981.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386837348 news.xs4all.nl 2931 [2001:888:2000:d::a6]:51494 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61683 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.