9.20作业

(1)总结列表,元组,字典,集合的联系与区别。

列表:

可以用list()函数或者方括号[]创建,元素之间用逗号’,‘’分隔。 

列表的元素不需要具有相同的类型 

可以进行增删改查操作

元祖:

可以用tuple()函数或者方括号()创建,元素之间用逗号’,‘’分隔。 

元组的元素不需要具有相同的类型 

元素的值一旦创建就不可修改

字典:

元素由键(key)和值(value)组成 

可以用dict()函数或者方括号()创建,元素之间用逗号’,‘’分隔,键与值之间用冒号”:”隔开 

键必须是唯一的,但值则不必。值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组 

使用键(key)来访问元素

集合:

可以用set()函数或者方括号{}创建,元素之间用逗号”,”分隔。 

与字典相比少了键  

不可以有重复元素

 

(2)列表,元组,字典,集合的遍历。

 列表:

classmate=['Michael','Bob','luck','angle','李斯',55]
new=['nance']
classmate[0]
classmate[-1]
classmate[0:3]
classmate.insert(4,'gfds')
classmate.append('fe')
classmate.extend(new)
classmate.pop()
del classmate[0]
#   print(classmate)
for stu in classmate:
    print(stu)

 

元组:

tuple=(("apple","banana"),("grape","orange"),("watermelon",),("grapefruit",))
for i in range(len(tuple)) :
   print(tuple[i],"")

字典和集合:

st = set('集合的遍历')        #集合的遍历
print(st)
for b in st:
    print(b)

dt = {'a':80,'b':79,'c':90}        #字典的遍历
print(dt)
for i in dt:
    print(i,dt[i])

 

 

 

 

 

 

 

 

 

 

(3)英文词频统计:

str ='''There's a girl but I let her get away 
It's all my fault cause pride got in the way 
And I'd be lying if I said I was OK 
About that girl the one I let get away 
I keep saying no 
This can't be the way we're supposed to be 
I keep saying no 
There's gotta be a way to get you close to me 
Now I know you gotta 
Speak up if you want somebody 
Can't let him get away oh no 
You don't wanna end up sorry The way that I'm feeling everyday 
Don't you know 
No no no no 
There's no home for the broken heart 
Don't you know 
No no no no 
There's no home for the broken 
There's a girl but I let her get away 
It's my fault cause I said I needed space 
I've been torturing myself night and day 
About that girl the one I let get away 
I keep saying no 
This can't be the way we're supposed to be 
I keep saying no 
There's gotta be a way to get you 
Gotta be a way 
To get you close to me 
You gotta 
Speak up if you want somebody 
Can't let him get away oh no 
You don't wanna end up sorry 
The way that I'm feeling everyday 
Don't you know 
No no no no 
There's no home for the broken heart 
Don't you know 
No no no no 
There's no home for the broken 
No home for me 
No home cause I'm broken 
No room to breathe 
And I got no one to blame 
No home for me 
No home cause I'm broken 
About that girl 
The one I let get away 
So you better 
Speak up if you want somebody 
You can't let him get away no no 
You don't wanna end up sorry 
The way that I'm feeling everyday 
Don't you know 
No no no no 
There's no home for the broken heart 
Don't you know 
No no no no 
There's no home for the broken 
Oh 
You don't wanna lose at love 
It's only gonna hurt too much 
I'm telling you 
You don't wanna lose at love 
It's only gonna hurt too much 
I'm telling you 
You don't wanna lose at love 
Cause there's no home for the broken heart 
That girl 
The one I let get away 
'''
daxie = str.upper()
jishu = str.count('no')
print(str.split(' '))
print(daxie)
print(jishu)

 

 

 

 

统计每个单词出现的次数 dict

posted @ 2018-09-20 11:57  与冰  阅读(139)  评论(0编辑  收藏  举报