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

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

复制代码
d={'小红':'60','小明':'72','小东':'98','小华':'88','小强':'91'}

print(d.get('小红'))
d.pop('小红')
print(d.items())
d['小强']=81
print(d.items())
for i in d:
    print(i)
复制代码

结果如下图所示:

 

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

复制代码
list=['1','2','3','4','5','6']
tu=('01','02','03','04','05','06')
dic={'01':'88','02':'99','03':'77','04':'86','05':'96','06':'98'}
jihe=set(list)

for i in list:
    print(i)
for i in tu:
    print(i)
for i in dic:
    print(i,dic[i])
for i in jihe:
    print(i)
复制代码

结果如下图所示:

 

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

       元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。集合就是我们数学学的集合,没有什么特殊的定义。集合最好的应用是去重。集合没有特殊的表示方法,而是通过一个set函数转换成集合。最后一个是字典,字典存储键值对数据,字典最外面用大括号,每一组用冒号连起来,然后各组用逗号隔开,字典最大的价值是查询,通过键,查找值。

4)英文词频统计实例

  1. (1)待分析字符串
  2. (2)分解提取单词
  3. 大小写 txt.lower()
  4. 分隔符'.,:;?!-_’
  5. 单词列表
  6. (3)单词计数字典
  7. 复制代码
    A='''The very thought of you  leaving my life
    Broke me down in tears.
    I took for granted all the love
    That you gave to me
    I know that's what I feared
    Don't go away.
    Every heart beat
    Every moment
    Everything I see is you
    Please forgive me.
    I'm so sorry
    Don't say we're through
    Girl I need you  to be by my side
    Girl I need you  to open up my eyes
    Cause without you  where would I be
    Yes I need you
    Come back to me
    A kiss is not a kiss
    Without your lips kissing mine
    You bring me paradise
    I can't live if you took your love
    Away from me
    Without you I would die.
    Every second
    Every minute
    Every time I close my eyes.
    I could feel you
    So I want you
    To stay in my life
    Girl I need you  to be by my side
    Girl I need you  to open up my eyes
    Cause without you  where would I be
    Yes I need you
    Come back to me
    Every second,
    Every minute,
    Every time I close my eyes,
    I could feel you.
    So I want you
    To always be mine
    Girl I need you  to be by my side
    Girl I need you  to open up my eyes
    Cause without you  where would I be
    Yes I need you  come back to me
    Girl I need you  to be by my side
    Girl I need you  to open up my eyes
    Cause without you  where would I be
    Yes I need you  come back to me
    Girl I need you  to be by my side
    Girl I need you  to open up my eyes
    Cause without you  where would I be
    Yes I need you  come back to me'''
    
    A=A.lower()
    for i in ',.!':
        A=A.replace(i,' ')
    words=A.split(' ')
    a=set(words)
    print(words)
    
    dic={}
    for i in a:
        dic[i]=0
    for i in words:
        dic[i]=dic[i]+1
    print(dic)
    复制代码

    结果如下图所示:

posted on 2017-09-22 20:51  133陈贝  阅读(172)  评论(0编辑  收藏  举报