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

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

n=['001','003','005','007','018','056','094','100']
f=[23,43,56,65,76,83,54,90]
g=dict(zip(n,f))
print(g)
#
g['080']=95
print(g)

#
g.pop('003')
print(g)

#
g['007']=100
print(g)

#查询
g['018']
print(g)

g.get('090')
print(g.get('090','不知道'))

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

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

l=list('5051205147')
print('列表遍历:',l)
for i in l:
    print(i)
      
s=tuple('4505684620')
print('元组遍历:',s)
for a in s:
    print(a)

d={'张三':10,'李四':50,'王五':80,'陈六':100}
print('字典遍历:',d)
for b in d:
    print(b,d[b])

j=set('1875630598')
print('集合遍历:',j)
for c in j:
    print(c)

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

(1)列表是任意对象的序列。列表用方括号表示。
(2)将一组值打包到一个对象中,称为元组。元组用圆括号表示。元组和列表的大部分操作相同。但是,列表是不固定的,可以随时插入,删除;而元组一旦确认就不能够再更改。所以,系统为了列表的灵活性,就需要牺牲掉一些内存;而元组就更为紧凑。
(3)与列表和元组不同,集合是无序的,也不能通过索引进行访问。此外,集合中的元素不能重复。
(4)字典就是一个关联数组或散列表,其中包含通过关键字索引的对象。用大括号表示。与集合相比,通过关键字索引,所以比集合访问方便。字典是Python解释器中最完善的数据类型。

3.英文词频统计实例

  1. 待分析字符串
  2. 分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符'.,:;?!-_’
    3. 单词列表
  3. 单词计数字典
s='''First you both go out your way

And the vibe is feeling strong and what's small turn to a friendship

Turn into a bond and that bond will never be broken and the love will never get lost

And when brotherhood come first then the line

Will never be crossed established it on our own

When that line had to be drawn and that line is what?

We reach so remember me when I'm gone?

How could we not talk about family when family's all that we got?

Everything I went through you were standing there by my side

And now you gonna be with me for the last ride

So let the light guide your way

Hold every memory as you go

And every road you take will always lead you home

Hoo?

It's been a long day without you my friend

And I'll tell you all about it when I see you again

We've come a long way from where we began?

Oh I'll tell you all about it when I see you again

When I see you again

When I see you again, see you again

When I see you again'''
s=s.lower()
print(s)

a=s.replace('?',' ')
print(a)

w=s.split(' ')
print(w)

dic={}
for i in w:
    dic[i]=s.count(i)
s=list(dic.items())
print('单词计数:',s,'
')

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