Python字典 day2
一、字典
1、字典的特点:
一系列键-值对(key-value),字典用放在花括号{ }中的一系列键值对表示;
字典中有多个元素时需要用逗号,隔开;
key不能重复;
字典是无序的。
字典的优点:字典取值方便,速度快,比列表取值快,不需要像列表一样一个一个找。
2、字典的方法:
已存在的字典:
info = {
"name":"andy",
"color":"red",
"city":"beijing"
}
(1)增加
方法有二:
【1】向字典info中增加一个新的key:height
info['height']=160
【2】向字典info中增加一个新的key:sex
info.setdefault('sex','male')
如上两种方法在新增不存在的键时没有区别,但要是增加已存在的键,则不同:
若使用方法【1】可修改原存在的键对应的值;
若使用方法【2】则不会修改原存在的键对应的值;
(2)删除
方法有二:
【1】pop方法
info.pop('sex')
【2】del方法
del info['sex']
随机删除一个元素方法不常用,info.popitem()
(3)修改
同新增的【1】方法
info['height']=160
(4)取值
【1】输入键取对应的值
print(info['color'])
【2】使用get取值
print(info.get('color'))
如上两种方法都可以取已存在的key,若取字典里不存在的key,则不同:
【1】会报错
【2】返回None (使用此方法可设置取不到值时,传入默认值,方法为info.get('color','red'))
>获取字典里所有的key
print(info.keys())
>获取字典里所有的value
print(info.values())
(5)清空
info.clear()
(6)update与list中的extend类似,如下为将stu字典加入到info字典中
stu ={
'stu_id':'',
'stu_no':'no.1'
}
info.update(stu)
print(info)
3、多层字典取值
stu_info = {
'liudonghai':{
'house':['三环','四环','五环'],
'car':{
'china':['byd','五菱宏光','Hongqi'],
'jap':['丰田','本田'],
'Germany':{
'ben-z':2,
'bmw':3,
'audi':5
}
}
}
} #1、刘东海又买了一辆宝马
stu_info['liudonghai']['car']['Germany']['bmw']+=1
print(stu_info['liudonghai']['car']['Germany']['bmw'])
#2、刘东海不要byd和红旗了
stu_info['liudonghai']['car']['china'].remove('byd')
print(stu_info['liudonghai']['car']['china'])
stu_info['liudonghai']['car']['china'].pop(-1)
print(stu_info)
4、循环字典:
accounts = {
"liuzhao":"",
"liudonghai":"",
"zhaowenming":"",
"xiaoming":""
}
for k in accounts: #直接循环字典每次循环的时候取到的是字典的key
print("%s => %s"%(k,accounts[k])) #同时取到key和value--方法1
for k,v in accounts.items(): #同时取到key和value--方法2,没有上面的方法1效率高
print("%s => %s"%(k,v)) l = ['liuzhao', 'liudonghai', 'zhaowenming', 'xiaoming', 'users']
s = '34125345235355ssssss'
for i in l: #循环list,循环list里的每一个元素
print(i) for j in s: #循环字符串,循环字符串里的每一个元素
print(j) print(list(range(5)))
print(list(range(1,5)))
Python字典 day2的更多相关文章
- Python基础-day2
1.Python模块python 中导入模块使用import语法格式:import module_name示例1: 导入os模块system('dir')列出当前目录下的所有文件 # _*_ codi ...
- python s12 day2
python s12 day2 入门知识拾遗 http://www.cnblogs.com/wupeiqi/articles/4906230.html 基本数据类型 注:查看对象相关成员 var, ...
- python学习day2
一.模块初识 python模块 模块让你能够有逻辑地组织你的Python代码段. 把相关的代码分配到一个 模块里能让你的代码更好用,更易懂. 模块也是Python对象,具有随机的名字属性用来绑定或引用 ...
- Python字典和集合
Python字典操作与遍历: 1.http://www.cnblogs.com/rubylouvre/archive/2011/06/19/2084739.html 2.http://5iqiong. ...
- python 字典排序 关于sort()、reversed()、sorted()
一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...
- python字典中的元素类型
python字典默认的是string item={"browser " : 'webdriver.irefox()', 'url' : 'http://xxx.com'} 如果这样 ...
- python字典copy()方法
python 字典的copy()方法表面看就是深copy啊,明显独立 d = {'a':1, 'b':2} c = d.copy() print('d=%s c=%s' % (d, c)) Code1 ...
- python 字典实现类似c的switch case
#python 字典实现类似c的switch def print_hi(): print('hi') def print_hello(): print('hello') def print_goodb ...
- python字典的常用操作方法
Python字典是另一种可变容器模型(无序),且可存储任意类型对象,如字符串.数字.元组等其他容器模型.本文章主要介绍Python中字典(Dict)的详解操作方法,包含创建.访问.删除.其它操作等,需 ...
随机推荐
- QT中使用高速排序
今天想到了用QT做一个高速排序.所以研究了一下. 由于用习惯了,C++的std::sort.就算是C的时候也用得是stdlib.h中的qsort. 手写板 手写板的快排事实上不难,仅仅是自从用C++打 ...
- 《简明Python编程》核心笔记(1~5章)
2014年8月20日 <简明Python编程>核心笔记 (1~5章) 昨天和今天两天时间里.把<简明Python编程>这一本书学完了,包含书上的代码.现把核心笔记记录下来,以 ...
- 一个基于cocos2d-x 3.0和Box2d的demo小程序
p图demo小应用.想怎么p就怎么p 本文參考于http://blog.csdn.net/xiaominghimi/article/details/6776096和http://www.cnblogs ...
- hdu5242 上海邀请赛 优先队列+贪心
题意是给你一棵树 n个点 n-1条边 起点是1 每一个点都有权值 每次能从根节点走到叶子节点 经行k次游戏 每次都是从1開始 拿过的点的权值不能拿第二次 问最大权值和. 開 ...
- stl之hash_multimap
hash_multimap的元素不能自己主动排序
- bzoj5038 四叶草魔杖
很有意思的最小生成树啊. 网上的题解大多是状压+最小生成树,经过我的试验,其实只要把每个联通块找出来,一个个做一次就可以了. 放一个状压的.懒得再写一个搜索找联通块 #include<cstdi ...
- CodeForces - 811C Vladik and Memorable Trip(dp)
C. Vladik and Memorable Trip time limit per test 2 seconds memory limit per test 256 megabytes input ...
- hdu 1035(DFS)
Robot Motion Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- codeforces 899F Letters Removing set+树状数组
F. Letters Removing time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- c programs