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: 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 To: "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: In-Reply-To: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 > For example, I was writing a program to detect whether two strings are > anagrams of each other. I had to write it like this: >=20 > def isAnagram(w1, w2): > w2=3Dlist(w2) > for c in w1: > if c not in w2: > return False > else: > w2.remove(c) > return True >=20 > But if there was a data structure in python which supported duplicate > elements(lets call it dset), then I could just write: >=20 > def inAnagram(w1,w2): > return dset(w1)=3D=3Ddset(w2) >=20 > Example of some dset methods: > {1,2,3,3} intersection {4,1,2,3,3,3} =3D=3D {1,2,3,3} > {1,2,3,3} union {4,1,2,3,3,3} =3D=3D {1,2,3,3,3,4} > {4,1,2,3,3,3} difference {1,2,3,3} =3D=3D {4,3} >=20 > 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) =3D=3D sorted(w2) To do the set-like operations, I guess that unless there's some itertools w= itchcraft available, you'd have to make your own dset type that inherits fr= om list. Then you can define your own intersection/union etc. methods. Cheers, Drea