Filtering a Python list to uniques

Furkan Kuru furkankuru at gmail.com
Thu Mar 27 22:37:49 CET 2008


lstOne = [ 'A', 'B', 'C', 'C' ]
dct = {}
for item in lstOne:
    if dct.has_key(item):
        dct[item] += 1
    else: dct[item] = 1

On Thu, Mar 27, 2008 at 10:51 PM, Kelly Greer <kellygreer1 at yahoo.com> wrote:

> I'm in Python 2.4.4
>
> And it works for me in the Python Shell like you just
> did. Let me try another thing or two. I can't
> reproduce the unhashable error now. :(
>
> Can you help me with one more thing?
> If I have
> lstOne = [ 'A', 'B', 'C', 'C' ]
>
> # what is the easiest way to get
> # back a dictionary like this:
> dct = { 'A': 1, 'B': 1, 'C': 2 }
> # in other words counting the times a letter appears
> in the list.
>
> Thanks,
> Kelly
>
>
> --- Furkan Kuru <furkankuru at gmail.com> wrote:
>
> > set(lstone)
> > works fine in python 2.5.1
> >
> > Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08)
> > [MSC v.1310 32 bit (Intel)]
> > on
> > win32
> > Type "help", "copyright", "credits" or "license" for
> > more information.
> > >>> lstone = [ 1, 2, 3, 3, 4, 5, 5, 6 ]
> > >>> set(lstone)
> > set([1, 2, 3, 4, 5, 6])
> >
> >
> >
> > On 3/26/08, kellygreer1 <kellygreer1 at yahoo.com>
> > wrote:
> > >
> > > What is the best way to filter a Python list to
> > its unique members?
> > > I tried some method using Set but got some
> > "unhashable" error.
> > >
> > > lstone = [ 1, 2, 3, 3, 4, 5, 5, 6 ]
> > > # how do i reduce this to
> > > lsttwo = [ 1, 2, 3, 4, 5, 6 ]
> > >
> > > Is there a page on this in the Python in a
> > Nutshell or the Python
> > > Cookbook?
> > > Did I miss something?
> > >
> > > Kelly Greer
> > > kellygreer1 at nospam.com
> > > change nospam to yahoo
> > > --
> > >
> > http://mail.python.org/mailman/listinfo/python-list
> > >
> >
> >
> >
> > --
> > Furkan Kuru
> >
>
>
>
>
>  ____________________________________________________________________________________
> Be a better friend, newshound, and
> know-it-all with Yahoo! Mobile.  Try it now.
> http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
>



-- 
Furkan Kuru
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080327/5e975045/attachment.html>


More information about the Python-list mailing list