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


Groups > comp.lang.python > #11920

Re: Compare tuples of different lenght

Date 2011-08-21 01:03 +1000
From John O'Hagan <research@johnohagan.com>
Subject Re: Compare tuples of different lenght
References <c5deea4a-7e0a-491f-8425-ab1c2acf6c60@t9g2000vbs.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.266.1313852603.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, 20 Aug 2011 01:25:18 -0700 (PDT)
Jurgens de Bruin <debruinjj@gmail.com> 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.
[...]

This should work:

def long_match(tuples):
    sorted_tuples = sorted(tuples, key=len)
    for n, t in enumerate(sorted_tuples):
        for s in sorted_tuples[n + 1:]:
            if len(s) > len(t) and any(i in s for i in t):    
                tuples.remove(t)
                break
    return tuples 
 

Regards,

John

Back to comp.lang.python | Previous | NextPrevious 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