博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
组合数据类型练习,英文词频统计实例上
阅读量:4364 次
发布时间:2019-06-07

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

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

d=['001','002','003','004','005']score=[88,75,66,98,79]grade=dict(zip(d,score))grade['006']=90  print('增加一个学生:',grade)  #增grade.pop('004')    print('删除一个学生:',grade)  #删grade['002']=78print('修改一个学生:',grade)  #改print('查找11学生分数:',grade['001'])print('查询字典里面没有的学生:',grade.get('007','找不到该学生'))
 

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

list1=list('112233')print('列表:',list1)print('列表遍历:')for i in list1:    print(i)print()list2=tuple('645654646')print('元组:',list2)print('元组遍历:')for i in list2:    print(i)print()names=['Mary','Bob','Jhon']scores=[78,85,44]d={}d=dict(zip(names,scores))print('字典:',d)print('字典遍历:')for i in d:    print(i,d[i])print()grades=['3','3','6','6','9','9']di=set(grades)print('集合:',di)print('集合遍历:')for i in di:    print(i)
 

总结列表,元组,字典,集合的联系与区别。

列表:特点就是:可重复,类型可不同。类型不同也是跟数组最本质的区别了。python里的列表用“[]”表示。

元组:元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。元组用“()”表示

字典:字典定义了键和值之间一对一的关系,但它们是以无序的方式储存的。 Python 中的 dictionary 像 Java 中的 Hashtable 类的实例。定义 Dictionary 使用一对大(花)括号” { } “

           Dictionary 不只是用于存储字符串。Dictionary 的值可以是任意数据类型,包括字符串、整数、对象,甚至其它的 dictionary。

           在单个 dictionary 里,dictionary 的值并不需要全都是同一数据类型,可以根据需要混用和匹配。

集合: Python的集合(set)和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数               学运算.由于集合是无序的,所以,sets 不支持 索引, 分片, 或其它类序列(sequence-like)的操作。

            集合也存在不可变形式,frozenset为固定集合.

            set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。

              注意:想要创建空集合,你必须使用 set() 而不是 {} ,后者用于创建空字典

3.英文词频统计实例

q='''When I was just a lad of ten, my father said to me,  "Come here and take a lesson from the lovely lemon tree." "Don't put your faith in love, my boy", my father said to me, "I fear you'll find that love is like the lovely lemon tree." Lemon tree very pretty and the lemon flower is sweet but the fruit of the poor lemon is impossible to eat. Lemon tree very pretty and the lemon flower is sweet but the fruit of the poor lemon is impossible to eat. One day beneath the lemon tree, my love and I did lie  A girl so sweet that when she smiled the stars rose in the sky.'''print('各单词出现的次数:')q=q.lower()  # 歌词替换为小写for i in ',.':    q=q.replace(i,' ')  #替换空格为分隔符words=q.split(' ')     di = {}words.sort()disc = set(words)  #统计单词出现个数for i in disc:    di[i] = 0for i in words:    di[i] = di[i]+1new = di.items()print(new)
 

 

转载于:https://www.cnblogs.com/1031353319qq/p/7598025.html

你可能感兴趣的文章
switch… case 语句的用法
查看>>
day07补充-数据类型总结及拷贝
查看>>
语言、数据和运算符
查看>>
正则表达式30分钟入门教程
查看>>
sqlserver try catch·
查看>>
1028: 可乐(2018年中南大学研究生复试机试题 )
查看>>
珍藏的最全的windows操作系统快捷键
查看>>
【DBAplus】SQL优化:一篇文章说清楚Oracle Hint的正确使用姿势
查看>>
二叉树结点删除操作
查看>>
图论-单源最短路-SPFA算法
查看>>
转换文件的字符集
查看>>
软件质量理解
查看>>
jquery 在 table 中修改某行值
查看>>
pyc文件是什么【转载】
查看>>
POM.xml 标签详解
查看>>
hdu 3635 Dragon Balls (并查集)
查看>>
文件操作
查看>>
7.java集合,泛型简单总结,IO流
查看>>
杭电2007 平方和与立方和
查看>>
JS邮箱验证-正则验证
查看>>