列表、元组、字典、集合的相关练习
一、建立学号成绩字典,并进行怎删改操作
s = {'01':'100','02':'99','03':'98','04':'97','05':'96','05':'96','06':'95','07':'98','08':'90','09':'91'} print('成绩表:',s) s.pop('09') print('成绩表:',s) print('主键是:',s.keys()) print('分数是:',s.values()) x = input('输入学号查分数:') print(s.get(x,"没有这个人的分数"))
二、练习相关列表、元组等函数的使用
s = list('123321123321112321322212311321313131313213213131321') s.append('99') print(s) print('删除第十一个元素:',s.pop(10)) print('3总数是:',s.count(3))
三、遍历循环输出列表、元组、集合等数据
ls = list('123456789123') ln = tuple('123456789123') s = {'01':'100','02':'99','03':'98','04':'97','05':'96','05':'96','06':'95','07':'98','08':'90','09':'91'} a= set('123456789123') print(ls) print(ln) print(s) print(a) for i in ls: print(i,' ',end='') for j in ln: print(j,' ',end='') for k in s: print(k) for l in a: print(l,'',end='')
四、列表、元组、字典、集合的概念以及一些区别
列表:列表是一些可以重复,类型不同的元素的一个清单这样子的一个东西,可读可修改,符号为[],可以使用append、pop等进行增删改计数操作等。
元组:和列表的不同之处在于只读不可修改,符号为()。
字典:字典里面存的是值对,有键和值得区分,符号为{}。
集合:可以通过set函数实现集合,集合的符号也是{}
五、在英文歌词中进行取词计数等操作
lo = '''At the moment, the sky is dark, the air is fresh factor after just rained. Suddenly thought of blue plaid shirt; Those were broken into various shapes of stationery;From the corner at the beginning of deep friendship; Have declared the end of the encounter that haven't start planning... Those years, those days of do, finally, like youth, will end in our life.''' lo = lo.lower() for i in ',.': lo = lo.replace(i,' ') words = lo.split(' ') dic = {} keys = set(words) for i in keys: dic[i]=words.count(i) t = sorted(dic) dd = list(dic.items()) dd.sort(key = lambda x:x[1],reverse = True) for i in range(10): print(dd[i])