-1
def wordCount(inPath):
    inFile = open(inPath, 'r')
    lineList = inFile.readlines()
    counter = {}
    for line in range(len(lineList)):
        currentLine = lineList[line].rstrip("\n")
        for letter in range(len(currentLine)):
            if currentLine[letter] in counter:
                counter[currentLine[letter]] += 1
            else:
                counter[currentLine[letter]] = 1

    sorted(counter.keys(), key=lambda counter: counter[0])
    for letter in counter:
        print('{:3}{}'.format(letter, counter[letter]))

inPath = "file.txt"
wordCount(inPath)

This is the output:

a  1
k  1
u  1
l  2
   12
h  5
T  1
r  4
c  2
d  1
s  5
i  6
o  3
f  2
H  1
A  1
e  10
n  5
x  1
t  5

This is the output I want:

  12
A 1
H 1
T 1
a 1
c 2
d 1
e 10
f 2
h 5
i 6
k 1
l 2
n 5
o 3
r 4
s 5
t 5
u 1
x 1

How do I sort the "counter" alphabetically? I've tried simply sorting by keys and values but it doesn't return it alphabetically starting with capitals first Thank you for your help!

  • 1
    Possible duplicate of [How can I sort a dictionary by key?](http://stackoverflow.com/questions/9001509/how-can-i-sort-a-dictionary-by-key) – pvg Mar 21 '17 at 21:09

1 Answers1

2
sorted(counter.keys(), key=lambda counter: counter[0])

alone does nothing: it returns a result which isn't used at all (unless you recall it using _ but that's rather a command-line practice)

As opposed to what you can do with a list with .sort() method, you cannot sort dictionary keys "in-place". But what you can do is iterating on the sorted version of the keys:

for letter in sorted(counter.keys()):

, key=lambda counter: counter[0] is useless here: you only have letters in your keys.

Aside: your whole code could be simplified a great deal using collections.Counter to count the letters.

import collections

c = collections.Counter("This is a Sentence")
for k,v in sorted(c.items()):
    print("{} {}".format(k,v))

result (including space char):

  3
S 1
T 1
a 1
c 1
e 3
h 1
i 2
n 2
s 2
t 1
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Instead of storing his data in a dictionary, he can also just use OrderedDict so that he won't have to sort counter.keys() every time he wants to print it – lordingtar Mar 21 '17 at 21:05
  • 2
    `OrderedDict` keeps track of the insertion order. It's not the same thing as sorting. sorting destroys order if the order was not sorting order in the first place. – Jean-François Fabre Mar 21 '17 at 21:06
  • How about `print(k, v)`? – Stefan Pochmann Mar 21 '17 at 21:30
  • @StefanPochmann: in python 2 it prints `k,v` as a `tuple` (unless you import `print_function` from `__futures__`), so using `format` `is version agnostic. – Jean-François Fabre Mar 21 '17 at 21:31
  • I appreciate your breakdown and simplification of the code. It's just that we haven't been taught collections so while I'd love to simply use your simpler solution, I wouldn't really be understanding what's going on. – Hridyansh Sharma Mar 22 '17 at 16:34
  • You don't have to use that since I pointed out your error. But look it up anyway, it's a very useful design pattern directly available as a standard – Jean-François Fabre Mar 22 '17 at 17:13