列表实例
随机生成100个小写字母存入一个列表中,统计26个字母的出现次数。
import random
def getRandomLetter():
code_a=ord('a')
code_z=ord('z')
x=random.randint(code_a,code_z)
return chr(x)
def createList(n):
chars=[]
for i in range(n):
chars.append(getRandomLetter())
return chars
def countLetters(chars):
counts=[0]*26
for i in range(len(chars)):
counts[ord(chars[i])-ord('a')]+=1
return counts
def main():
chars=createList(100)
print("The lowercase letters are:")
for i in range (len(chars)):
print(chars[i],end='')
print()
counts=countLetters(chars)
print("The occurrences of each letters are:")
for i in range(26):
print(chr(i +ord('a')),':',sep='',end='')
print(counts[i])
main()The lowercase letters are:
c
The occurrences of each letters are:
a:0
b:0
c:1
d:0
e:0
f:0
g:0
h:0
i:0
j:0
k:0
l:0
m:0
n:0
o:0
p:0
q:0
r:0
s:0
t:0
u:0
v:0
w:0
x:0
y:0
z:0
>>>



