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


Groups > comp.lang.python > #10343

RE: Selecting unique values

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 <fred.sells@adventistcare.org>
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 <j0lvt6$elc$1@dough.gmane.org>
X-MS-Has-Attach
X-MS-TNEF-Correlator
Thread-Topic Selecting unique values
Thread-Index AcxLc3gqDhMlx7MCQna3Hrs6LAQa/wAR2Vng
References <CABK368hMfEwm63OTjaHMy0DU7QCHt0=cwUPc42NL1aXL1GEUUg@mail.gmail.com> <j0lvt6$elc$1@dough.gmane.org>
From "Sells, Fred" <fred.sells@adventistcare.org>
To <python-list@python.org>
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.1503.1311701969.1164.python-list@python.org> (permalink)
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

Show key headers only | View raw


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=adventistcare.org@python.org
[mailto:python-list-bounces+frsells=adventistcare.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.
> 
> 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 = 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=None):
...     seen = set()
...     for item in items:
...             if key is None:
...                     keyval = item
...             else:
...                     keyval = key(item)
...             if keyval not in seen:
...                     seen.add(keyval)
...                     yield item
...

Unique latitudes:

>>> sys.stdout.writelines(uniquify(open("species.txt"), key=lambda s: 
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=lambda s: 
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):          
...     return StringIO("""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                 
... """)                                                  
...                                                       


-- 
http://mail.python.org/mailman/listinfo/python-list

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


Thread

RE: Selecting unique values "Sells, Fred" <fred.sells@adventistcare.org> - 2011-07-26 13:39 -0400

csiph-web