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

Posted 琼佳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了组合数据类型练习,英文词频统计实例相关的知识,希望对你有一定的参考价值。

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

score=list(\'3231233212232313232\')
print(\'评分表:\\n\',score)

score.append(\'1\')
print(\'\\n增加一个同学的评分:\\n\',score)

score.pop(0)
print(\'\\n删除第一个的同学的评分:\\n\',score)

score[1]=0
print(\'\\n修改第二个同学的评分为0:\\n\',score)

print(\'\\n查找第一个3分的下标:{}\'.format(score.index(\'3\')))
print(\'1分的人数:{}个\'.format(score.count(\'1\')))
print(\'3分的人数:{}个\'.format(score.count(\'3\')))

 运行结果:

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

dic={\'小白\':100,\'小红\':80,\'小粉\':90,\'小紫\':70}
print(\'学生成绩字典:\\n\',dic)

dic[\'小黑\']=60
print(\'\\n增加一个学生小黑及他的成绩:\\n\',dic)

dic.pop(\'小紫\')
print(\'\\n删除学生小紫:\\n\',dic)

dic[\'小红\']=87
print(\'\\n修改学生小红的成绩:\\n\',dic)

print(\'\\n查找学生小白的成绩:\',dic.get(\'小白\'))

 运行结果:

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

l=list(\'2354321333431\')#列表
y=tuple(\'2134354675898\')#元组
dic={\'1111\':\'01\',\'2222\':\'02\',\'3333\':\'03\'}#字典
s=set(l)#集合

print("列表:",l)
for i in l:
    print(i,end=\' \')

print("\\n\\n元组:",y)
for i in y:
    print(i,end=\' \')

print("\\n\\n字典:",dic)
for i in dic:
    print(i,end=\'\\t\')
    print(dic[i])
    
print("\\n集合:",s)
for i in s:
    print(i,end=\' \')

 运行结果:

区别:

  • 列表是任意对象的序列,用方方括号[ ]表示,可增删改查。
  • 元组用圆括号( )表示,元组一旦确认就不能够再更改。元组在定义过程中,字符串必须用单引号‘扩起来。
  • 集合是无序的,用大括号{ }表示。不能通过索引进行访问,集合中的元素不能重复。
  • 字典就是一个关联数组或散列表,其中包含通过关键字索引的对象,用大括号{ }表示。与集合相比,通过关键字索引,所以比集合访问方便。

4.英文词频统计实例

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

  4. 排序list.sort()
  5. 输出TOP(10)
passage=\'\'\'  An old gentleman whose eyesight was failing came to stay in a hotel room with a bottle of wine in each hand. On the wall there was a fly which he took for a nail. So the moment he hung them on, the bottles fell broken and the wine spilt all over the floor. When a waitress discovered what had happened, she showed deep sympathy for him and decided to do him a favour.
 So the next morning when he was out taking a walk in the roof garden, she hammered a nail exactly where the fly had stayed.
 Now the old man entered his room. The smell of the spilt wine reminded him of the accident. When he looked up at the wall, he found the fly was there again! He walked to it carefully adn slapped it with all his strength. On hearing a loud cry, the kind-hearted waitress rushed in. To her great surprise, the poor old man was there sitting on the floor, his teeth clenched and his right hand bleeding!\'\'\'
print(\'短文:\\n\',passage)

passage=passage.upper()
print(\'\\n变为大写:\\n\',passage)
passage=passage.lower()
print(\'\\n变为小写:\\n\',passage)

for i in \',.!\':
     passage=passage.replace(i,\' \')
print(\'\\n所有符号换成空格:\\n\',passage)

words=passage.split(\' \')
keys=set(words)
dic={}

exc={\'an\',\'a\',\'to\',\'in\',\'on\',\'of\',\'at\',\'was\',\'and\',\'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(\'\\n排序输出TOP10:\')
for i in range(10):
     print(wc[i])

 运行结果:

以上是关于组合数据类型练习,英文词频统计实例的主要内容,如果未能解决你的问题,请参考以下文章

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

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

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

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

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

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