英文词频统计预备,组合数据类型练习

1实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词

 dd=dd.lower()
>>> dd
'in china, most parents keep reminding their children of the great expectation. it is natural for every parent wants their kids to stand out. but the traditional parents often make plans for their children, which is the dream that they hadn’t realized before, so they need their offspring to continue their unfinished dream. most parents haven’t realized what they do is hurting their babies, as they take away these young people’s freedom. every child has their own thought and right. as the complete individuality, they have their features and interest. parents’ expectation sometimes takes away their speciality. wise parents should not put their willingness to children, but let them make their own '
>>> for i in ',.?!':
    dd=dd.replace(i,' ')

    
>>> dd
'in china  most parents keep reminding their children of the great expectation  it is natural for every parent wants their kids to stand out  but the traditional parents often make plans for their children  which is the dream that they hadn’t realized before  so they need their offspring to continue their unfinished dream  most parents haven’t realized what they do is hurting their babies  as they take away these young people’s freedom  every child has their own thought and right  as the complete individuality  they have their features and interest  parents’ expectation sometimes takes away their speciality  wise parents should not put their willingness to children  but let them make their own '
>>> dd.count('to')
3
>>> print(dd.split(' '))
['in', 'china', '', 'most', 'parents', 'keep', 'reminding', 'their', 'children', 'of', 'the', 'great', 'expectation', '', 'it', 'is', 'natural', 'for', 'every', 'parent', 'wants', 'their', 'kids', 'to', 'stand', 'out', '', 'but', 'the', 'traditional', 'parents', 'often', 'make', 'plans', 'for', 'their', 'children', '', 'which', 'is', 'the', 'dream', 'that', 'they', 'hadn’t', 'realized', 'before', '', 'so', 'they', 'need', 'their', 'offspring', 'to', 'continue', 'their', 'unfinished', 'dream', '', 'most', 'parents', 'haven’t', 'realized', 'what', 'they', 'do', 'is', 'hurting', 'their', 'babies', '', 'as', 'they', 'take', 'away', 'these', 'young', 'people’s', 'freedom', '', 'every', 'child', 'has', 'their', 'own', 'thought', 'and', 'right', '', 'as', 'the', 'complete', 'individuality', '', 'they', 'have', 'their', 'features', 'and', 'interest', '', 'parents’', 'expectation', 'sometimes', 'takes', 'away', 'their', 'speciality', '', 'wise', 'parents', 'should', 'not', 'put', 'their', 'willingness', 'to', 'children', '', 'but', 'let', 'them', 'make', 'their', 'own', '']
>>>

2列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。

>> zz=list('123456789')
>>> zz
['1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> zz.insert(0,'9')
>>> zz
['9', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> zz.pop(0)
'9'
>>> zz.count(3)
0
>>> zz.count('3')
1
>>> zz.count('1')
1

>>> zz.index('3')
2
3简要描述列表与元组的异同

列表:序列是一种有序的序列,正向递增,反向递减序号,处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目。
可以添加,删除,或者是搜索列表中的项目。由于你可以增加或删除项目,我们说列表是可变的数据类型,即这种类型是可以被改变的。列表是可以嵌套的。
没有长度限制、元素类型可以不同。
元组:元组和列表十分相似,不过元组是不可变的。即你不能修改元组。元组通过圆括号中用逗号分隔的项目定义。
元组通常用在使语句或用户定义的函数能够安全的采用一组值的时候,即被使用的元组的值不会改变。元组可以嵌套
posted on 2017-09-20 20:18  201506050009曹艺健  阅读(119)  评论(0编辑  收藏  举报