英文词频统计预备,组合数据类型练习
2017-09-22 14:37 095罗其婷 阅读(182) 评论(0) 收藏 举报
1、实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
May you bloom and grow! Bloom and grow forever Edelweiss, edelweiss Bless my homeland forever ? Small and white Clean and bright You look happy to meet me Blossom of snow May you bloom and grow Bloom and grow forever Edelweiss, edelweiss Bless my homeland forever? Edelweiss, edelweiss, Every morning you greet me. Small and white, Clean and bright, You look happy to meet me. Blossom of snow, May you bloom and grow, Bloom and grow forever. Edelweiss, edelweiss, Bless my homeland forever. Small and white, Clean and bright, You look happy to meet me. Blossom of snow May you bloom and grow, Bloom and grow forever. Edelweiss, edelweiss, Bless my homeland forever. ''' y=y.lower() print(y) for i in ',.?!': y=y.replace(i,' ') print(y) y=y.split(' ') print(y) y=y.count('me') print(y)
every morning you greet me small and white clean and bright you look happy to meet me blossom of snow may you bloom and grow bloom and grow forever edelweiss edelweiss bless my homeland forever small and white clean and bright you look happy to meet me blossom of snow may you bloom and grow bloom and grow forever edelweiss edelweiss bless my homeland forever edelweiss edelweiss every morning you greet me small and white clean and bright you look happy to meet me blossom of snow may you bloom and grow bloom and grow forever edelweiss edelweiss bless my homeland forever small and white clean and bright you look happy to meet me blossom of snow may you bloom and grow bloom and grow forever edelweiss edelweiss bless my homeland forever ['every', 'morning', 'you', 'greet', 'me', '\nsmall', 'and', 'white', '\nclean', 'and', 'bright', '\nyou', 'look', 'happy', 'to', 'meet', 'me', '', '\nblossom', 'of', 'snow', '\nmay', 'you', 'bloom', 'and', 'grow', '', '\nbloom', 'and', 'grow', 'forever', '\nedelweiss', '', 'edelweiss', '\nbless', 'my', 'homeland', 'forever', '', '\nsmall', 'and', 'white', '\nclean', 'and', 'bright', '\nyou', 'look', 'happy', 'to', 'meet', 'me', '\nblossom', 'of', 'snow', '\nmay', 'you', 'bloom', 'and', 'grow', '\nbloom', 'and', 'grow', 'forever', '\nedelweiss', '', 'edelweiss', '\nbless', 'my', 'homeland', 'forever', '\nedelweiss', '', 'edelweiss', '\nevery', 'morning', 'you', 'greet', 'me', '\nsmall', 'and', 'white', '\nclean', 'and', 'bright', '\nyou', 'look', 'happy', 'to', 'meet', 'me', '\nblossom', 'of', 'snow', '\nmay', 'you', 'bloom', 'and', 'grow', '\nbloom', 'and', 'grow', 'forever', '\nedelweiss', '', 'edelweiss', '\nbless', 'my', 'homeland', 'forever', '\nsmall', 'and', 'white', '\nclean', 'and', 'bright', '\nyou', 'look', 'happy', 'to', 'meet', 'me', '\nblossom', 'of', 'snow\nmay', 'you', 'bloom', 'and', 'grow', '\nbloom', 'and', 'grow', 'forever', '\nedelweiss', '', 'edelweiss', '\nbless', 'my', 'homeland', 'forever', '\n'] 6 >>>
2、列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
g=list('33212231212221111') #原列表 print(g) g.append('2') print(g) g.pop(5) print(g) g.insert(5,'1') print(g) #查询第一个3分的下标 g.index('3') print(g.index('3')) #统计1分的同学人数 g.count('1') print(g.count('1')) #统计3分的同学人数 g.count('3') print(g.count('3'))
RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/j.py ['3', '3', '2', '1', '2', '2', '3', '1', '2', '1', '2', '2', '2', '1', '1', '1', '1'] ['3', '3', '2', '1', '2', '2', '3', '1', '2', '1', '2', '2', '2', '1', '1', '1', '1', '2'] ['3', '3', '2', '1', '2', '3', '1', '2', '1', '2', '2', '2', '1', '1', '1', '1', '2'] ['3', '3', '2', '1', '2', '1', '3', '1', '2', '1', '2', '2', '2', '1', '1', '1', '1', '2'] 0 8 3
3、简要描述列表与元组的异同。
异:list是处理一组有序项目的数据结构,即可以在一个列表中存储一个序列的项目。列表中的项目,一旦创建了一个列表,就可以添加,删除,或者是搜索列表中的项目。由于可以增加或删除项目,所以一般称列表是可变的,而元组与列表非常相似,但是元组是不可变的,即一旦初始化便不可以更改。
同:列表和元组都属于有序序列,支持使用双向索引访问其中的元素、使用内置函数len()统计元素个数、使用运算符in测试是否包含某个元素、使用count()方法统计指定元素的出现次数和index()方法获取指定元素的索引。
浙公网安备 33010602011771号