开发者

Python character count

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 开发者_如何学JAVAwhat 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.

>>> 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.


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]]


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜