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

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

num=['087','088','089','090','091','092']
grade=[88,90,85,89,99,95]
d={'087':88,'088':90,'089':85,'090':89,'091':99,'092':95}
print(d)
d['087']
print(d['087'])
print('010' in d)
print('088' in d)
d['093']='100'
print('增加一个学号和成绩:',d)
d['090']='80'
print('更改学号为090的成绩:',d)
del(d['087'])
print('删除学号为087和对应的成绩:',d)
print(d.keys())
print(d.values())
print(d.items())
print('查询学号087:',d.get('087','不知道'))
print('遍历:')
for i in d:
    print(i,d[i])

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

list=['2','3','4','5','9','8']
tuple=('4','8','50','9','20','7')
print('遍历列表:')
for i in list:
    print(i)

print('遍历元组:')
for i in tuple:
    print(i)
  
names=['Jack','Mike','Rose','Tom']
grade=[88,95,98,87,100]
ns=dict(zip(names,grade))
print('遍历字典:')
for i in ns:
    print(i)
   
for i in ns:
    print(i,ns[i])

print('遍历集合;')
s=set(['1','2','2','3','1','2','1','3'])
print(s)
for i in s:
    print(i)

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

 答:列表:可重复,没有长度的限制,元素类型可不同,可以随时增加和删除其中的元素,python里的列表用“[]”表示。

 元组:是不可以变的。即一旦被赋值,值就不能进行增加,删除和修改,元组是用‘()’中用逗号分隔的。

 字典:是用来存储键值对数据。用大括号,每一组用冒号连起来,然后各组用逗号隔开。可以通过键来查找值。字典是无序的。

 集合:集合里面的元素不能重复,通过一个set函数转换成集合。集合是无序的,所以不支持 索引, 分片。

 

3、英文词频统计实例

  1. 待分析字符串
  2. 分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符'.,:;?!-_’
    3. 单词列表
  3. 单词计数字典
    lo='''passion is sweet,love makes weak,you siad you sherished ferrdom
    so you refuse to let it go follow your fate,love and hate.
    never fail to seize the day but dont give yourself away.
    oh when the night falls,are you all alone?
    in your deepest sleep what are you dreaming of
    my skin's still burning from your touch
    oh i just can't get enough i,said i wouldn't ask for much
    but your eyes are dangerousoh the thought keep spinning in my head
    can we dorp this masquerade,i can't predict where it ends
    if your the rock i'll crush against.trapped in a crowd.the music is loud
    i said i love my ferrdom to now i'm not sure i do all eyes on you!
    rings so ture,better quit while you're ahead now i'm not so ture i am
    my soul my heart.if you're near if you're far
    my life my love.you can have it all!'''
    print('大写转小写:')
    s=lo.lower()
    print(s)
    for i in ',.?!':
        s=s.replace(i,' ')
    print('用空格代替,.?!:',s)
    a=s.split(' ')
    print('分隔结果:',a)
    dic={}
    for i in a:
        dic[i]= lo.count(i)
    mu=list(dic.items())
    print('单词计数字典:',mu,'\n')

转载于:https://www.cnblogs.com/lqx12/p/7596042.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值