组合数据类型练习,英文词频统计实例上

1.字典实例:建立学生学号成绩字典,做增删改查遍历操作。

dict={"01":"90","02":"91","03":"92","04":"94","05":"95","06":"96","07":"97","08":"98"}
print("查询02学号的成绩")
print(dict.get('02'))
print("删除07号的记录")
dict.pop('07')
print(dict.items())
print("将01的成绩改为100")
dict['01']=100
print(dict.items())
print("添加18号学生")
dict["18"]="100"
print(dict.items())
print("遍历:")
for i in dict:
    print(i)

运行:

2.列表,元组,字典,集合的遍历。
总结列表,元组,字典,集合的联系与区别。

lis=["01","02","03","04"]
tu=("01","02","03","04","05")
dic={"01":"91","02":"92","03":"93","04":"94"}
s=set(lis)
for i in lis:
    print(i)


for i in tu:
    print(i)

for i in dic:
   print('{0:<}:{1:>}'.format(i,dic[i]))

for i in s:
    print(i)

运行:

答:列表是有序的元素集合,元组与列表很相像,但是最大的区别就是元组只能被读取而不能被修改。字典是以键值对作为单位来进行存放的集合。集合(set)则是一组key的无序集合,但是它只包含key,key值不能够重复,并且需要先创建一个List作为输入集合才能继续创建set。

3英文词频统计实例

bad='''your butt is mine

I Gonna tell you right

Just show your face

In broad daylight

I'm telling you

On how I feel

Gonna Hurt Your Mind

Don't shoot to kill

Shamone

Shamone

Lay it on me

All right

I'm giving you

On count of three

To show your stuff

Or let it be

I'm telling you

Just watch your mouth
I know your game

What you're about

Well they say the sky's the limit

And to me that's really true

But my friend you have seen nothin'

Just wait till I get through

Because I'm bad,I'm bad

shamone

(Bad,bad,really,really bad)

You know I'm bad,I'm bad

(Bad,bad,really,really bad)

You know it

You know I'm bad,I'm bad

Come on,you know

(Bad,bad,really,really bad)

And the whole world

Has to answer right now

Just to tell you once again

Who's bad

The word is out

You're doin' wrong

Gonna lock you up

Before too long

Your lyin' eyes

Gonna tell you right

So listen up

Don't make a fight

Your talk is cheap

You're not a man

Your throwin' stones

To hide your hands

Well they say the sky's the limit

And to me that's really true

But my friend you have seen nothin'

Just wait till I get through

Because I'm bad,I'm bad

shamone

(Bad,bad,really,really bad)

You know I'm bad,I'm bad

(Bad,bad,really,really bad)

You know it

You know I'm bad,I'm bad

Come on,you know

(Bad,bad,really,really bad)

And the whole world

Has to answer right now

Just to tell you once again

Who's bad

We could change the world tomorrow

This could be a better place

If you don't like what I'm sayin'

Then won't you slap my face

Because I'm bad'''

bad=bad.lower()
for i in ",.?!()":
    bad=bad.replace(i,' ')
bad=bad.replace('
',' ')
words=bad.split(' ')

s=set(words)
dic={}

lis=[]
value=[]
for i in s:
    if(i==" "):
        continue
    if(i==""):
        continue  
    dic[i]=words.count(i)
    lis.append(words.count(i))
    value=dic.values()

lis=list (dic.items())
lis.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
    print(lis[i])

 

运行:

 

  

原文地址:https://www.cnblogs.com/mavenlon/p/7573333.html