Python:dict用法
dict全称dictionary,使用键-值(key-value)存储,有极快的查找速度。
以下整理几种常用的dict用法
定义
空dict
>>> dict={}
普通dict
>>> dict={'adele':'hello','taylor':''}
>>> dict
{'taylor': '', 'adele': 'hello'}
嵌套
>>> a_dict={1:"{11:'a',12:'b'}",2:"2B",3:"3C"}
>>> a_dict
{1: "{11:'a',12:'b'}", 2: '2B', 3: '3C'}
>>> a_dict[1][12]
'b'
获取键、值
key&values
>>> a_dict.keys()
[1, 2, 3] #结果为list
>>> a_dict.values()
["{11:'a',12:'b'}", '2B', '3C']
items
>>> a_dict.items()
[(1, "{11:'a',12:'b'}"), (2, '2B'), (3, '3C')] #结果为list,list里面的元素是元组
for..in
>>> for key in a_dict:
... print (key)
...
1
2
3
>>> for value in a_dict.values():
... print(value)
...
{11:'a',12:'b'}
2B
3C
输出value等价语句
>>> for key in a_dict:
... print a_dict[key]
...
{11:'a',12:'b'}
2B
3C
同时输出键、值
两种方法:
1)使用两个变量k,v,完成循环
2)使用一个变量k,通过k求出对应v
>>> for k,v in a_dict.items():
... print str(k)+":"+str(v)
...
1:{11:'a',12:'b'}
2:2B
3:3C >>> for k in a_dict:
... print str(k)+":"+str(a_dict[k])
...
1:{11:'a',12:'b'}
2:2B
3:3C
另一种实现形式
>>> for k in a_dict:
... print "a_dict(%s)="%k,a_dict[k]
...
a_dict(1)= {11:'a',12:'b'}
a_dict(2)= 2B
a_dict(3)= 3C
get
>>> a_dict.get(1)
"{11:'a',12:'b'}"
删除
分别使用了三种方法:pop、del和clear
>>> a_dict.pop('taylor')
'' #根据键值删除,并返回值
>>> del a_dict[1]
>>> a_dict
{2: '2B', 3: '3C', 'adele': 'hello'}
>>> a_dict.clear()
>>> a_dict
{}
拷贝
>>> new_dict=a_dict.copy()
>>> new_dict
{1: "{11:'a',12:'b'}", 2: '2B', 3: '3C'}
合并
>>> add_dict={'adele':'hello','taylor':''}
>>> a_dict.update(add_dict)
>>> a_dict
{1: "{11:'a',12:'b'}", 2: '2B', 3: '3C', 'adele': 'hello', 'taylor': ''}
排序
按照key排序
>>> print sorted(a_dict.items(),key=lambda d:d[0])
[(1, "{11:'a',12:'b'}"), (2, '2B'), (3, '3C')]
按照value排序
>>> print sorted(a_dict.items(),key=lambda d:d[1])
[(2, '2B'), (3, '3C'), (1, "{11:'a',12:'b'}")]
后续使用中,再补充..
Python:dict用法的更多相关文章
- Python Dict用法
Operation Result len(a) the number of items in a 得到字典中元素的个数 a[k] the item of a with key k 取得键K所对应的值 ...
- Python高级用法总结
Python很棒,它有很多高级用法值得细细思索,学习使用.本文将根据日常使用,总结介绍Python的一组高级特性,包括:列表推导式.迭代器和生成器.装饰器. 列表推导(list comprehensi ...
- Python dict operation introduce
字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示: d = ...
- python argparse用法总结
转:python argparse用法总结 1. argparse介绍 argparse是python的一个命令行解析包,非常适合用来编写可读性非常好的程序. 2. 基本用法 prog.py是我在li ...
- Python dict(或对象)与json之间的互相转化
Python dict(或对象)与json之间的互相转化 原文转载自 1.JSON:JavaScript 对象表示法,是轻量级的文本数据交换格式,独立于语言,平台 2.JSON 语法规则 数据在名称/ ...
- Anaconda下载及安装及查看安装的Python库用法
Anaconda下载及安装及查看安装的Python库用法 Anaconda 是一个用于科学计算的 Python 发行版,提供了包管理与环境管理的功能.Anaconda 利用 conda 来进行 pac ...
- python enumerate用法总结【转】
enumerate()说明 enumerate()是python的内置函数 enumerate在字典上是枚举.列举的意思 对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enum ...
- python & dict & switch
python & dict & switch python 中是没用switch语句的,这应该是体现python大道至简的思想,python中一般多用字典来代替switch来实现. # ...
- Python dict() 函数
Python dict() 函数 Python 内置函数 描述 dict() 函数用于创建一个字典. 语法 dict 语法: class dict(**kwarg) class dict(mappi ...
随机推荐
- 如何用Java代码列出一个目录下所有的文件?
目录文件夹 File file=new File("H:\\"); for(File temp:file.listFiles()){//Java5的新特性之一就是增强的for循环. ...
- spring中用到哪些设计模式
1.工厂模式,这个很明显,在各种BeanFactory以及ApplicationContext创建中都用到了: 2.模版模式,这个也很明显,在各种BeanFactory以及ApplicationCon ...
- oracle dblink的创建方式
操作语句: create database link zfxtdblink connect to tianhe2014 identified by tianhe2014 using '199.169. ...
- http常见状态码解析
自定义 Ajax原生编写ajax:function(opt){ var xmlhttp; //创建对象 if (window.XMLHttpRequest){// code for IE7+, Fir ...
- html(二)
1无序列表 ul是没有前后顺序的信息列表. <ul> <li></li> <li></li> ...... </ul> ul在网 ...
- oracle游标透彻详解分析
主:本文来自:http://www.cnblogs.com/huyong/archive/2011/05/04/2036377.html 4.1 游标概念 4.1.1 处理显式游标 4.1.2 处理隐 ...
- NPY and girls-HDU5145莫队算法
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...
- js date 火狐不兼容 解决办法 火狐版本25,0
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- Tips collection of iOS development
<转>UITableView当数据很少的时候,去掉多余的cell分割线 在tableView初始化的时候 UIView *v = [[UIViewalloc] initWithFram ...
- JS作用域及call
<script type="text/javascript"> function log(val){ console.log(val); } function base ...