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

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

s=list('321232331111')
print('作业评分列表',s)
s.append('3')
print('增加',s)
s.pop()
print('删除最后一个',s)
s[3]='3'
print('将第三个改为3',s)
print('第一次出现3分的下标',s.index('3'))
print('1分的同学有:',s.count('1'))
print('3分的同学有:',s.count('3'))

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

x={'201601':'90','201602':'99','201603':'88'}
print(x)
x['201603']=88
print('增加201603学号成绩',x)
x.pop('201603')
x['201602']=99
print('修改201602学号成绩',x['201602'])
print('查找2016学号成绩',x.get('201602','无数据'))
print(x.items())

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

l=list('1233332121')
t=tuple('654321123456')
x={'201420':'88','201721':'90','201422':'78'}
s=set('123321332122')
print("列表:",l)
for i in l:
    print(i,end=' ')
print("\n")
print("元组:",t)
for i in t:
    print(i,end=' ')
print("\n")
print("字典:",x)
for i in x:
    print(i,end='\t')
    print(x[i],end='\n')
print("集合:",)
for i in s:
    print(i,end=' ')

4

英文词频统计实例

下面的新闻是关于费德勒和纳达尔的比较

>>> new=''' Rafael Nadal and Roger Federer will set aside their long-time rivalry when they play for Europe against the rest of the world in the inaugural Laver Cup tournament which starts on Friday.Team Europe, featuring the world's top two players, are overwhelming favorites to win the three-day tournament, which is named after Australian great Rod Laver, with a squad boasting a combined 36 grand slam titles against one for Team World.The teams are captained by Bjorn Borg and John McEnroe, whose own rivalry starting in the 1970s featured a contrast in temperament and style that made their matches the kind of must-watch events that Laver Cup organizers hope to showcase."I've been watching these guys play for so many years, it's going to be a fun weekend," Borg, now 61, told reporters on Wednesday. "But make no mistake, we are here to win."Conceived by Federer and his sports management company Team 8, the tournament is the latest to join a crowded calendar and comes on the heels of the Davis Cup semi-finals last week.World number two Federer played down the impact of another event, saying matches over a short period were manageable and gave players on the two teams the chance to build camaraderie."I don't think it's too much otherwise all the players wouldn't be here," the Swiss great told a news conference."The best (players) in the world are very picky in what (tournaments) they play, which (is why it) is great they made this a priority.""(We're) looking forward to making friendships because we play together and not against each other for a change." '''
fo = open('text.txt','w')
>>> fo.write(''' Rafael Nadal and Roger Federer will set aside their long-time rivalry when they play for Europe against the rest of the world in the inaugural Laver Cup tournament which starts on Friday.Team Europe, featuring the world's top two players, are overwhelming favorites to win the three-day tournament, which is named after Australian great Rod Laver, with a squad boasting a combined 36 grand slam titles against one for Team World.The teams are captained by Bjorn Borg and John McEnroe, whose own rivalry starting in the 1970s featured a contrast in temperament and style that made their matches the kind of must-watch events that Laver Cup organizers hope to showcase."I've been watching these guys play for so many years, it's going to be a fun weekend," Borg, now 61, told reporters on Wednesday. "But make no mistake, we are here to win."Conceived by Federer and his sports management company Team 8, the tournament is the latest to join a crowded calendar and comes on the heels of the Davis Cup semi-finals last week.World number two Federer played down the impact of another event, saying matches over a short period were manageable and gave players on the two teams the chance to build camaraderie."I don't think it's too much otherwise all the players wouldn't be here," the Swiss great told a news conference."The best (players) in the world are very picky in what (tournaments) they play, which (is why it) is great they made this a priority.""(We're) looking forward to making friendships because we play together and not against each other for a change." ''')
1567
>>> fo.close()
>>> fr = open('text.txt','r')
>>> fr.read()
>>> new =open('text.txt','r').read()
>>> exc={'the', 'a', 'to', 'of', 'and', 'in','that','on',''}
>>> new = new.lower()
>>> new
' rafael nadal and roger federer will set aside their long-time rivalry when they play for europe against the rest of the world in the inaugural laver cup tournament which starts on friday.team europe, featuring the world\'s top two players, are overwhelming favorites to win the three-day tournament, which is named after australian great rod laver, with a squad boasting a combined 36 grand slam titles against one for team world.the teams are captained by bjorn borg and john mcenroe, whose own rivalry starting in the 1970s featured a contrast in temperament and style that made their matches the kind of must-watch events that laver cup organizers hope to showcase."i\'ve been watching these guys play for so many years, it\'s going to be a fun weekend," borg, now 61, told reporters on wednesday. "but make no mistake, we are here to win."conceived by federer and his sports management company team 8, the tournament is the latest to join a crowded calendar and comes on the heels of the davis cup semi-finals last week.world number two federer played down the impact of another event, saying matches over a short period were manageable and gave players on the two teams the chance to build camaraderie."i don\'t think it\'s too much otherwise all the players wouldn\'t be here," the swiss great told a news conference."the best (players) in the world are very picky in what (tournaments) they play, which (is why it) is great they made this a priority.""(we\'re) looking forward to making friendships because we play together and not against each other for a change." '



for i in ',.' :
    new =new .replace(i,'')
words = new.split(' ')
dic={}
>>> keys = set(words)
>>> keys
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)
SyntaxError: invalid syntax
>>> wc.sort(key=lambda x:x[1],reverse=True)

    
SyntaxError: invalid syntax
>>> for i in range(15):
    print(wc[i])

    输出
('play', 4)
('are', 4)
('for', 4)
('laver', 3)
('tournament', 3)
('great', 3)
('cup', 3)
('players', 3)
('which', 3)
('they', 3)
('is', 3)
('against', 3)
('two', 3)
('federer', 3)
('borg', 2)

 

转载于:https://www.cnblogs.com/xialuokesh/p/7569549.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值