作業6
1、字典實例:建立學生學號成績字典,做增刪改查遍歷操作。
建立:
d={‘0001‘:‘99‘,‘0003‘:‘89‘,‘0004‘:‘98‘,‘0005‘:‘100‘,‘0006‘:‘78‘}
增:
d[‘0002‘]=‘79‘
刪:
d.pop(‘0001‘)
改:
d[‘0004‘]=100
查:
print(d[‘0002‘])
遍歷操作:
for i in d:
print(d)
2、列表,元組,字典,集合的遍歷。
l=list(‘12438849374837‘)
for i in l:
print(l)
t=tuple(‘nhaildhabvka‘)
for i in t:
print(t)
d={‘0001‘:‘99‘,‘0003‘:‘89‘,‘0004‘:‘98‘,‘0005‘:‘100‘,‘0006‘:‘78‘}
for i in d:
print(d)
s=set(l)
for i in s:
print(s)
總結列表,元組,字典,集合的聯系與區別。
列表:是一組有序序列,能進行查詢,增加,刪除,修改。是用中括號【】括起來的。
元組:是一組有序序列,能進行查詢。是用小括號()括起來的。
字典:字典不是有序序列,是由一組組鍵值對組成。能進行增刪改查等操作。是用大括號{}括起來的。
集合:集合也不是有序序列,能進行查詢等操作。是用大括號{}括起來的。
3、英文詞頻統計實例
(1)待分析字符串
bin=‘‘‘Put your make up on
Get your nails done
Curl your hair
Run the extra mile
Keep it slim
so they like you, do they like you?
Get your sexy on
Don‘t be shy, girl
Take it off
This is what you want, to belong, so they like you
Do you like you?
You don‘t have to try so hard
You don‘t have to, give it all away
You just have to get up, get up, get up, get up
You don‘t have to change a single thing
You don‘t have to try, try, try, try
You don‘t have to try, try, try, try
You don‘t have to try, try, try, try
You don‘t have to try
Yooou don‘t have to try
Oooh Oooh
Get your shopping on, at the mall, max your credit card
You don‘t have to choose, buy it all, so they like you
Do they like you?
Wait a second,Why, should you care, what they think of you
When you‘re all alone, by yourself, do you like you?
Do you like you?You don‘t have to try so hard
You don‘t have to, give it all away
You just have to get up, get up, get up, get up
You don‘t have to change a single thing
You don‘t have to try so hard
You don‘t have to bend until you break
You just have to get up, get up, get up, get up
You don‘t have to change a single thing
You don‘t have to try, try, try, try
You don‘t have to try, try, try, try
You don‘t have to try
You don‘t have to try
You don‘t have to try, try, try, try
You don‘t have to try, try, try, try
You don‘t have to try, try, try, try
You don‘t have to tryYooou don‘t have to try
NooooOooh
You don‘t have to try so hard
You don‘t have to, give it all away
You just have to get up, get up, get up, get up
You don‘t have to change a single thing
You don‘t have to try, try, try, tryYou don‘t have to try, try, try, try
You don‘t have to try, try, try, try
You don‘t have to try
Take your make up off
Put your head down
Take a breath
Look into the mirror, at yourself.
Don‘t you like you?
Cause I like you!‘‘‘
(2)分解提取單詞
大小寫 txt.lower()
bin=bin.lower()
分隔符‘.,:;?!-_’
for i in ‘,.?!\n‘:
bin=bin.replace(i,‘ ‘)
單詞列表
bin=bin.split(" ")
(3)單詞計數字典
words=set(bin)
d={}
d[‘star‘]=bin.count(‘star‘)
for a in words:
d[a]=bin.count(a)
for a in d:
print("{0:<11}{1}".format(a,d[a])
作業6