#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Letter count dictionary?


    I have to write a program that takes the standard input lines and gives a letter count, ignoring case distinction ('a' counts the same as 'A').

    here's what I have so far:

    Code:
    import sys
    x = sys.stdin.readlines()
    d = {}
    char_set = set(x)
    for char in char_set:
            if not char in d: d[char] = 1
            else: d[char] = d[char] + 1
    print (d)
    but I need it to display like:
    a 453679
    b 85266
    c 188794

    I'm pretty new to programming, where would I put the strip and split commands? And how do I make it print each letter count on a separate line?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    492
    Rep Power
    33
    First you have to convert to all upper or all lower case so that "a" and "A" are counted the same. Next, test that the letter is between "a" and "z" to eliminate punctuation. Then sort the dictioary's keys to print in order. Also you do not have to convert the string to a set; you can just use
    for char in x
    as a set can not have duplicate keys, so will eliminate any letters that occur more than once.
    Code:
    s= "aabbcdee"
    print s
    print set(s)
    Accessing a dictionary by keys.
    Getting input using raw_input (or input for Python 3.X).
    Last edited by dwblas; November 18th, 2012 at 08:36 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    so what would the code look like? do I have to use append?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    Code:
    import sys
    import string
    
    Xx = sys.stdin.read()    # read entire file, don't split it into lines.
    x = Xx.lower()           # convert to lower case
    
    d = {}                   # make a dictionary of counts.
    
    # You used sets, I assume you understand set theory
    # to the extent of applying Venn diagrams.
    char_set = set(x).intersection(set(string.ascii_lowercase))
    
    for char in char_set:
        d[char] = x.count(char)               # >>> help
    
    for key in sorted(d):
        print(key,d[key])
    
    
    
    
    # How did I know about count method of list?
    # I read the documents docs.python.org , and the tutorial.
    # Also the information is available from the interpreter.
    '''
    >>> import pprint
    >>> pprint.pprint(dir(''))
    ['__add__',
     '__class__',
     '__contains__',
     '__delattr__',
     ...
     'center',
     'count',
     'encode',
     'endswith',
     ...
     'zfill']
    >>>
    >>>
    >>>
    >>>
    >>> help(''.count)
    Help on built-in function count:
    
    count(...)
        S.count(sub[, start[, end]]) -> int
    
        Return the number of non-overlapping occurrences of substring sub in
        string S[start:end].  Optional arguments start and end are
        interpreted as in slice notation.
    '''
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    is there a way to do it without sets? just 'for char in x:' ?
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    You don't have to use sets directly. I don't think you can avoid sets entirely because
    Originally Posted by http://www.mathworld.com
    A set is a finite or infinite collection of objects in which order has no significance, and multiplicity is generally also ignored
    (and so the keys of a dictionary are a set)
    Last edited by b49P23TIvg; November 19th, 2012 at 01:34 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    Python makes this simple with a default dictionary:
    Code:
    import collections
    
    text = "Do not run in front of darn car"
    text = text.lower()  # optional
    
    # values are integers
    ddict = collections.defaultdict(int)
    for c in text:
        ddict[c] += 1
    
    print("Sorted alphabetically:")  
    for letter, freq in sorted(ddict.items()):
        print("%r  %d" % (letter, freq))
    
    print('-'*15)
    
    print("Sorted by frequency:")
    for letter, freq in sorted(ddict.items(), key=lambda x: x[1]):
        print("%r  %d" % (letter, freq))
    
    '''result ...
    Sorted alphabetically:
    ' '  7
    'a'  2
    'c'  1
    'd'  2
    'f'  2
    'i'  1
    'n'  5
    'o'  4
    'r'  4
    't'  2
    'u'  1
    ---------------
    Sorted by frequency:
    'c'  1
    'i'  1
    'u'  1
    'a'  2
    'd'  2
    'f'  2
    't'  2
    'o'  4
    'r'  4
    'n'  5
    ' '  7
    Last edited by Dietrich; November 19th, 2012 at 02:16 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo