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


Groups > comp.lang.python > #11913

Re: Compare tuples of different lenght

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'skip:[ 40': 0.07; '__name__': 0.09; 'assert': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'skip:[ 30': 0.09; 'tuple': 0.09; 'def': 0.15; '"__main__":': 0.16; '(1,': 0.16; '(2,': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.16; 'smallest': 0.23; 'skip:[ 10': 0.27; 'compare': 0.28; 'import': 0.28; 'print': 0.29; 'example': 0.30; 'collections': 0.30; 'from:addr:web.de': 0.30; 'tuples': 0.30; 'thanks': 0.30; 'list': 0.32; 'hi,': 0.32; 'to:addr:python-list': 0.33; 'header:X-Complaints-To:1': 0.35; 'list.': 0.35; 'element': 0.37; 'two': 0.37; 'received:org': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'skip:d 20': 0.39; 'chain': 0.66; 'subject:different': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Compare tuples of different lenght
Date Sat, 20 Aug 2011 13:56:18 +0200
Organization None
References <c5deea4a-7e0a-491f-8425-ab1c2acf6c60@t9g2000vbs.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084a8dc.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.259.1313841381.27778.python-list@python.org> (permalink)
Lines 68
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1313841381 news.xs4all.nl 23966 [2001:888:2000:d::a6]:52886
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:11913

Show key headers only | View raw


Jurgens de Bruin wrote:

> Hi,
> 
> I have a list of tuples:
> 
> [(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),]
> 
> I would like to compare all the tuples to each other and if one
> element if found two tuples the smallest tuples is removed from the
> list.
> 
> example if tuple 1 and tuple 3 are compare it should find that a
> single element in each are the same and tuple 1 should be removed
> resulting in
> 
> [(12,13),(2,3,4),(8,),(5,6),(7,8,9),]
> 
> the same for tuple 4 and 6 resulting in
> 
> [(12,13),(2,3,4),(5,6),(7,8,9),]
> 
> is this possible as I am having no success.
> 
> Thanks

from collections import Counter, defaultdict
from itertools import chain

def process_counter(sample):
    c = Counter()
    d = defaultdict(list)
    for items in sample:
        c.update(items)
        d[len(items)].append(items)

    result = []
    for cluster in sorted(d.values(), key=len):
        c.subtract(chain.from_iterable(cluster))
        for items in cluster:
            if not any(c[item] for item in items):
                result.append(items)

    result.sort(key=sample.index)
    return result

if __name__ == "__main__":
    for process in [process_counter]:
        print process.__name__

        sample = [(2,),(12,13),(2,3,4),(8,),(5,6),(7,8,9),]
        wanted = [(12,13),(2,3,4),(5,6),(7,8,9),]
        assert process(sample) == wanted


        sample = [(5,6), (6,7,8)]
        wanted = [(6,7,8)]
        got = process(sample)
        assert got == wanted

        sample = wanted = [(5, 6), (6, 7)]
        assert process(sample) == wanted

        sample = [(1,), (1, 2), (2, 3, 4)]
        wanted = [(2, 3, 4)]
        assert process(sample) == wanted
 

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


Thread

Compare tuples of different lenght Jurgens de Bruin <debruinjj@gmail.com> - 2011-08-20 01:25 -0700
  Re: Compare tuples of different lenght Chris Rebert <clp2@rebertia.com> - 2011-08-20 01:45 -0700
    Re: Compare tuples of different lenght Jurgens de Bruin <debruinjj@gmail.com> - 2011-08-20 01:54 -0700
    Re: Compare tuples of different lenght Jurgens de Bruin <debruinjj@gmail.com> - 2011-08-20 02:00 -0700
  Re: Compare tuples of different lenght Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-20 20:17 +1000
    Re: Compare tuples of different lenght Jurgens de Bruin <debruinjj@gmail.com> - 2011-08-20 03:47 -0700
  Re: Compare tuples of different lenght Peter Otten <__peter__@web.de> - 2011-08-20 13:56 +0200
  Re: Compare tuples of different lenght John O'Hagan <research@johnohagan.com> - 2011-08-21 01:03 +1000

csiph-web