字典集合


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


================ RESTART: C:/Users/Administrator/Desktop/1.py ================
>>> d={'hello':88,'insert':12,'input':45,'open':56}
>>> d['open']
56
>>> d.pop('insert')
12
>>> d.keys()
dict_keys(['hello', 'input', 'open'])
>>> d.values()
dict_values([88, 45, 56])
>>> d.items()
dict_items([('hello', 88), ('input', 45), ('open', 56)])
>>> d.get('hi',' ')
' '
>>> d.get('jis','nius')
'nius'
>>> d.get('open',' ')
56
>>> del(d['hello'])
>>> d
{'input': 45, 'open': 56}
>>> for i in d:
print(i,d[i])


input 45
open 56
>>>

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


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

列表:list是一种有序的序列,正向递增、反向递减序号;可以随时添加和删除其中的元素;没有长度限制,元素类型可以不同。
元组:tuple和list非常类似;都是内置的集合;tuple一旦初始化就不能修改。
字典:是用空间来换取时间的一种方法。
和list比较;特点是:查找和插入的速度极快,不会随着key的增加而变慢,需要占用大量的内存,内存浪费多。
和list相反;查找和插入的时间随着元素的增加而增加;占用空间小,浪费内存很少。
集合:也是一组key的集合,但是不存储values;没有重复的key;创建一个set,需要提供一个list做为输入集合;set 是无序的。

英文词频统计实例

Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> sing='''Hello, it's me
I was wondering if after all these years you'd like to meet
To go over everything
They say that time's supposed to heal ya
But I ain't done much healing
Hello, can you hear me?
I'm in California dreaming about who we used to be
When we were younger and free
I've forgotten how it felt
Before the world fell at our feet
There's such a difference between us
And a million miles

Hello from the other side
I must have called a thousand times
To tell you I'm sorry for everything that I've done
But when I call, you never seem to be home
Hello from the outside
At least I can say that I've tried
To tell you I'm sorry for breaking your heart
But it don't matter, it clearly doesn't tear you apart
Anymore

Hello, how are you?
It's so typical of me to talk about myself, I'm sorry
I hope that you're well
Did you ever make it out of that town
Where nothing ever happened?
It's no secret that the both of us
Are running out of time

So hello from the other side (other side)
I must have called a thousand times (thousand times)
To tell you I'm sorry for everything that I've done
But when I call, you never seem to be home
Hello from the outside (outside)
At least I can say that I've tried (I've tried)
To tell you I'm sorry for breaking your heart
But it don't matter, it clearly doesn't tear you apart
Anymore

Ooh, anymore
Ooh, anymore
Ooh, anymore
Anymore

So hello from the other side (other side)
I must have called a thousand times (thousand times)
To tell you I'm sorry for everything that I've done
But when I call, you never seem to be home
Hello from the outside (outside)
At least I can say that I've tried (I've tried)
To tell you I'm sorry for breaking your heart
But it don't matter, it clearly doesn't tear you apart
Anymore'''
>>> s=sing.lower()
>>> for i in ',.?/!':
s=sing.replace(i,' ')


>>> s=sing.split(' ')
>>> words=set(s)
>>> d={}
>>> d['we']=s.count('we')
>>> for i in words:
d[i]=s.count(i)


>>> for i in d:
print('{0:<10}{1}'.format(i,d[i]))


we 2
used 1
sorry 6
I've 6
must 3
times
To 1
tried
To 1
miles

Hello1
hello 2
ain't 1
to 7
outside
At1
go 1
(other 2
thousand 3
least 3
talk 1
supposed 1
I 7
that 10
dreaming 1
so 1
apart
Anymore1
sorry
I 1
when 3
you're 1
feet
There's1
meet
To 1
clearly 3
us
And 1
was 1
like 1
much 1
it 8
(thousand 2
called 3
home
Hello3
times)
To 2
tried 2
times 2
everything
They1
time

So 1
younger 1
our 1
side)
I 2
us
Are 1
you'd 1
free
I've 1
who 1
I'm 7
both 1
never 3
these 1
forgotten 1
me
I 1
heal 1
all 1
wondering 1
world 1
everything3
running 1
(outside)
At2
you?
It's 1
side
I 1
don't 3
(I've 2
apart
Anymore

Hello,1
secret 1
years 1
between 1
the 8
Hello, 1
me?
I'm 1
after 1
for 6
side 2
myself, 1
were 1
California1
doesn't 3
done 1
can 4
ever 2
typical 1
out 2
such 1
anymore
Anymore

So1
outside 2
over 1
heart
But 3
difference1
million 1
a 5
breaking 3
are 1
done
But 3
of 4
well
Did 1
healing
Hello,1
other 3
it's 1
at 1
ya
But 1
tell 6
hear 1
call, 3
town
Where1
say 4
from 6
fell 1
nothing 1
and 1
felt
Before1
be 3
have 3
about 2
hope 1
no 1
tried)
To 2
your 3
make 1
seem 3
anymore
Ooh,2
be
When 1
me 1
how 2
you 14
in 1
apart
Anymore

Ooh,1
if 1
matter, 3
time's 1
happened?
It's1
tear 3
>>>

 

转载于:https://www.cnblogs.com/zhangmuqing/p/7573329.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值