[portland] Still Having List/Tuple Problems

kirby urner kirby.urner at gmail.com
Wed Apr 16 21:28:12 CEST 2008


On Wed, Apr 16, 2008 at 12:21 PM, Rich Shepard <rshepard at appl-ecosys.com> wrote:
> On Wed, 16 Apr 2008, kirby urner wrote:

<< SNIP >>

>  >>>> thekeys = set([i for (i,j) in mydata])
>  >
>  >>>> thekeys
>  > set(['A', 'C', 'B'])
>  >

>  >>>> for thekey in thekeys:
>  >       print thekey
>  >       print [j for (i,j) in mydata if i == thekey]
>
>    I need to read and learn about sets in python.
>

I forget in which version you don't need to import,
but by 2.5 they're built-in native.   Because sets
don't allow duplicates, they're a quick way to scan
through a list while purging duplicates e.g.

>>> set(['A','A','B','B'])
set(['A', 'B'])

>    Now I have two approaches as solutions when I again run into the same
>  need.
>

The list comprehensions approach is based on
filtration i.e. no pre-sorting of your data to ensure
clumping on the key.  Not super efficient, but
maybe OK for smallish data sets (10K elements
or whatever).

Remember, your time is more valuable than your
machine's and sometimes slower code is better if
it's more readable / maintainable by you and your
peers.

Kirby

>  Thank you very much,
>
>
>
>  Rich
>
>  --
>  Richard B. Shepard, Ph.D.               |  Integrity            Credibility
>  Applied Ecosystem Services, Inc.        |            Innovation
>  <http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863
>  _______________________________________________
>  Portland mailing list
>  Portland at python.org
>  http://mail.python.org/mailman/listinfo/portland
>


More information about the Portland mailing list