当前位置:首页 > Software > Python > 正文内容

列表实例

Doraemon5年前 (2020-02-21)Python5019

随机生成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

>>> 


扫描二维码推送至手机访问。

版权声明:本文由我的FPGA发布,如需转载请注明出处。

本文链接:http://myfpga.cn/index.php/post/133.html

分享给朋友:
返回列表

上一篇:random库

下一篇:顺序查找

“列表实例” 的相关文章

1.Python基本的使用

1.Python基本的使用

我们打开python或者通过运行python也可以,请复制如下代码,然后按下Enter键,看看会发生什么?print('\n'.join([''.join([('MyFpga'[(x-y) % len('MyFpga'...

  索引运算符【】

索引运算符【】

选择字符串的子序列语法【start:finish】        start:子序列开始位置的索引值        finish:子序列结束位置的下一个字符的索引值如果不提供start或者finish,默认start为第一个字符,finish为最后一个字符。例如>>>my_str=...

列表作为函数参数

列表作为函数参数

列表作为函数参数,函数中可以修改原列表def multiply(values,factor):     for i in range(len(values)):        values[i]*=factor          aList=[1,2,3,4,5]  multiply(aL...

(原创)使用Python对任意网站图片进行爬取,仅用于学习

(原创)使用Python对任意网站图片进行爬取,仅用于学习

import os import time import argparse import requests import re import io from urllib.parse import ...

(原创)使用Python自动对子文件夹中的图片文件进行重命名

(原创)使用Python自动对子文件夹中的图片文件进行重命名

为了解决Python深度学习的时候,经常遇到的文件名问题import os # 获取指定目录下的所有子文件夹 def get_subfolders(path):     subfolders = []...

(原创)使用Python提取ISE工程的RTL代码

(原创)使用Python提取ISE工程的RTL代码

在工程文件夹下运行Python程序即可 #Author       : / #Description  : 从ISE的项目文件夹中提取rtl文件,用于LEDA调试 #Time ...