get method
James Mills
prologic at shortcircuit.net.au
Mon Dec 29 20:35:44 EST 2008
More information about the Python-list mailing list
Mon Dec 29 20:35:44 EST 2008
- Previous message (by thread): get method
- Next message (by thread): get method
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Tue, Dec 30, 2008 at 11:32 AM, James Mills <prologic at shortcircuit.net.au> wrote: > Ross, the others have informed you that you are not > actually incrementing the count. I'll assume you've > fixed your function now :) ... I want to show you a far > simpler way to do this which takes advantage of > Python's list comprehensions and mappings (which are > really what dictionaries are): > >>>> s = "James Mills and Danielle Van Sprang" >>>> dict([(k, len([x for x in s if x == k])) for k in s]) > {'a': 5, ' ': 5, 'e': 3, 'd': 1, 'g': 1, 'i': 2, 'M': 1, 'J': 1, 'm': > 1, 'l': 4, 'n': 4, 'p': 1, 's': 2, 'r': 1, 'V': 1, 'S': 1, 'D': 1} >>>> > > Let us know when you get to the "List Comprehension" > section - They are very powerful - As as Generators > and Generator Expressions. > > Have fun learning Python, Also, here's a nice function: >>> def histogram(s): ... d = dict([(k, len([x for x in s if x == k])) for k in s]) ... for k, v in d.iteritems(): ... print "%s: %s" % (k, "*" * v) ... >>> histogram("Hello World!") !: * : * e: * d: * H: * l: *** o: ** r: * W: * cheers James
- Previous message (by thread): get method
- Next message (by thread): get method
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list