tags:

views:

102

answers:

3

I have been going over python tutorials in this resource. Everything is pretty clear in the below code which counts number of characters. Only section that i dont understand is the section where count assigned to a list and multiplied by 120. Can anyone explain what is the purpose of this in plain english please.

def display(i):
    if i == 10: return 'LF'
    if i == 13: return 'CR'
    if i == 32: return 'SPACE'
    return chr(i)

infile = open('alice_in_wonderland.txt', 'r')
text = infile.read()
infile.close()

counts = 128 * [0]

for letter in text:
    counts[ord(letter)] += 1

outfile = open('alice_counts.dat', 'w')
outfile.write("%-12s%s\n" % ("Character", "Count"))
outfile.write("=================\n")

for i in range(len(counts)):
    if counts[i]:
        outfile.write("%-12s%d\n" % (display(i), counts[i]))

outfile.close()
+5  A: 

128 * [0] creates a list of 128 elements, each with a value of 0.

>>> 3 * [0]
[0, 0, 0]

Then, since valid ASCII characters are in the range 0-127, each letter accesses an index in counts (ord(letter) will return the numeric value of a character), and increments the value at that index.

For example, the character '0' corresponds to a numeric value of 48. So when a '0' is encountered, counts[48] is incremented by 1.

Mark Rushakoff
+1  A: 

The line counts = 128 * [0] populates a 128-element list with zeroes.

This list is then use to increment character counts based on each character's ascii value, since ascii values range from 0 to 127.

Justin Ardini
+3  A: 

As the other answers explain, it creates a 128-list of 0. What they don't explain is that it's the same 0, 128 times. Since int is immutable this masks a problem that many novice Python programmers run into:

>>> l = [[]] * 5
>>> l
[[], [], [], [], []]
>>> l[0].append(1)
>>> l
[[1], [1], [1], [1], [1]]
Ignacio Vazquez-Abrams
Yes, that's dangerous but only for complex data types. It's OK in Mark's example since there are only ints in the list.
mmmasterluke
Of course, if all one does is *assign* objects to the list indices, (im)mutability of the list contents is totally irrelevant: `l= 4*[[]]; print l; l[0]= "hi"; print l`.
ΤΖΩΤΖΙΟΥ