Selecting unique values

sparky gmail sparkytwobillion at gmail.com
Tue Jul 26 10:58:29 CEST 2011


On 7/25/2011 3:03 PM, Kumar Mainali wrote:
> Greetings
>
> 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_nameLongitudeLatitude
> Abies concolor-106.60135.868
> Abies concolor-106.49335.9682
> Abies concolor-106.48935.892
> Abies concolor-106.49635.8542
> Accipiter cooperi-119.68834.4339
> Accipiter cooperi-119.79234.5069
> Accipiter cooperi-118.79734.2581
> Accipiter cooperi-77.3833339.68333
> Accipiter cooperi-77.3833339.68333
> Accipiter cooperi-75.9915340.633335
> Accipiter cooperi-75.9915340.633335
>
> - Kumar
It would seem to me that what you are wanting is a dictionary where the 
species name is the key and the value is a list of Long/Lat

So assuming your data is in a list like so (such as you might get from 
reading lines from a file) ['Abies concolor-106.60135.868', 'Abies 
concolor-106.49335.9682']

You could say

output = dict()
for record in my_long_list:
     values = record.rsplit(' ', 2)
     key = values[:1]
     value = ' '.join(values[1:])
     try:
          output[key].append(value)
      except KeyError:
          output[key] = [value]


Something like that should do.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110726/15f860cf/attachment.html>


More information about the Python-list mailing list