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

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

1、字典

d={'Tom':'01','Helen':'02','David':'03','Jim':'04'}增d['Wang']='05'删d.pop('Tom')改d['Tom']='06'查d['Tom']

2、集合

a=set('12345')b=set('56789')增a.add('6')删b.remove('9')并集a&b交集a|ba-b

  3、英文词频统计实例

abc='''every night in my dreamsi see you, i feel you,that is how i know you go onfar across the distanceand spaces between usyou have come to show you go onnear, far, wherever you arei believe that the heart does go ononce more you open the doorand you're here in my heartand my heart will go on and onlove can touch us one timeand last for a lifetimeand never let go till we're onelove was when i loved youone true time i hold toin my life we'll always go onnear, far, wherever you arei believe that the heart does go ononce more you open the doorand you're here in my heartand my heart will go on and onthere is some love that will not go awayyou're here, there's nothing i fear,and i know that my heart will go onwe'll stay forever this wayyou are safe in my heartAND my heart will go on and on'''abc=abc.lower()for i in ',.':    abc=abc.replace(i,' ')words=abc.split(' ')print(words)di{}words.sort()disc = set(words)for i in disc:      di[i] = 0for i in words:    di[i] = di[i]+1items = di.items()print(items)


  

  

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

   列表查找和插入的时间随元素的增加而增加,占用空间小,浪费内存很少;列表可以有N个元素,元素的类型是任意的,与列表本身无关。字典的查找和插入速度极快,不会随key的增加而变慢,需要占用大量内存,内存浪费多。字典使用键—值存储名具有极快的查找速度。字典的key必须是不可变对象。Python的集合(set)和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算.由于集合是无序的,所以,sets 不支持 索引, 分片, 或其它类序列(sequence-like)的操作。

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

列表l = list('23132111123132')元组t = tuple('1233211232123')字典d = dict(zip([1,2,3,4],[4,3,2,1]))集合s = set(l)for i in l:print(i)for i in t:print(i)for i in s:print(i)for i in d:print(i)

  

转载于:https://www.cnblogs.com/Sun584125503/p/7573164.html

你可能感兴趣的文章
Jquery getJSON 实现跨域请求 --- callback
查看>>
Zend Studio (eclipse)使用速度优化
查看>>
Linux系统各个目录的一般作用
查看>>
maven安装与配置
查看>>
Windows Phone 8 开发环境配置(记录)
查看>>
MVC
查看>>
使用重置按钮,重置表单信息
查看>>
在指定时间干,必须干(kbmmw 中的事件调度)
查看>>
通过微信查找SAP TCODE代码
查看>>
c 二叉树的使用
查看>>
Helpers\Assets
查看>>
Thrift安装问题
查看>>
Linux常用命令大全
查看>>
JavaScript:下拉列表框的事件处理
查看>>
ASP.NET Aries 3.0发布(附带通用API设计及基本教程介绍)
查看>>
jQuery EasyUI实现关闭全部tabs
查看>>
poj3635Full Tank?[分层图最短路]
查看>>
【USACO 1.4】Mother's Milk
查看>>
CentOs 设置静态IP 方法
查看>>
使用Maven构建多模块项目
查看>>