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

  • 索引可以有副本,但是试图根据存在副本的索引访问元素时,只会取最靠后的那个。
  • 索引必须是immutable的,意味着可以用string, number,tuple都可以,但是list不行
#创建字典
>>> dicA = { 'Num':1 , 'Grade':98 , 'Sch':yes }

#访问元素
>>> print dicA['Sch'], dicA['Num']
yes 1

#添加元素
>>> dicB = {'When':'Today' , 'Affair':'Meeting' , 'Who':'Tom'}
>>> dicB['WithWho'] = 'Sunny'
>>> print dicB
{'Affair': 'Meeting', 'Who': 'Tom', 'When': 'Today', 'WithWho': 'Sunny'}

#更新元素
>>> dicB['Who'] = 'XJ'
>>> print dicB
{'Affair': 'Meeting', 'Who': 'XJ', 'When': 'Today', 'WithWho': 'Sunny'}

#删除元素
>>> dicB = {'When':'Today' , 'Affair':Meeting , 'Who':Tom}
>>> del dicB['When']
>>> print dicB
{'Affair': 'Meeting', 'Who': 'Tom'}

#清空元素
>>> dicB.clear()
>>> print dicB
{}

#删除整个字典
>>> del dicB
>>> print dicB
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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.

>>> dicA,dicB
({'Num': 1, 'Sch': 'yes'}, {'Num': 2, 'Sch': 'yes'})
>>> del dicB['Num']
>>> cmp(dicA,dicB)
1
>>> dicA,dicB
({'Num': 1, 'Sch': 'yes'}, {'Sch': 'yes'})
>>> dicB['Num']=1
>>> cmp(dicA,dicB)
0

len(dict)

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

>>> len(dicA)
2

str(dict)

Produces a printable string representation of a dictionary

>>> str(dicA)
"{'Num': 1, 'Sch': 'yes'}"
>>> print str(dicA)
{'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

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

Built-in Methods

dict.clear()

Removes all elements of dictionary dict

>>> dicB.clear()
>>> print dicB
{}

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

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

dict.fromkeys()

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

>>> dicE = {}
>>> dicE
{}
>>> seq = ('Who','When','What')
>>> dicE = dict.fromkeys(seq)   #或dicE.fromkeys(seq)
>>> dicE
{'What': None, 'Who': None, 'When': None}
>>> dicE = dict.fromkeys(seq,'XXX')
>>> dicE
{'What': 'XXX', 'Who': 'XXX', 'When': 'XXX'}

dict.get(key, default=None)

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

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

dict.has_key(key)

Returns true if key in dictionary dict, false otherwise

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

dict.items()

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

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

dict.keys()

Returns list of dictionary dict's keys

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

dict.setdefault(key, default=None)

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

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

dict.update(dict2)

Adds dictionary dict2's key-values pairs to dict

>>> dicF = {'Work':'No'}
>>> dicE
{'Num': 1, 'Sch': 'yes'}
>>> dicE.update(dicF)
>>> dicE
{'Num': 1, 'Sch': 'yes', 'Work': 'No'}

dict.values()

Returns list of dictionary dict's values

>>> dicE.values()
[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. Word, PPT和Excel的常用技巧(持续更新)

    本文的目的是记录平时使用Word, PowerPoint和Excel的过程中的一些小技巧,用于提升工作效率. 此文会不定期的更新,更新频率完全取决于实际使用遇到的问题的次数. 目录 Word Powe ...

  2. Android中的checkbox和RadioButton的区别

    1.单个RadioButton在选中后,通过点击无法变为未选中       单个CheckBox在选中后,通过点击可以变为未选中 2.一组RadioButton,只能同时选中一个      一组Che ...

  3. mybatis There is no getter for property named 'xxxx

    mybatis There is no getter for property named 'xxxx 360反馈意见截图16230322799670.png http://blog.sina.com ...

  4. 【背景建模】PbModel

    PbModel是基于概率模型的背景差分算法,其基本思想是像素点会因光照变化.运动物体经过产生多种颜色值,但是一段时间内,像素点处于静止状态的时间会比处于运动状态的时间长.因而一段时间内,像素点某个颜色 ...

  5. node.js图片上传

    1.node-formidable 对文件上传提供帮助的组件 2.app.js var formidable = require('formidable'); var http = require( ...

  6. GJM : 使用浏览器的计算力,对抗密码破解 [转载]

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

  7. 【我的产品观】开发wangEditor一年总结

    1. 引言 标题说是一周年,其实是不是正好是一周年,我也忘记了,光从github的提交记录看也不准确.印象中觉得,如果要论想法,到现在一年多了,如果要论实际写代码,可能差不多正好一年. 从8月底在济南 ...

  8. 20个优秀的 JavaScript 键盘事件处理库

    键盘事件是 Web 开发中最常用的事件之一,通过对键盘事件的捕获和处理可以提高网站的易用性和交互体验.下面,我们向大家介绍收集的20款优秀的 JavaScript 键盘事件处理库,帮助开发人员轻松处理 ...

  9. windows中安装node.js和测试

    首先下载node.js安装包:下载页面:http://down.keleyi.com/goto/node.js.htm 选择windows msi安装包,根据自己操作系统选择32位或者64位安装包.然 ...

  10. 【小贴士】zepto find元素以及ios弹出键盘可能让你很头疼

    前言 在此,我不得不说移动端的兼容问题很多,并且很令人头疼,这不,这个星期又有两个让我逮着了,一个是使用zepto过程中出现的问题,一个是ios虚拟键盘的问题 我这里做一次记录,以免以后忘了,同时希望 ...