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

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

d={'001':'90','002':'80','003':'75','004':'65','005':'85'}
print(d.get('001'))
d.pop('002')
d['003']=65
for i in d:
    print(i)

 

2.

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

复制代码
l=list('12345678')
print('遍历列表:')
for i in l:
    print(i)
t=tuple('abcdefg')
print('遍历元组:')
for i in t:
    print(i)
d={'001':50,'002':90,'003':85}
print('遍历字典:')
for i in d:
    print(i,d[i])
s=set(l)
print('遍历集合:')
for i in s:
    print(i)
复制代码

1.列表是任意对象的序列,用方括号表示。

2.将一组值打包到一个对象中,称为元组。元组用圆括号表示.元组和列表的大部分操作相同。但是,列表是不固定的,可以随时插入,删除;而元组一旦确认就不能够再更改。所以,系统为了列表的灵活性,就需要牺牲掉一些内存;而元组就更为紧凑。

3.集合是无序的,也不能通过索引进行访问且集合中的元素不能重复。

4.字典就是一个关联数组或散列表,其中包含通过关键字索引的对象。用大括号表示。与集合相比,通过关键字索引,所以比集合访问方便。

 

3.英文词频统计实例

A.待分析字符串

B.分解提取单词
a.大小写 txt.lower()
b.分隔符'.,:;?!-_’
c.单词列表
C.单词计数字典

复制代码
s='''You were my everythingthis goes out to someone that was 
once the most important person in my life 
i didn't realize it at the time 
i can't forgive myself for the way i treated you so 
i don't really expect you to either 
it's just... i don't even know 
just listen... 
you're the one that i want, the one that i need 
the one that i gotta have just to succeed '''
s=s.lower()
for i in ',':
    s=s.replace(i,' ')
w=s.split(' ')
print(w)
dict={}
for i in w:
    dict[i]=w.count(i)
print(dict)
复制代码

posted on 2017-09-22 21:12  137陈楚洪  阅读(139)  评论(0编辑  收藏  举报

导航