博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python-组合数据类
阅读量:5377 次
发布时间:2019-06-15

本文共 3029 字,大约阅读时间需要 10 分钟。

列表:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。

s=list('kkkkk')s.insert(3,2)s.pop(3)s[6]=1print(s)print('第一个1的位置是:',s.index('1'))x=0y=0for i in range(len(s)):    if s[i]=='3':        x=x+1        if x==1:            y=iprint('三分个数为:',x,' ','第一个三分位置是:',y)print('二分个数为:',s.count('2'))

字典:建立学生学号成绩字典,做增删改查遍历操作。

>>> dic=dict(zip([1,2,3],['98','100','88']))>>> dic{
1: '98', 2: '100', 3: '88'}>>> dic2={4:'95'}>>> dic.update(dic2)>>> dic{
1: '98', 2: '100', 3: '88', 4: '95'}>>> dic.pop(2)'100'>>> dic{
1: '98', 3: '88', 4: '95'} >>> dic{
1: '120', 3: '88', 4: '95'}>>> del dic[3]>>> dic{
1: '120', 4: '95'}>>> dic[1]='120'>>> dic{
1: '120', 3: '88', 4: '95'}>>> dic{
1: '98', 3: '88', 4: '95'}>>> dic.get(4)'95'>>> dic.get(5,'没有')'没有' >>> dic[4]'95'>>> dic.keys()dict_keys([1, 3, 4])>>> dic.values()dict_values(['98', '88', '95'])>>> dic.items()dict_items([(1, '98'), (3, '88'), (4, '95')])dict

列表,元组,字典,集合的遍历。

s=list(‘456132798‘)t=set(‘4561327489‘)c={‘小明‘:‘1‘,‘小红‘:‘3‘,‘小华‘:‘2‘}x=(1, 2, 3, 4, 5 )print(‘列表的遍历为:‘,end=‘‘)for i in s:    print(i,end=‘‘)print()print(‘元祖的遍历为:‘,end=‘‘)for i in x:    print(i,end=‘‘)print()print(‘字典key的遍历为:‘,end=‘‘)for i in c:    print(i,end=‘‘)print()print(‘字典value的遍历为:‘,end=‘‘)for i in c.values():    print(i,end=‘‘)print()print(‘数组的遍历为:‘,end=‘‘)for i in x:    print(i,end=‘‘)

 

 

'''下载一首英文的歌词或文章,统计单词出现的次数,将所有,.?!替换为空格,将所有大写转换为小写。'''sr='''You're ?insecureDon't know what forYou're turning heads through the doorDon't need make-upto cover upBeing the way that you are is enoughEveryone else in the room can see itEveryone else but youBaby you light up my world like nobody elseThe way that you flip your hair gets me overwhelmedBut when you smile at the ground it ain't hard to tellYou don't know Oh ohYou don't know you're beautifulIf only you saw what I can seeYou'll understand why I want you so desperatelyRight now I'm looking at you and I can't believeYou don't know Oh !ohYou don't know you're beautiful Oh ohThat's what makes you beautifulSo c-come onYou got it wrongTo prove I'm ,right '''print(sr.count('you'))for i in sr:    sr=sr.replace(',','/t ')    sr=sr.replace('!','/t ')    sr=sr.replace('?','/t ')print(sr)for i in sr:    sr=sr.upper()print(sr)

 

 

英文词频统计实例

s='''Well I wonder could it be When I was dreaming about you babyYou were dreaming of me Call me crazy Call me blind To still be sufferingis stupid after all of this time Did I lose my love to someone betterAnd does she love you like I do I do, you know I really really doWell hey So much I need to say Been lonely since the day The day you went awaySo sad but true For me there‘s only you Been crying since the daythe day you went away.!?'''print("do出现次数",s.count('do'))s=s.replace(",","")s=s.replace(‘.‘,‘‘)s=s.replace(‘?‘,‘‘)s=s.replace(‘!‘,‘‘)s=s.replace(‘\n‘,‘‘)s=s.lower()s1=s.split(‘ ‘)key=set(s1)dic={}for i in key:    dic[i]=s.count(i)wc=list(dic.items())wc.sort(key=lambda x:x[1],reverse=True)for i in range(10):print(wc[i])

 

转载于:https://www.cnblogs.com/yxhjss/p/7567758.html

你可能感兴趣的文章
数据库设计不推荐使用Bool类型
查看>>
POJ 3281 Dining 【最大流】【神建模】
查看>>
查看当前运行的SQL语句
查看>>
js一些常用方法总结
查看>>
PHP二次开发常用的工具|只能在服务器上调试用什么工具开发
查看>>
Windows Azure Virtual Network (10) 使用Azure Access Control List(ACL)设置客户端访问权限
查看>>
宇宙中最强大的开发环境免费了!
查看>>
C#中运行bat
查看>>
lang3 StringUtils
查看>>
Sniffer
查看>>
nodejs 实现继承
查看>>
android闹钟(三):实现时钟功能
查看>>
2.1 容器的基本实现
查看>>
用映射的方法获取当前方法的名称
查看>>
一个值得纪念的日子
查看>>
Android 访问 Webapi 更新UI
查看>>
极角排序详解:
查看>>
数据结构之图形结构
查看>>
设计方法小总结
查看>>
Verify Preorder Serialization of a Binary Tree
查看>>