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


  1. 字典实例:建立学生学号成绩字典,做增删改查遍历操作。
  2. 列表,元组,字典,集合的遍历。
    总结列表,元组,字典,集合的联系与区别。
  3. 英文词频统计实例
    • A.待分析字符串
    • B.分解提取单词
      • a.大小写 txt.lower()
      • b.分隔符'.,:;?!-_’
      • c.单词列表
    • C.单词计数字典

1.答:

a=('201506050011','201506050012','201506050013')
b=('80','90','100')
c=dict(zip(a,b))
print(c)

print('键:',c.keys())
print('值:',c.values())
print('201506050011的成绩:'+c['201506050011'])

c['201506050014']=95
print('增:',c)
c.pop('201506050014')
print('删:',c)
print('取值:'+c.get('201506050015','85'))

 

 

2.答:

s='python'
a='124455'
b=list(s)
c=tuple(s)
d=dict(zip(s,a))
e=set(a)
for i in (b,c,d,e):
    print(i)

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

列表和元组类似,列表以中括号[]表示,元组以括号()表示,列表是一种有序的序列,正向递增,反向递减序号,可以随时添加和删除其中的元素,没有长度限制,元素类型可以不同,而元组一旦初始化就不能修改。

字典是使用键值存储,具有极快的查找速度,其中的key键必须是不可变对象,可以用元素元组引入,而值可以用列表的值引入,且它们没有顺序。

集合是一组key的集合,但不存储value,在集合中不存在重复的值,且它们是无序的。

 

3.答:

  • A.待分析字符串
    s='''Beat It - Michael Jackson
    They Told Him Don't You Ever Come Around Here
    Don't Wanna See Your Face You Better Disappear
    The Fire's In Their Eyes And Their Words Are Really Clear
    So Beat It Just Beat It
    You Better Run You Better Do What You Can
    Don't Wanna See No Blood Don't Be A Macho Man
    You Wanna Be Tough Better Do What You Can
    So Beat It But You Wanna Be Bad
    Just Beat It Beat It Beat It Beat It
    No One Wants To Be Defeated
    Showin' How Funky Strong Is Your Fighter
    It Doesn't Matter Who's Wrong Or Right
    Just Beat It Beat It
    Just Beat It Beat It
    Just Beat It Beat It Just Beat It Beat It
    They're Out To Get You Better Leave While You Can
    Don't Wanna Be A Boy You Wanna Be A Man
    You Wanna Stay Alive Better Do What You Can
    So Beat It Just Beat It
    You Have To Show Them That You're Really Not Scared
    You're Playin' With Your Life This Ain't No Truth Or Dare
    They'll Kick You Then They Beat You
    Then They'll Tell You It's Fair
    So Beat It But You Wanna Be Bad
    Just Beat It Beat It Beat It Beat It
    No One Wants To Be Defeated
    Showin' How Funky Strong Is Your Fighter
    It Doesn't Matter Who's Wrong Or Right
    Just Beat It Beat It Beat It Beat It
    No One Wants To Be Defeated
    Showin' How Funky Strong Is Your Fighter
    It Doesn't Matter Who's Wrong Or Right
    Just Beat It Beat It Beat It Beat It Beat It...
    Beat It Beat It Beat It Beat It
    No One Wants To Be Defeated
    Showin' How Funky Strong Is Your Fighter
    It Doesn't Matter Who's Wrong Or Right
    Who Just Beat It Beat It Beat It Beat It
    No One Wants To Be Defeated
    Showin' How Funky Strong Is Your Fighter
    It Doesn't Matter Who's Wrong Or Who's Right
    Just Beat It Beat It Beat It Beat It
    No One Wants To Be Defeated
    Showin' How Funky Strong Is Your Fighter
    It Doesn't Matter Who's Wrong Or Right
    Just Beat It Beat It Beat It Beat It
    No One Wants To Be Defeated
    Showin' How Funky Strong Is Your Fighter
    It Doesn't Matter Who's Wrong Or Right
    Just Beat It
    -'''

    B.分解提取单词

    s=s.lower()
    for i in ',.?!-':
        s=s.replace(i,' ')
    s=s.replace('\n',' ')
    s=s.split(' ')
    print(s)

        A和B图略,参照上一篇博文。

 

  • C.单词计数字典
    a=set(s)
    for i in a:
        print(i+'出现的次数',s.count(i))

     

posted on 2017-09-22 12:18  L文斌  阅读(146)  评论(0编辑  收藏  举报