py第二天

1.统计小写字母出现的次数

str=input("please enter a text:")
str=str.lower()
lst=[chr(i) for i in range(97,123)]
print(lst)

res={}
for i in str:
    if i in lst:
        res[i]=res.get(i,0)+1
        
res2=list(res.items())
res2.sort(key=lambda x:x[1],reverse=True)
print(res2)

for i in range(len(res2)):
    word,count=res2[i]
    print("{:5}{:5}".format(word,count))
 

2.统计出现的数据的次数

str=input("please enter a text:")
a={}
for i in str:
    a[i]=a.get(i,0)+1
b=list(a.items())
b.sort(key=lambda x:x[1],reverse=True)
for i in range(len(b)):
    word,count=b[i]
    print("{:5}{:5}".format(word,count))

3.

str=input("please enter a text:")
res={}
for i in str:
    res[i]=res.get(i,0)+1
print(res)

res2=list(res.items())
res2.sort(key=lambda x:x[1],reverse=True)
print(res2)

for i in range(len(res2)):
    word,count=res2[i]
    print("{:5}{:5}".format(word,count))

4.随机生成验证码

import random
lst1=[chr(i) for i in range(97,123)]
lst2=[chr(i) for i in range(65,91)]
lst3=[i for i in range(0,10)]
res=lst1+lst2+lst3
for i in range(10):
    for i in range(8):
        print(random.choice(res),end='')
    print()

//反复用到了构造列表

运用文件的相关知识,矩阵,excel

import random
word=''
for row in range(10):
    for line in range(10):
        word+=str(random.randint(1,100))+' '
    word+='\n'
print(word)
f=open('2.txt','w')
f.write(word)
f.close()

x=open('1.csv','w')
y=open('2.txt','r')
for i in y:
    s=i.replace(' ',',')
    x.write(s)
x.close()
y.close()
 

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值