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

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

score = list('012332211')
print('分数为:',score)
print('1分的同学个数:',score.count('1'))
print('3分的同学个数:',score.count('3'))
print('第一个3分同学的下标为:',score.index('3'))
score.append('0')
print('添加一个0分在表的末尾:',score)
score.insert(0,'3')
print('添加一个3分在表的开头:',score)
score.pop(1)
print('删除第二个分数:',score)

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

d={'':95,'':86,'':75,'':84,'':96}
print('学生成绩字典',d)
d['']=87
print('增加一个学生')
print(d)
d.pop('')
print('删除学生孙')
print(d)
d['']=97
print('修改学生吴的成绩')
print(d)
print('查找学生赵的成绩:',d.get(''))

3、分别做列表,元组,字典,集合的遍历,并总结列表,元组,字典,集合的联系与区别

 ls = list('123456789')
  ln = tuple('123456789')
  s = {'01':'100','02':'99','03':'98','04':'97','05':'96','05':'96','06':'95','07':'98','08':'90','09':'91'}
  a= set('123456789')
  print('列表:',ls)
  print('元组',ln)
  print('字典',s)
  print('集合',a)
  print('\n列表的遍历:')
 for i in ls:
     print(i,' ',end='  ')
 print('\n元组的遍历:')
 for j in ln:
     print(j,' ',end='  ')
 print('\n字典的遍历:')    
 for k in s:
     print(k,'',end='  ')
 print('\n集合的遍历:')    
 for l in a:
     print(l,'',end='  ')

4、英文词频统计实例

待分析字符串分解提取单词

  1. 待分析字符串
  2. 分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符'.,:;?!-_’
  3. 计数字典
    1. 排除语法型词汇,代词、冠词、连词

  4. 排序list.sort()
  5. 输出TOP(10)
news='''It's normal for fast food brands to poke fun at their competitors,
but Panera Bread's latest social media campaign isn't cracking jokes.
It's a call for executives everywhere to put their kiddie meals where their
mouth is.
On Wednesday, Panera Bread's founder and CEO, Ron Shaich, launched
the #KidsMenuChallenge, a social media campaign that challenges other fast food
executives to spend a week eating the food that's served on their own children's
menus.'''
print('新闻:',news)

news=news.lower()#小写
print('小写:',news)

for i in ',.':#将符号替换成空格
     news=news.replace(i,' ')
print('符号换成空格:',news)

words=news.split(' ')#用空格分解每一个单词

dic={}#字典
keys=set(words)#出现的所有单词的集合,字典的key
exc={"a","to","is","at","the",''}#没有意义的、不要的单词
for w in exc:#去掉没有意义的单词
     keys.remove(w)
for i in keys:#记录次数
    dic[i]=words.count(i)
wc=list(dic.items())#列表
wc.sort(key=lambda x:x[1],reverse=True)
print('记录次数:')
for i in range(10):#显示10个单词及其次数
     print(wc[i])

5、文本操作

fo=open('/Users/Administrator/Desktop/test.txt','r')
news=fo.read()
fo.close()
exc={'the','a','to','of','and','on','in','that'}
news =news.lower()
for i in ''',.?!"''':
    news=news.replace(i,' ')

print(news)
words=news.split(' ')
print(words)
d={}
keys = set(words)
for r in exc:
    keys.remove(r)
for i in keys:
    d[i]=words.count(i)
wc=list(d.items())
wc.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
    print(wc[i])

 

posted @ 2017-09-21 18:06  23许捷凯  阅读(136)  评论(0编辑  收藏  举报