Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'else:': 0.03; 'sys': 0.05; 'python': 0.08; 'advance.': 0.09; '""")': 0.09; 'none:': 0.09; 'tuple.': 0.09; 'message-----': 0.12; 'wrote:': 0.15; 'bonus:': 0.16; 'open()': 0.16; 'stringio': 0.16; 'version)': 0.16; '>>>': 0.16; 'def': 0.16; 'header:In-Reply-To:1': 0.22; 'function': 0.26; 'url:mailman': 0.27; 'import': 0.29; 'rid': 0.29; 'yield': 0.29; 'subject:': 0.30; 'module': 0.30; 'thanks': 0.31; 'values': 0.31; 'lines': 0.31; 'url:listinfo': 0.32; 'to:addr:python-list': 0.34; 'record': 0.34; 'sent:': 0.34; '(as': 0.34; 'from:': 0.36; 'charset:us-ascii': 0.36; 'url:python': 0.37; 'received:192': 0.38; 'received:org': 0.38; 'url:org': 0.38; 'subject:: ': 0.38; 'skip:s 20': 0.39; 'to:addr:python.org': 0.39; 'peter': 0.62; 'below': 0.62; 'july': 0.64; 'unique': 0.64; 'here:': 0.64; '26,': 0.67; 'listings': 0.68; 'records': 0.73; 'latitude': 0.84; 'longitude': 0.84; 'otten': 0.84; 'species': 0.84 X-MimeOLE: Produced By Microsoft Exchange V6.5 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: RE: Selecting unique values Date: Tue, 26 Jul 2011 13:39:25 -0400 In-Reply-To: X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Selecting unique values Thread-Index: AcxLc3gqDhMlx7MCQna3Hrs6LAQa/wAR2Vng References: From: "Sells, Fred" To: 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: 115 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1311701969 news.xs4all.nl 23943 [2001:888:2000:d::a6]:39706 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10343 The set module or function (depends on which python version) will do this if you make each record a tuple. -----Original Message----- From: python-list-bounces+frsells=3Dadventistcare.org@python.org [mailto:python-list-bounces+frsells=3Dadventistcare.org@python.org] On Behalf Of Peter Otten Sent: Tuesday, July 26, 2011 5:04 AM To: python-list@python.org Subject: Re: Selecting unique values Kumar Mainali wrote: > I have a dataset with occurrence records of multiple species. I need to > get rid of multiple listings of the same occurrence point for a species > (as you see below in red and blue typeface). How do I create a dataset > only with unique set of longitude and latitude for each species? Thanks in > advance. >=20 > Species_name Longitude Latitude > Abies concolor -106.601 35.868 > Abies concolor -106.493 35.9682 > Abies concolor -106.489 35.892 > Abies concolor -106.496 35.8542 > Accipiter cooperi -119.688 34.4339 > Accipiter cooperi -119.792 34.5069 > Accipiter cooperi -118.797 34.2581 > Accipiter cooperi -77.38333 39.68333 > Accipiter cooperi -77.38333 39.68333 > Accipiter cooperi -75.99153 40.633335 > Accipiter cooperi -75.99153 40.633335 >>> def uniquify(items): ... seen =3D set() ... for item in items: ... if item not in seen: ... seen.add(item) ... yield item ... >>> import sys >>> sys.stdout.writelines(uniquify(open("species.txt"))) Species_name Longitude Latitude Abies concolor -106.601 35.868 Abies concolor -106.493 35.9682 Abies concolor -106.489 35.892 Abies concolor -106.496 35.8542 Accipiter cooperi -119.688 34.4339 Accipiter cooperi -119.792 34.5069 Accipiter cooperi -118.797 34.2581 Accipiter cooperi -77.38333 39.68333 Accipiter cooperi -75.99153 40.633335 If you need to massage the lines a bit: >>> def uniquify(items, key=3DNone): ... seen =3D set() ... for item in items: ... if key is None: ... keyval =3D item ... else: ... keyval =3D key(item) ... if keyval not in seen: ... seen.add(keyval) ... yield item ... Unique latitudes: >>> sys.stdout.writelines(uniquify(open("species.txt"), key=3Dlambda s:=20 s.rsplit(None, 1)[-1])) Species_name Longitude Latitude Abies concolor -106.601 35.868 Abies concolor -106.493 35.9682 Abies concolor -106.489 35.892 Abies concolor -106.496 35.8542 Accipiter cooperi -119.688 34.4339 Accipiter cooperi -119.792 34.5069 Accipiter cooperi -118.797 34.2581 Accipiter cooperi -77.38333 39.68333 Accipiter cooperi -75.99153 40.633335 Unique species names: >>> sys.stdout.writelines(uniquify(open("species.txt"), key=3Dlambda s:=20 s.rsplit(None, 2)[0])) Species_name Longitude Latitude Abies concolor -106.601 35.868 Accipiter cooperi -119.688 34.4339 Bonus: open() is not the built-in here: >>> from StringIO import StringIO >>> def open(filename): =20 ... return StringIO("""Species_name Longitude Latitude ... Abies concolor -106.601 35.868 =20 ... Abies concolor -106.493 35.9682 =20 ... Abies concolor -106.489 35.892 =20 ... Abies concolor -106.496 35.8542 =20 ... Accipiter cooperi -119.688 34.4339 =20 ... Accipiter cooperi -119.792 34.5069 =20 ... Accipiter cooperi -118.797 34.2581 =20 ... Accipiter cooperi -77.38333 39.68333 =20 ... Accipiter cooperi -77.38333 39.68333 =20 ... Accipiter cooperi -75.99153 40.633335 =20 ... Accipiter cooperi -75.99153 40.633335 =20 ... """) =20 ... =20 --=20 http://mail.python.org/mailman/listinfo/python-list