如果我们知道先例字符,我有一个代码可以计算一个字符的出现次数。
这是我尝试过的,但它不起作用。
该文件仅包含字符为“K”、“L”、“G”、“A”、“S”、“”的单词。
text = open("fichier_a_compresser 1.txt", 'r')
alphabet = ("K", "L", "G", "A", "S", " ")
for i in text:
characterlist = list(i)
j = 0
cont = 0
for i in alphabet:
for k in alphabet:
while j < len(characterlist):
if (characterlist[j-1]==k and characterlist[j]==i):
cont = cont + 1
j = j + 1
print str(i) + " appears after the character " + str(k) + " " + str(cont) + " times."
cont = 0
我认为我在“继续”部分做错了,因为出口始终为 0。
在此先感谢