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


Groups > comp.lang.python > #5852

RE: Python sets which support multiple same elements

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <andreas.tawn@ubisoft.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'python?': 0.05; 'subject:Python': 0.06; 'python': 0.08; 'to:name:python- list@python.org': 0.09; 'this:': 0.10; 'def': 0.12; 'from:addr:andreas.tawn': 0.16; 'from:addr:ubisoft.com': 0.16; 'from:name:andreas tawn': 0.16; 'intersection': 0.16; 'message-id :@PDC-MAIL-CMS01.ubisoft.org': 0.16; 'received:195.22': 0.16; 'received:195.22.144': 0.16; 'received:195.22.144.43': 0.16; 'received:pdc-mail-out01.ubisoft.fr': 0.16; 'received:ubisoft.fr': 0.16; 'operations,': 0.16; 'cheers,': 0.19; 'guess': 0.19; 'header :In-Reply-To:1': 0.21; 'detect': 0.22; 'structure': 0.23; 'example': 0.27; 'problem': 0.28; 'subject:same': 0.30; 'subject:support': 0.30; 'define': 0.31; 'to:addr:python-list': 0.33; 'list.': 0.33; 'example,': 0.35; 'there': 0.35; 'duplicate': 0.35; 'skip:{ 10': 0.35; 'idea': 0.36; 'charset:us-ascii': 0.36; 'difference': 0.37; 'two': 0.37; 'think': 0.38; 'could': 0.38; 'but': 0.38; 'data': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'unless': 0.39; 'add': 0.39; 'to:addr:python.org': 0.39; 'kind': 0.60; 'your': 0.60; 'other.': 0.63; 'union': 0.71; 'overlook': 0.84; 'problems?': 0.84
From Andreas Tawn <andreas.tawn@ubisoft.com>
To "python-list@python.org" <python-list@python.org>
Date Fri, 20 May 2011 13:53:55 +0200
Subject RE: Python sets which support multiple same elements
Thread-Topic Python sets which support multiple same elements
Thread-Index AcwW4rDQyF6Em4SxQ4u9MldZUarKpgAAOxHQ
References <f7760e6f-b176-4c13-8285-08d7689ece02@f31g2000pri.googlegroups.com>
In-Reply-To <f7760e6f-b176-4c13-8285-08d7689ece02@f31g2000pri.googlegroups.com>
Accept-Language en-US
Content-Language en-US
X-MS-Has-Attach
X-MS-TNEF-Correlator
acceptlanguage en-US
Content-Type text/plain; charset="us-ascii"
Content-Transfer-Encoding quoted-printable
MIME-Version 1.0
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.1838.1305892439.9059.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 82.94.164.166
X-Trace 1305892439 news.xs4all.nl 49175 [::ffff:82.94.164.166]:60530
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5852

Show key headers only | View raw


> For example, I was writing a program to detect whether two strings are
> anagrams of each other. I had to write it like this:
> 
> def isAnagram(w1, w2):
>   w2=list(w2)
>   for c in w1:
>     if c not in w2:
>       return False
>     else:
>       w2.remove(c)
>   return True
> 
> But if there was a data structure in python which supported duplicate
> elements(lets call it dset), then I could just write:
> 
> def inAnagram(w1,w2):
>   return dset(w1)==dset(w2)
> 
> Example of some dset methods:
> {1,2,3,3} intersection {4,1,2,3,3,3}  == {1,2,3,3}
> {1,2,3,3} union {4,1,2,3,3,3} == {1,2,3,3,3,4}
> {4,1,2,3,3,3} difference {1,2,3,3} == {4,3}
> 
> Do you think that it would be a good idea to add this kind of data
> structure to python? Or did I overlook some other easy way to solve
> this kind of problems?

Just to do the anagram problem you could do...

def isAnagram(w1, w2):
    return sorted(w1) == sorted(w2)

To do the set-like operations, I guess that unless there's some itertools witchcraft available, you'd have to make your own dset type that inherits from list. Then you can define your own intersection/union etc. methods.

Cheers,

Drea

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


Thread

Python sets which support multiple same elements ErichCart ErichCart <erichcart@gmail.com> - 2011-05-20 04:37 -0700
  RE: Python sets which support multiple same elements Andreas Tawn <andreas.tawn@ubisoft.com> - 2011-05-20 13:53 +0200
  Re: Python sets which support multiple same elements Chris Angelico <rosuav@gmail.com> - 2011-05-20 21:57 +1000

csiph-web