python3 字典相关函数
python版本3.5
#Author by Liguangbo
#_*_ coding:utf-8 _*_
'''
info={'No.1':'ligb','No.2':'donglx','No.3':'buzd'}
print(info['No.1'])
#修改
info['No.1']='李广博'
print(info['No.1'])
#增加
info['No.4']='hehe'
print(info)
#删除
del info['No.4']
print(info)
#标准删除
info.pop('No.1')
print(info)
#随机删除
info.popitem()
#查找
print(info.get('No.3'))
#判断字典中是否有某个键.返回True or False
print('No.1' in info)
print(info.keys())
print(info.values())
'''
#字典的嵌套
provice_catalog={
'北京':[
'东城区 西城区 崇文区 宣武区 朝阳区 海淀区 丰台区 房山区 通州区 顺义区 昌平区 大兴区 怀柔区 平谷区 密云县 延庆县'
],
'河北':{
'石家庄':[
'长安区',' 桥东区 ','桥西区 新华区 郊区 井陉矿区 井陉县 正定县 栾城县 行唐县 灵寿县 高邑县 深泽县 赞皇县 无极县 平山县 元氏县 赵县 辛集市 藁城市 晋州市 新乐市 鹿泉市'
]
},
'天津':[
'和平区 河东区 河西区 南开区 河北区 红桥区 塘沽区 汉沽区 大港区 东丽区 西青区 津南区 北辰区 武清区 宝坻区 宁河县 静海县 蓟县'
]
}
#修改
provice_catalog['河北']['石家庄'][1]='qiaodongqu'
#增加,先在字典中搜素 天津这个值,若能取到,则返回,不能取到则添加
provice_catalog.setdefault('天津',['中国'])
provice_catalog.setdefault('上海',{'baoshan':[1,2]})
print(provice_catalog)
#合并另个字典,并将交叉的值更新
info={'No.1':'ligb','No.2':'donglx','No.3':'buzd'}
dict={
'NO.1':'Andy',
1:2,
3:5 }
info.update(dict)
print(info)
#{'No.2': 'donglx', 'No.3': 'buzd', 3: 5, 1: 2, 'No.1': 'ligb', 'NO.1': 'Andy'}
#初始化一个字典,讲一个列表转化为字典
list=[1,2,3,4,5]
x=dict.fromkeys(list,['a',{'name':'Andy'},'b'])
print(x[1][1])
#{'name': 'Andy'}
x[1][1]['name']='John'
print(x)
#{1: ['a', {'name': 'John'}, 'b'], 2: ['a', {'name': 'John'}, 'b'], 3: ['a', {'name': 'John'}, 'b'], 4: ['a', {'name': 'John'}, 'b'], 5: ['a', {'name': 'John'}, 'b']}
#字典的循环
for i in info:
print(i,info[i])
#会先把字典转换为列表,数据量大时不建议使用。
for k,v in info.items():
print(k,v)
python3 字典相关函数的更多相关文章
- python3 字典常见用法总结
python3 字典常见用法总结 Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 一.创建字典 字典由键和对应值成对组成.字典也被称作关联数组或哈希表 ...
- Python3 字典 get() 方法
Python3 字典 描述 Python 字典 get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...
- Python3 字典 fromkeys()方法
Python3 字典 描述 Python 字典 fromkeys() 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值. 语法 fromkeys()方法语法: ...
- Python3 字典 update() 方法
Python3 字典 描述 Python 字典 update() 函数把字典dict2的键/值对更新到dict里. 语法 update()方法语法: dict.update(dict2) 参数 di ...
- Python3 字典 pop() 方法
Python3 字典 描述 Python 字典 pop() 方法删除字典给定键 key 所对应的值,返回值为被删除的值.key值必须给出. 否则,返回default值. 语法 pop()方法语法: ...
- Python3 字典 clear()方法
Python3 字典 描述 Python 字典 clear() 函数用于删除字典内所有元素. 语法 clear()方法语法: dict.clear() 参数 NA. 返回值 该函数没有任何返回值. ...
- Python3 字典(map)
ayout: post title: Python3 字典(map) author: "luowentaoaa" catalog: true tags: mathjax: true ...
- python系列七:Python3字典dict
#!/usr/bin/python #Python3 字典#字典是支持无限极嵌套的citys={ '北京':{ '朝阳':['国贸','CBD','天阶','我爱我家','链接地产 ...
- python011 Python3 字典
Python3 字典字典是另一种可变容器模型,且可存储任意类型对象.字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如 ...
随机推荐
- NRF905 无线模块实验
---恢复内容开始--- 采用2440开发板,CON4 不仅包含了很多富余的 GPIO 引脚,还包含了一些其他 CPU 引脚, 如 AD0-AIN3, CLKOUT 等.你所看到的图中的 SPI 接口 ...
- Leetcode: Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- 17. 星际争霸之php设计模式--职责链模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- 监控web服务方法
本地监控:端口 netstat -anltup | grep 80 nmap ip -p 80 telnet ip:80 lsof -i :80|wc -l 进程 ps -ef| grep ngi ...
- nginx安装ssl
http://wiki.nginx.org/Modules#Standard_HTTP_modules 这里面带有所有基本的模块,及需要额外增加的模块 1.安装带有ssl模块的 nginx wget ...
- SQL标签
SQL标签库提供了与关系型数据库进行交互的标签. 引入语法:<%@ taglib prefix="sql" uri="http://java.sun.com/jsp ...
- mysql常用命令(1)
Mysql安装目录数据库目录/var/lib/mysql/配置文件/usr/share/mysql(mysql.server命令及配置文件)相关命令/usr/bin(mysqladmin mysqld ...
- 网页特殊符号HTML代码大全
往网页中输入特殊字符,需在html代码中加入以&开头的字母组合或以&#开头的数字.下面就是以字母或数字表示的特殊符号大全. ´ ´ © © > > µ µ ® ® &a ...
- 安装Ifconfig
1.ifconfig 2.whereis 检查 3.yum search ifconfig 4.分割线下面让我们安装 net-tools.x86_64 执行 yum -y install net-to ...
- 【转】Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If
转载地址:http://fanshuyao.iteye.com/blog/1695482 在eclipse启动tomcat时遇到超时45秒的问题: Server Tomcat v7.0 Server ...