标签:set range 直接 英文 top txt not key rds
列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
>>> ls=list(‘123321231‘) >>> ls [‘1‘, ‘2‘, ‘3‘, ‘3‘, ‘2‘, ‘1‘, ‘2‘, ‘3‘, ‘1‘] >>> ls.sort() >>> ls [‘1‘, ‘1‘, ‘1‘, ‘2‘, ‘2‘, ‘2‘, ‘3‘, ‘3‘, ‘3‘] >>> ls.pop() ‘3‘ >>> ls [‘1‘, ‘1‘, ‘1‘, ‘2‘, ‘2‘, ‘2‘, ‘3‘, ‘3‘] >>> ls.append(4) >>> ls [‘1‘, ‘1‘, ‘1‘, ‘2‘, ‘2‘, ‘2‘, ‘3‘, ‘3‘, 4] >>> ls.insert(2,5) >>> ls [‘1‘, ‘1‘, 5, ‘1‘, ‘2‘, ‘2‘, ‘2‘, ‘3‘, ‘3‘, 4] >>> ls.index(5) 2 >>> ls[1]=7 >>> ls [‘1‘, 7, 5, ‘1‘, ‘2‘, ‘2‘, ‘2‘, ‘3‘, ‘3‘, 4] >>> s=list(‘yzlyuan‘) >>> s [‘y‘, ‘z‘, ‘l‘, ‘y‘, ‘u‘, ‘a‘, ‘n‘] >>> ls [‘1‘, 7, 5, ‘1‘, ‘2‘, ‘2‘, ‘2‘, ‘3‘, ‘3‘, 4] >>> ls.pop(0) ‘1‘ >>> ls [7, 5, ‘1‘, ‘2‘, ‘2‘, ‘2‘, ‘3‘, ‘3‘, 4] >>> ls.index(‘3‘) 6 >>> ls.count(‘3‘) 2 >>> ls.count(‘2‘) 3 >>>
字典实例:建立学生学号成绩字典,做增删改查遍历操作。
#list()+zip()建立字典 >>> dic=dict(zip([1,2,3],[‘98‘,‘100‘,‘88‘])) >>> dic {1: ‘98‘, 2: ‘100‘, 3: ‘88‘} #增update() >>> dic2={4:‘95‘} >>> dic.update(dic2) >>> dic {1: ‘98‘, 2: ‘100‘, 3: ‘88‘, 4: ‘95‘} #删pop()/del a[k] >>> dic.pop(2) ‘100‘ >>> dic {1: ‘98‘, 3: ‘88‘, 4: ‘95‘} #del >>> dic {1: ‘120‘, 3: ‘88‘, 4: ‘95‘} >>> del dic[3] >>> dic {1: ‘120‘, 4: ‘95‘} #改insert() >>> dic[1]=‘120‘ >>> dic {1: ‘120‘, 3: ‘88‘, 4: ‘95‘} #查get() >>> dic {1: ‘98‘, 3: ‘88‘, 4: ‘95‘} >>> dic.get(4) ‘95‘ >>> dic.get(5,‘没有‘) ‘没有‘ #直接查也可以 >>> dic[4] ‘95‘ # 返回一个list:keys()/values()/items() >>> dic.keys() dict_keys([1, 3, 4]) >>> dic.values() dict_values([‘98‘, ‘88‘, ‘95‘]) #将dict中的元素变为元祖items() >>> dic.items() dict_items([(1, ‘98‘), (3, ‘88‘), (4, ‘95‘)])
列表,元组,字典,集合的遍历。
>>>ls=list("4613125646") >>>tu=tuple("4613125646") >>>s=set("4613125646") >>>d={‘小洪‘:93,‘小英‘:74,‘小杰‘:45,‘小均‘:66} >>>print("列表:",ls) >>>for i in ls: print(i,end=‘ ‘) >>>print("\n") >>>print("元组:",tu) >>>for i in tu: print(i,end=‘ ‘) >>>print("\n") >>>print("集合:",s) >>>for i in s: print(i,end=‘ ‘) >>>print("\n") >>>print("字典:",d) >>>for i in d: print(i,end=‘\t‘) >>>print(d[i],end=‘\n‘)
总结列表,元组,字典,集合的联系与区别。
属性 | 列表list | 元祖tuple | 字典dict | 集合set |
有序 | 是 (正向递增/反向递减) | 是 | 无 | 无 |
数据可重复 | 是 | 是 | key值唯一 | 否 |
数据可修改 | 是 | 否 | 是 | 是 |
特点 |
查询速度随内容增加而变慢 占用内存较小 |
表达固定数据项、函数多返回值、 多变量同步赋值、循环遍历等情况下适用 |
改&查操作速度快, 不会因key值增加而变慢。 占用内存大,内存浪费多 (利用空间成本换时间) |
数据独立性: 能够过滤重复参数
|
英文词频统计实例:待分析字符串,分解提取单词,大小写 txt.lower(),分隔符‘.,:;?!-_’,计数字典,排序list.sort(),输出TOP(10)
faded = ‘‘‘You were the shadow to my light Did you feel us? Another start You fade away Afraid our aim is out of sight Wanna see us Alive Where are you now? Where are you now? Where are you now? Was it all in my fantasy? Where are you now? Were you only imaginary? Where are you now? Atlantis Under the sea Under the sea Where are you now? Another dream The monster‘s running wild inside of me I‘m faded I‘m faded So lost, I‘m faded I‘m faded So lost, I‘m faded These shallow waters never met what I needed I‘m letting go a deeper dive Eternal silence of the sea. I‘m breathing alive Where are you now? Where are you now? Under the bright but faded lights You‘ve set my heart on fire Where are you now? Where are you now? Where are you now? Atlantis Under the sea Under the sea Where are you now? Another dream The monster‘s running wild inside of me I‘m faded I‘m faded So lost, I‘m faded I‘m faded So lost, I‘m faded‘‘‘ faded = faded.lower() for i in ‘?!,.\‘\n‘: faded = faded.replace(i,‘ ‘) words = faded.split(‘ ‘) dic={} keys = set(words) for i in keys: dic[i]=words.count(i) c = list(dic.items()) c.sort(key=lambda x:x[1],reverse=True) for i in range(10): print(c[i])
标签:set range 直接 英文 top txt not key rds
原文地址:http://www.cnblogs.com/junjun21/p/7569460.html