组合数据类型和英文词频统计实例

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

复制代码
>>> ls=list('1231323232323131323')
>>> ls
['1', '2', '3', '1', '3', '2', '3', '2', '3', '2', '3', '2', '3', '1', '3', '1', '3', '2', '3']
>>> ls.append('4')
>>> ls.append('5')
>>> ls
['1', '2', '3', '1', '3', '2', '3', '2', '3', '2', '3', '2', '3', '1', '3', '1', '3', '2', '3', '4', '5']
>>> ls.pop()
'5'
>>> ls
['1', '2', '3', '1', '3', '2', '3', '2', '3', '2', '3', '2', '3', '1', '3', '1', '3', '2', '3', '4']

>>> ls.index('3')
2
>>> ls.count('1')
4
>>> ls.count('3')
9
>>> 
复制代码

 

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

复制代码
>>> d={'01':'95','02':'85','03':'90','04':'92','05':'72','06':'93'}
>>> d.keys()
dict_keys(['01', '02', '03', '04', '05', '06'])
>>> d.values()
dict_values(['95', '85', '90', '92', '72', '93'])
>>> d.get('03','100')
'90'
>>> d['99']='99'
>>> d
{'01': '95', '02': '85', '03': '90', '04': '92', '05': '72', '06': '93', '99': '99'}
复制代码

 

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

复制代码
>>> l=list('123456665432145')
>>> t=tuple('65432566123333')
>>> d={'01':'刘','02':'张','03':'王','04':'李'}
>>> s=set('123654598777296582')
>>> l
['1', '2', '3', '4', '5', '6', '6', '6', '5', '4', '3', '2', '1', '4', '5']
>>> 
>>> t
('6', '5', '4', '3', '2', '5', '6', '6', '1', '2', '3', '3', '3', '3')
>>> 
>>> d
{'01': '刘', '02': '张', '03': '王', '04': '李'}
>>> 
>>> s
{'8', '2', '7', '9', '1', '3', '6', '5', '4'}
>>> 
复制代码

列表使用方括号,元组使用小括号,字典使用花括号,集合使用[()];

元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可,列表则复杂多;

列表、字典、集合能增加、修改、删除,元组不能;

 

4.英文词频统计实例

复制代码
复制代码
new = '''An empty street An empty house 
A hole inside heart  all alone and the rooms 
Are getting smaller 
I wonder how I wonder why 
I wonder where they are 
The days we had 
The songs we sang together 
And oh! my love  holding on forever 
Reaching for a love 
That seems so far 
So I say a litter prayer 
No my dream will take me there 
Where the skies are blue to see you 
once again my love 
Overseas from coast to coast 
Find a place I love the most 
Where the fields are green 
To see you once again 
My love 
I Try to read I go to work  laughing with my friends 
But I can't stop to keep myself 
From thinking 
I wonder how I wonder why 
I wonder where they are 
The days we had 
The songs we sang togetther 
And oh! my love 
I'mholding on forever 
Reaching for a love 
That seems so far 
So I say a litter prayer 
No my dream will take me there 
Where the skies are blue to see you 
once again my love 
Overseas from coast to coast 
Find a place I love the most 
Where the fields are green 
To see you once again 
To hold you in my arms 
To promise my love 
To tell you from my heart 
You are all I'm thinking of 

Reaching for a love 
That seems so far 
So I say a litter prayer 
No my dream will take me there 
Where the skies are blue to see you 
once again my love 
Overseas from coast to coast 
Find a place I love the most 
Where the fields are green 
To see you once again 
My love 
And hope my dream will take me there 
Where the skies are blue to see you 
once again my love 
Overseas from coast to coast 
Find a place I love the most 
Where the fields are green 
To see you once again 
My love'''

new=new.lower() 
for i in '.,"':
    new=new.replace(i,' ')
word = new.split(' ') 
dic ={}
keys = set(word)
for i in keys:
    dic[i]=word.count(i)
words = list(dic.items())
words.sort(key= lambda x:x[1],reverse = True)
for i in range(10):
    print(word[i])
复制代码

posted on 2017-09-20 17:57  17黄金源 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/hjyhjy123-/p/7562855.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值