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

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

时间:2017-09-26 21:20:25      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:union   ret   重复元素   对象   个数   stars   replace   只读   不同   

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

d=[001,002,003,004,005]
score=[88,75,66,98,79]
grade=dict(zip(d,score))
grade[006]=90  
print(增加一个学生:,grade)  #
grade.pop(004)    
print(删除一个学生:,grade)  #
grade[002]=78
print(修改一个学生:,grade)  #
print(查找11学生分数:,grade[001])
print(查询字典里面没有的学生:,grade.get(007,找不到该学生))
技术分享

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

list1=list(112233)
print(列表:,list1)
print(列表遍历:)
for i in list1:
    print(i)
print()

list2=tuple(645654646)
print(元组:,list2)
print(元组遍历:)
for i in list2:
    print(i)
print()

names=[Mary,Bob,Jhon]
scores=[78,85,44]
d={}
d=dict(zip(names,scores))
print(字典:,d)
print(字典遍历:)
for i in d:
    print(i,d[i])
print()

grades=[3,3,6,6,9,9]
di=set(grades)
print(集合:,di)
print(集合遍历:)
for i in di:
    print(i)
技术分享

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

列表:特点就是:可重复,类型可不同。类型不同也是跟数组最本质的区别了。python里的列表用“[]”表示。

元组:元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。元组用“()”表示

字典:字典定义了键和值之间一对一的关系,但它们是以无序的方式储存的。 Python 中的 dictionary 像 Java 中的 Hashtable 类的实例。定义 Dictionary 使用一对大(花)括号” { } “

           Dictionary 不只是用于存储字符串。Dictionary 的值可以是任意数据类型,包括字符串、整数、对象,甚至其它的 dictionary。

           在单个 dictionary 里,dictionary 的值并不需要全都是同一数据类型,可以根据需要混用和匹配。

集合: Python的集合(set)和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数               学运算.由于集合是无序的,所以,sets 不支持 索引, 分片, 或其它类序列(sequence-like)的操作。

            集合也存在不可变形式,frozenset为固定集合.

            set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。

              注意:想要创建空集合,你必须使用 set() 而不是 {} ,后者用于创建空字典

3.英文词频统计实例

q=‘‘‘When I was just a lad of ten, my father said to me,  "Come here and take a lesson from the lovely lemon tree." "Don‘t put your faith in love, my boy", my father said to me, "I fear you‘ll find that love is like the lovely lemon tree." Lemon tree very pretty and the lemon flower is sweet but the fruit of the poor lemon is impossible to eat. Lemon tree very pretty and the lemon flower is sweet but the fruit of the poor lemon is impossible to eat. One day beneath the lemon tree, my love and I did lie  A girl so sweet that when she smiled the stars rose in the sky.‘‘‘
print(‘各单词出现的次数)
q=q.lower()  # 歌词替换为小写
for i in ,.:
    q=q.replace(i, )  #替换空格为分隔符
words=q.split( )     
di = {}
words.sort()
disc = set(words)  #统计单词出现个数
for i in disc:
    di[i] = 0
for i in words:
    di[i] = di[i]+1
new = di.items()
print(new)
技术分享

 

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

标签:union   ret   重复元素   对象   个数   stars   replace   只读   不同   

原文地址:http://www.cnblogs.com/1031353319qq/p/7598025.html

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