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()
128 * [0]creates a list of 128 elements, each with a value of 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.