字典,即我们可以通过索引来查找更详细的内容。每个item都是由一对index:value构成的

  • 索引可以有副本,但是试图根据存在副本的索引访问元素时,只会取最靠后的那个。
  • 索引必须是immutable的,意味着可以用string, number,tuple都可以,但是list不行
  1. #创建字典
  2. >>> dicA = { 'Num':1 , 'Grade':98 , 'Sch':yes }
  3. #访问元素
  4. >>> print dicA['Sch'], dicA['Num']
  5. yes 1
  6. #添加元素
  7. >>> dicB = {'When':'Today' , 'Affair':'Meeting' , 'Who':'Tom'}
  8. >>> dicB['WithWho'] = 'Sunny'
  9. >>> print dicB
  10. {'Affair': 'Meeting', 'Who': 'Tom', 'When': 'Today', 'WithWho': 'Sunny'}
  11. #更新元素
  12. >>> dicB['Who'] = 'XJ'
  13. >>> print dicB
  14. {'Affair': 'Meeting', 'Who': 'XJ', 'When': 'Today', 'WithWho': 'Sunny'}
  15. #删除元素
  16. >>> dicB = {'When':'Today' , 'Affair':Meeting , 'Who':Tom}
  17. >>> del dicB['When']
  18. >>> print dicB
  19. {'Affair': 'Meeting', 'Who': 'Tom'}
  20. #清空元素
  21. >>> dicB.clear()
  22. >>> print dicB
  23. {}
  24. #删除整个字典
  25. >>> del dicB
  26. >>> print dicB
  27. Traceback (most recent call last):
  28. File "<stdin>", line 1, in <module>
  29. NameError: name 'dicB' is not defined

Built-in Functions

cmp(dict1, dict2)

Compares elements of both dict.Returns 0 if both dictionaries are equal, -1 if dict1 < dict2 and 1 if dict1 > dic2.

  1. >>> dicA,dicB
  2. ({'Num': 1, 'Sch': 'yes'}, {'Num': 2, 'Sch': 'yes'})
  3. >>> del dicB['Num']
  4. >>> cmp(dicA,dicB)
  5. 1
  6. >>> dicA,dicB
  7. ({'Num': 1, 'Sch': 'yes'}, {'Sch': 'yes'})
  8. >>> dicB['Num']=1
  9. >>> cmp(dicA,dicB)
  10. 0

len(dict)

Gives the total length of the dictionary. This would be equal to the number of items in the dictionary.

  1. >>> len(dicA)
  2. 2

str(dict)

Produces a printable string representation of a dictionary

  1. >>> str(dicA)
  2. "{'Num': 1, 'Sch': 'yes'}"
  3. >>> print str(dicA)
  4. {'Num': 1, 'Sch': 'yes'}

type(variable)

Returns the type of the passed variable. If passed variable is dictionary, then it would return a dictionary type

  1. >>> print "%s" %type(dicA)
  2. <type 'dict'>

Built-in Methods

dict.clear()

Removes all elements of dictionary dict

  1. >>> dicB.clear()
  2. >>> print dicB
  3. {}

dict.copy()

Returns a shallow copy of dictionary dict. VS DicA = DicB means these two dictionaries share the same address, one is modified automaticaly with the other is changed

  1. >>> dicB=dict.copy(dicA) #或dicB.copy(dicA)
  2. >>> dicB['Num']=2
  3. >>> dicA,dicB
  4. ({'Num': 1, 'Sch': 'yes'}, {'Num': 2, 'Sch': 'yes'})

dict.fromkeys()

Create a new dictionary with keys from seq and values set to value.

  1. >>> dicE = {}
  2. >>> dicE
  3. {}
  4. >>> seq = ('Who','When','What')
  5. >>> dicE = dict.fromkeys(seq) #或dicE.fromkeys(seq)
  6. >>> dicE
  7. {'What': None, 'Who': None, 'When': None}
  8. >>> dicE = dict.fromkeys(seq,'XXX')
  9. >>> dicE
  10. {'What': 'XXX', 'Who': 'XXX', 'When': 'XXX'}

dict.get(key, default=None)

For key key, returns value or default if key not in dictionary

  1. >>> dict.get(dicE,'Which') #或dicE.get('Which')
  2. >>> dict.get(dicE,'Who')
  3. 'XXX'

dict.has_key(key)

Returns true if key in dictionary dict, false otherwise

  1. >>> dict.has_key(dicE,'Who') #或dicE.has_key('Who')
  2. True

dict.items()

Returns a list of dict's (key, value) tuple pairs

  1. >>> dict.items(dicE) #或dictE.items()
  2. [('What', 'XXX'), ('Who', 'XXX'), ('When', 'XXX')]

dict.keys()

Returns list of dictionary dict's keys

  1. >>> dict.keys(dicE) #或dicE.keys()
  2. ['What', 'Who', 'When']

dict.setdefault(key, default=None)

Similar to get(), but will set dict[key]=default if key is not already in dict

  1. >>> dict.get(dicE,'Which') #或dicE.get('Which')
  2. >>> dict.setdefault(dicE,'Which')
  3. >>> dicE
  4. {'What': 'XXX', 'Who': 'XXX', 'When': 'XXX', 'Which': None}

dict.update(dict2)

Adds dictionary dict2's key-values pairs to dict

  1. >>> dicF = {'Work':'No'}
  2. >>> dicE
  3. {'Num': 1, 'Sch': 'yes'}
  4. >>> dicE.update(dicF)
  5. >>> dicE
  6. {'Num': 1, 'Sch': 'yes', 'Work': 'No'}

dict.values()

Returns list of dictionary dict's values

  1. >>> dicE.values()
  2. [1, 'yes', 'No']

字典dictionary的更多相关文章

  1. C#创建安全的字典(Dictionary)存储结构

    在上面介绍过栈(Stack)的存储结构,接下来介绍另一种存储结构字典(Dictionary). 字典(Dictionary)里面的每一个元素都是一个键值对(由二个元素组成:键和值) 键必须是唯一的,而 ...

  2. 索引器、哈希表Hashtabl、字典Dictionary(转)

    一.索引器 索引器类似于属性,不同之处在于它们的get访问器采用参数.要声明类或结构上的索引器,使用this关键字. 示例:   索引器示例代码 /// <summary> /// 存储星 ...

  3. Python 字典(Dictionary)操作详解

    Python 字典(Dictionary)的详细操作方法. Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 一.创建字典 字典由键和对应值成对组成.字 ...

  4. Python 字典(Dictionary) get()方法

    描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...

  5. Python字典 (dictionary)

    字典dict,是Python唯一的标准mapping类型,也是内置在Python解释器中的. mapping object把一个可哈希的值(hashable value)映射到一个任意的object上 ...

  6. Python 字典(Dictionary) setdefault()方法

    描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: ...

  7. C#字典Dictionary排序(顺序、倒序)

    这里是针对.NET版本过低的排序方式,没怎么用过,记录一下: 一.创建字典Dictionary 对象 假如 Dictionary 中保存的是一个网站页面流量,key 是网页名称,值value对应的是网 ...

  8. Python 字典(Dictionary) has_key()方法

    描述 Python 字典(Dictionary) has_key() 函数用于判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false. 语法 has_key()方法语法:dic ...

  9. 字典 Dictionary

    字典 Dictionary {Key-Value} 1.字典是无序的,没有下标(因为有key,取值直接用key值) Key尽量不要用中文编写,以防止编码不同导致取不出来 2.字典常用方法: 查找: ① ...

  10. [Swift]JSON字符串与字典(Dictionary)、数组(Array)之间的相互转换

    1.JSON字符串与字典(Dictionary)之间的相互转换 import Foundation //JSON字符串转换为字典(Dictionary) func getDictionaryFromJ ...

随机推荐

  1. sso demo mysql ( cas )

    基本配置 参考之前得随笔  http://www.cnblogs.com/rocky-fang/p/5354947.html 1. tomcat-cas 修改配置 1.1 在D:\test\sso\t ...

  2. mysql Access denied for user root@localhost错误解决方法总结(转)

    mysql Access denied for user root@localhost错误解决方法总结(转) mysql Access denied for user \'root\'@\'local ...

  3. Monkey测试3——Monkey测试结果分析

    Monkey测试结果分析 一. 初步分析方法: Monkey测试出现错误后,一般的差错步骤为以下几步: 1. 找到是monkey里面的哪个地方出错 2. 查看Monkey里面出错前的一些事件动作,并手 ...

  4. Spring4学习笔记1-HelloWorld与IOC和DI概述

    1.安装环境 1.1安装eclipse,jdk 1.1安装Spring tool suite(非必要项) 2.spring HelloWorld 2.1 需要的jar包(spring官网下载:http ...

  5. IOS 调用WebService(同步和异步)

    因为公司的服务全都是webservice,每次总要花费大量时间在调试服务上面,干脆就写了一个解析wsdl的项目,希望将来能用上吧.还未经过烘焙,有问题,还请高手点播点播. 下面,我拿天气服务的wsdl ...

  6. 使用XmlHelper添加节点C#代码

    接着上一篇:http://keleyi.com/a/bjac/ttssua0f.htm在前篇文章中,给出了C# XML文件操作类XmlHelper的代码,以及使用该类的一个例子,即使用XmlHelpe ...

  7. 总结CSS3新特性(Transform篇)

    概述: CSS3新添加的Transform可以改变元素在可视化区域的坐标(这种改变不会引起文档的重排,只有重排),以及形状,还有些3D形变.结合 Animation(这里以后会有个链接的) 能实现酷炫 ...

  8. MSCRM 2013/2015 Ribbon Editor

    由于新版本2015的解决方案与之前有变化,因此许多老的Tools已经不能使用,推荐给大家新的Ribbon Editor Tool. 下载地址: http://www.develop1.net/publ ...

  9. SharePoint 2013 JavaScript 对象判断用户权限

    场 景 近期有个场景,判断当前用户对项目有没有编辑权限,使用JavaScript完成,弄了好久才弄出来,分享一下,有需要的自行扩展吧,具体如下: 代 码 function getPermissions ...

  10. Linux新手扫盲

    一. Linux特点 1.免费/开源: 2.支持多线程/多用户: 3.安全性好: 4.对内存和文件管理优越. Linux最小只需4M ——> 嵌入式开发 二. 文件目录 Linux系统所有软硬件 ...