第3次作业(组合数据类型,英文词频统计 )

1.练习:

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

      列表是可变的对象,可进行动态的增加、删除、更新,用[]表示。

      元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。元组用“()”表示。

      字典是存储键值对数据的对象,字典的元素都是无序的,且键不能相同,可以通过键,找到值,字典最外面用大括号,每一组用冒号连接起来。

      集合相当于字典的键,也是一个无序不重复的元素集,用一对{}表示。

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

       列表是可变序列,元组是不可变序列;列表的值可以修改,而元祖的值初始化后不可修改,两者都是有序的。字典和集合 两者都是无序的,数据量大时可用集合或列表来创建。

a = list('hello world!')      #列表的遍历
print(a)
for i in a:
    print(i)

b = tuple('344578655')       #元组的遍历
print(b)
for i in b:
    print(i)

c = set('lkfsfdr')        #集合的遍历
print(c)
for i in c:
    print(i)

d = {'A':45,'B':99,'D':68}        #字典的遍历
print(d)
for i in d:
    print(i,d[i])

  

2.英文词频统计:

  • 下载一首英文的歌词或文章str
  • 分隔出一个一个的单词 list
  • 统计每个单词出现的次数 dict
musicstr = '''I'v been brokehearted 
ever since the day we partied
I didn't know where to turn to
ever since you went away
I goin' in circles
wander if really hurts you
I know i don't deserve it
But I'm dealing with it everyday
I was so surprised
I didn't know what to say
But i forgave you
for throwing all away
I've been hopin' you come back to me
I forgive you..... for everything
I've been crying about everything
Now I won't be satisfied till you come back to me
It really don't make sence
i've been trying to figure out why you left
I've picking up the pieces
now I wish you're here with me
I'v been brokehearted
ever since the day we partied
I don't have much to say
Haven't seen you since you've gone away
I've been hopin' you come back to me
I forgive you for everything
I've been crying about everything
Now I won't be satisfied till you come back to me
wanna be stay
come back to me
Cause I know I don't wanna be without you, baby
I've been hopin' you come back to me
I forgive you for everything
I've been crying about everything
Now I won't be satisfied till you come back to me
wanna be stay
come back to me
I won't be satisfied till you come back to me'''
musicstr = musicstr.lower()
sep = ''',.:!?'"'''
for c in sep:
    musicstr = musicstr.replace(c,' ')
musicList= musicstr.split()

print(musicstr)
print(musicList)
dd=set(musicList)
print(dd)
for word in dd:
    print(musicList.count(word))

  

转载于:https://www.cnblogs.com/LLABC/p/9753247.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值