码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-20 22:00:31      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:form   been   pre   col   color   排序   分析   分隔符   学生   

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

 1 work = list("1232122331231")
 2 print(work)
 3 
 4 work.append(3)
 5 print(work[-1])
 6 
 7 work.pop()
 8 print(work[-1])
 9 
10 work[-1] = 2
11 print(work[-1])
12 
13 print(work.index(3))
14 print(work.count(1))
15 print(work.count(3))

输出结果:

[1, 2, 3, 2, 1, 2, 2, 3, 3, 1, 2, 3, 1]
3
1
2
2
3
4

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

score = {1:44,
             2:66,
             3:78,
             4:67,
             5:88
            }
print(score)
score[6] = 98
print(score)
score[6] = 99
print(score)
score.pop(6)
print(score)

for i in score:
    print("{:>2} : {:<2}".format(i,score.get(i)))

输出结果:

{1: 44, 2: 66, 3: 78, 4: 67, 5: 88}
{1: 44, 2: 66, 3: 78, 4: 67, 5: 88, 6: 98}
{1: 44, 2: 66, 3: 78, 4: 67, 5: 88, 6: 99}
{1: 44, 2: 66, 3: 78, 4: 67, 5: 88}
 1 : 44
 2 : 66
 3 : 78
 4 : 67
 5 : 88

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

s=list(456132798)
t=set(4561327489)
c={小明:1,小红:3,小华:2}
x=(1, 2, 3, 4, 5 )
print(列表的遍历为:,end=‘‘)
for i in s:
    print(i,end=‘‘)
print()
print(元祖的遍历为:,end=‘‘)
for i in x:
    print(i,end=‘‘)
print()
print(字典key的遍历为:,end=‘‘)
for i in c:
    print(i,end=‘‘)
print()
print(字典value的遍历为:,end=‘‘)
for i in c.values():
    print(i,end=‘‘)
print()
print(数组的遍历为:,end=‘‘)
for i in x:
    print(i,end=‘‘)

输出结果:

列表的遍历为:456132798
元祖的遍历为:12345
字典key的遍历为:小明小红小华
字典value的遍历为:132
数组的遍历为:12345

4、英文词频统计实例

  1. 待分析字符串
  2. 分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符‘.,:;?!-_’
  3. 计数字典
  4. 排序list.sort()
  5. 输出TOP(10)
s=‘‘‘Well I wonder could it be When I was dreaming about you baby
You were dreaming of me Call me crazy Call me blind To still be suffering
is stupid after all of this time Did I lose my love to someone better
And does she love you like I do I do, you know I really really do
Well hey So much I need to say Been lonely since the day The day you went away
So sad but true For me there‘s only you Been crying since the day
the day you went away.!?‘‘‘
print("do出现次数",s.count(do))
s=s.replace(",","")
s=s.replace(.,‘‘)
s=s.replace(?,‘‘)
s=s.replace(!,‘‘)
s=s.replace(\n,‘‘)
s=s.lower()
s1=s.split( )
key=set(s1)
dic={}
for i in key:
    dic[i]=s.count(i)
wc=list(dic.items())
wc.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
print(wc[i])

 

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

标签:form   been   pre   col   color   排序   分析   分隔符   学生   

原文地址:http://www.cnblogs.com/xypbk/p/7563498.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!