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

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

>>> ls=list('123123123')
>>> ls
['1', '2', '3', '1', '2', '3', '1', '2', '3']
>>> ls.append ('3')
>>> ls
['1', '2', '3', '1', '2', '3', '1', '2', '3', '3']
>>> ls.pop(5)
'3'
>>> ls
['1', '2', '3', '1', '2', '1', '2', '3', '3']
>>> ls[1]='3'
>>> ls
['1', '3', '3', '1', '2', '1', '2', '3', '3']
>>> ls.index ('3')
1
>>> ls.count ('1')
3
>>> ls.count ('3')
4
>>> 

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

>>> ls={'1':'90','2':'67','3':'79','4':'89'}
>>> ls['1']
'90'
>>> ls['5']=99
>>> ls
{'1': '90', '2': '67', '3': '79', '4': '89', '5': 99}
>>> ls.pop('1')
'90'
>>> ls['2']='88'
>>> ls
{'2': '88', '3': '79', '4': '89', '5': 99}
>>> ls.keys ()
dict_keys(['2', '3', '4', '5'])
>>> ls.values ()
dict_values(['88', '79', '89', 99])
>>> 
>>> 

3.列表,元组,字典,集合的遍历。

>>> a=list('123123213')
>>> b=tuple('456456465')
>>> c={'1':'45','2':'67','3':'56'}
>>> d={'10','11','12','13'}
>>> for i in a:
	print(i,end=' ')

	
1 2 3 1 2 3 2 1 3 
>>> for i in b:
	print(i,end=' ')

	
4 5 6 4 5 6 4 6 5 
>>> for i in c:
	print(i,end=' ')

	
1 2 3 
>>> for i in d:
	print(i,end=' ')

	
13 12 11 10 
>>> for i in c:
	print(i,c.values ())

	
1 dict_values(['45', '67', '56'])
2 dict_values(['45', '67', '56'])
3 dict_values(['45', '67', '56'])

4.英文词频统计实例

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

  4. 排序list.sort()
  5. 输出TOP(10)
news='''See You Again - Wiz Khalifa
Everything I went through
and that line is what
We reached so remember me when I'm gone
How could we not talk about family
when family's all that we got?
Everything I went through you were
standing there by my side
And now you gon' be with me for the last ride
Let the light guide your way hold every memory
As you go and every road you take will always
lead you home
Hoo
It's been a long day without you my friend
And I'll tell you all about it when I see you again
We've come a long way from where we began
Oh I'll tell you all about it when I see you again
When I see you again
Again
When I see you again see you again
When I see you again'''
print('歌曲','see you again')

news=news.lower()

for i in ',.`/?':
     news=news.replace(i,' ')
print('符号换成空格:',news)
words=news.split(' ')

dic={}
keys=set(words)

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):
     print(wc[i])

  

posted @ 2017-09-21 17:53  405陈素欣  阅读(102)  评论(0编辑  收藏  举报