博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
字典集合
阅读量:4658 次
发布时间:2019-06-09

本文共 4270 字,大约阅读时间需要 14 分钟。


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

================ 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

你可能感兴趣的文章
Spring cloud 基础
查看>>
游戏开发Unity渲染场景光照性能优化 ShaderLOD
查看>>
java中构造方法的使用
查看>>
使用Expression动态创建lambda表达式
查看>>
MapReduce
查看>>
找工作——JVM内存管理
查看>>
【Flask】在Flask中使用logger
查看>>
好系统重装助手教你如何让win10系统快速开机
查看>>
linux开机启动
查看>>
BZOJ 1101 [POI2007]Zap 【莫比乌斯反演】
查看>>
SQL Server-The target principal name is incorrect. Cannot generate SSPI context
查看>>
AS3全局与局部坐标转换
查看>>
Java内部类详解
查看>>
初识Twisted(一)
查看>>
linux 软件安装篇
查看>>
Sql server数据库大小写敏感设置
查看>>
JAVA多线程-内存模型、三大特性、线程池
查看>>
RxJS速成 (下)
查看>>
无锁栈与无锁队列
查看>>
微信开发第8章 通过accesstoken将长连接转换为短链接
查看>>