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

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

a={'ohno':'85','sho':'100','aiba':'80','nino':'95','jun':'90'}
print('查询jun同学的分数')
print(a['jun'])

print('增加toma同学,修改nino同学的分数')
a['toma']='75'
a['nino']='96'
print(a)

print('删除toma同学的信息')
del(a['toma'])
print(a)


print('遍历字典')
for i in a:
    print(i,a[i])

结果如图:

 

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

print('字典的遍历')
a={'ohno':'85','sho':'100','aiba':'80','nino':'95','jun':'90'}
for i in a:
    print(i,a[i])

print('集合的遍历')
s=set('256312')
for i in s:
    print(i)

print('列表的遍历')
l=list('256312')
for i in l:
    print(i)
    
print('元组的遍历')
t=tuple('256312')
for i in t:
    print(i)

结果如图:


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

答:列表是可变对象,它支持在原处修改的操作,区别于元组,可动态增加,删除,更新。元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。一旦创建元组,则这个元组就不能被修改,即不能对元组进行更新、增加、删除操作。集合内的元素没有重复的元素,是一个无序不重复元素集,基本功能包括关系测试和消除重复元素.。字典存储键值对数据,字典最大的价值是查询,通过键,查找值。

 

英文词频统计实例

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

大小写 txt.lower()

分隔符'.,:;?!-_’

单词列表

单词计数字典

str='''It took a spark and my heart was energized.Electrocute like a hot live wire.
I looked away cause her eyes will hypnotize.An attitude that can start a fire.
Baby your love's a phenomena.Tower of love rising higher higher.Gotta g-get a thermometer.
Running a fever like I'm on fire.Baby it's a miracle.Something more than physical.
Flow of attraction.Refraction tonight!Running with the devil.Hot like heavy metal.Power me like a BATTERY!
I'm flying like an eagle.Sharper than a needle.Charge me up cause I need your love!
Let's get Higher Higher.Your love's like a Battery.Let's get Higher Higher.'''

print('将文章中的单词改为小写')
str=str.lower()
print(str,'\n')

print('将文章中分隔符换成空格')
for i in ',.!':
    str=str.replace(i,' ')
print(str,'\n')

print('显示单词列表')
words=str.split(' ')
print(words,'\n')

print('单词计数字典')
dic={}
for i in words:
    dic[i]=words.count(i)
d=dic.items()
print(d)

结果如图:

 

posted @ 2017-09-26 20:24  086黄向薇  阅读(171)  评论(0)    收藏  举报