1.列表:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
1 >>> ls = list('132231321132') 2 >>> ls 3 ['1', '3', '2', '2', '3', '1', '3', '2', '1', '1', '3', '2'] 4 >>> ll=['123456'] 5 >>> ll 6 ['123456'] 7 #增 8 >>> ls.append(9) 9 >>> ls 10 ['1', '3', '2', '2', '3', '1', '3', '2', '1', '1', '3', '2', 9] 11 #改 12 >>> ls.insert(0,'10') 13 >>> ls 14 ['10', '1', '3', '2', '2', '3', '1', '3', '2', '1', '1', '3', '2', 9] 15 #删 16 >>> ls.pop(0) 17 '10' 18 >>> ls 19 ['1', '3', '2', '2', '3', '1', '3', '2', '1', '1', '3', '2', 9] 20 #查 21 >>> ls.index('3') 22 1 23 #统计 24 >>> ls.count('1') 25 4 26 >>> ls.count('3') 27 4 28 >>>
2.字典:建立学生学号成绩字典,做增删改查遍历操作。
1 #list()+zip()建立字典 2 >>> dic=dict(zip([1,2,3],['98','100','88'])) 3 >>> dic 4 {1: '98', 2: '100', 3: '88'} 5 6 #增update() 7 >>> dic2={4:'95'} 8 >>> dic.update(dic2) 9 >>> dic 10 {1: '98', 2: '100', 3: '88', 4: '95'} 11 12 #删pop()/del a[k] 13 >>> dic.pop(2) 14 '100' 15 >>> dic 16 {1: '98', 3: '88', 4: '95'} 17 #del 18 >>> dic 19 {1: '120', 3: '88', 4: '95'} 20 >>> del dic[3] 21 >>> dic 22 {1: '120', 4: '95'} 23 24 #改insert() 25 >>> dic[1]='120' 26 >>> dic 27 {1: '120', 3: '88', 4: '95'} 28 29 #查get() 30 >>> dic 31 {1: '98', 3: '88', 4: '95'} 32 >>> dic.get(4) 33 '95' 34 >>> dic.get(5,'没有') 35 '没有' 36 #直接查也可以 37 >>> dic[4] 38 '95' 39 40 # 返回一个list:keys()/values()/items() 41 >>> dic.keys() 42 dict_keys([1, 3, 4]) 43 >>> dic.values() 44 dict_values(['98', '88', '95']) 45 46 #将dict中的元素变为元祖items() 47 >>> dic.items() 48 dict_items([(1, '98'), (3, '88'), (4, '95')])
3.列表,元组,字典,集合的遍历。
1 #list 2 ls=list('654561') 3 #set 4 st={'100','100','98','95','96','96'} 5 #tuple 6 tup='a','b',1,st 7 #('a', 'b', 1, {'98', '96', '100', '95'}) 8 #dict 9 dic={'a':'blue','b':'red','c':'white'} 10 11 print('遍历list'.center(50,'=')) 12 for i in ls: 13 print(i,end=' ') 14 print(' ') 15 print('遍历set'.center(50,'=')) 16 for i in st: 17 print(i,end=' ') 18 print(' ') 19 print('遍历tuple'.center(50,'=')) 20 for i in tup: 21 print(i,end=' ') 22 print(' ') 23 print('遍历dict'.center(50,'=')) 24 for (k,v) in dic.items(): 25 print('dict[%s]='%k,v)
4.总结列表,元组,字典,集合的联系与区别。
属性 | 列表list | 元祖tuple | 字典dict | 集合set |
有序 | 是 (正向递增/反向递减) | 是 | 无 | 无 |
数据可重复 | 是 | 是 | key值唯一 | 否 |
数据可修改 | 是 | 否 | 是 | 是 |
特点 |
查询速度随内容增加而变慢 占用内存较小 |
表达固定数据项、函数多返回值、 多变量同步赋值、循环遍历等情况下适用 |
改&查操作速度快, 不会因key值增加而变慢。 占用内存大,内存浪费多 (利用空间成本换时间) |
数据独立性: 能够过滤重复参数 |
5.英文词频统计实例:待分析字符串,分解提取单词,大小写 txt.lower(),分隔符'.,:;?!-_’,计数字典,排序list.sort(),输出TOP(10)
1 #待分析字符串,分解提取单词,大小写 txt.lower(),分隔符'.,:;?!-_’,计数字典,排序list.sort(),输出TOP(10) 2 song=''' 3 Oh woah 4 Oh woah 5 Oh woahohhohh 6 7 You know you love me, I know you care 8 You shout whenever, and I'll be there 9 You want my love, you want my heart 10 And we will never ever ever be apart 11 Are we an item? Girl quit playing 12 We're just friends, what are you saying 13 Said there's another and look right in my eyes 14 My first love broke my heart for the first time 15 And I was like 16 17 Baby, baby, baby, oh like 18 Baby, baby, baby, no like 19 Baby, baby, baby, oh 20 I thought you'd always been mine, mine 21 Baby, baby, baby, oh like 22 Baby, baby, baby, no like 23 Baby, baby, baby, oh 24 I thought you'd always been mine, mine 25 Oh, Oh 26 27 For you, I would have done whatever 28 And I just can't believe we ain't together 29 And I wanna play it cool, but I'm losing you 30 I'll buy you anything, I'll buy you any ring 31 And I'm in pieces, baby fix me 32 And you'll shake me till you wake me from this bad dream 33 I'm going down, down, down, down 34 And I just can't believe my first love won't be around 35 And I'm now like 36 37 Baby, baby, baby, oh like 38 Baby, baby, baby, no like 39 Baby, baby, baby, oh 40 I thought you'd always been mine, mine 41 Baby, baby, baby, oh like 42 Baby, baby, baby, no like 43 Baby, baby, baby, oh 44 I thought you'd always been mine, mine 45 46 When I was 13 I had my first love 47 There was nobody that compared to my baby 48 And nobody came between us or could ever come above 49 She had me going crazy 50 Oh I was starstruck 51 She woke me up daily don't need no starbucks 52 She made my heart pound 53 Asking for a beat when I see her in the street 54 And at school on the playground 55 But I really wanna see her on the weekend 56 She knows she got me dazy 57 Cause she was so amazing 58 And now my heart is breaking 59 But I'll just keep on saying 60 61 Baby, baby, baby, oh like 62 Baby, baby, baby, no like 63 Baby, baby, baby, oh 64 I thought you'd always been mine, mine 65 Baby, baby, baby, oh like 66 Baby, baby, baby, no like 67 Baby, baby, baby, oh 68 I thought you'd always been mine, mine 69 Now I'm all gone 70 Now I'm all gone 71 Now I'm all gone 72 Now I'm all gone 73 I am gone 74 75 ''' 76 #1.全文转为小写 77 song=song.lower() 78 79 #2.去除标点符号 80 for i in ',.?! ': 81 song=song.replace(i,' ') 82 83 #3.分解提取单词 84 words=song.split(' ')#将字符串以' '为依据划分为一个个单词 85 86 #expend={'in','and','so','i','ll','is','her','are','a','an'} 87 #for i in expend: 88 # songs=words.replace(i,'*') 89 90 #4.做一个空字典计数 91 dic={} 92 keys=set(words)#用数组的独立性取出字典的key值 93 for i in keys: 94 dic[i]=words.count(i)#dic[i]输出key值 95 96 #5.排序 97 tupleee=list(dic.items()) 98 tupleee.sort(key=lambda x:x[1],reverse=True) 99 100 #6.输出TOP(10) 101 for i in range(10): 102 print(tupleee[i])