python]dictionary方法说明
2007-05-19 23:24
申明 m={};
[python]dictionary方法说明
2007-03-13 18:10
Operation Result Notes
len(a) the number of items in a 得到字典中元素的个数
 
a[k] the item of a with key k 取得键K所对应的值
(1), (10)
a[k] = v set a[k] to v 设定键k所对应的值成为v
 
del a[k] remove a[k] from a 从字典中删除键为k的元素
(1)
a.clear() remove all items from a 清空整个字典
 
a.copy() a (shallow) copy of a 得到字典副本
 
k in a True if a has a key k, else False 字典中存在键k则为返回True,没有则返回False
(2)
k not in a Equivalent to not k in a   字典中不存在键k则为返回true,反之返回False (2)
a.has_key(k) Equivalent to k in a, use that form in new code 等价于k in a  
a.items() a copy of a's list of (keyvalue) pairs 得到一个键,值的list (3)
a.keys() a copy of a's list of keys 得到键的list (3)
a.update([b]) updates (and overwrites) key/value pairs from b从b字典中更新a字典,如果键相同则更新,a中不存在则追加 (9)
a.fromkeys(seq[value]) Creates a new dictionary with keys from seq and values set to value 
(7)
a.values() a copy of a's list of values (3)
a.get(k[x]) a[k] if k in a, else x (4)
a.setdefault(k[x]) a[k] if k in a, else x (also setting it) (5)
a.pop(k[x]) a[k] if k in a, else x (and remove k) (8)
a.popitem() remove and return an arbitrary (keyvalue) pair (6)
a.iteritems() return an iterator over (keyvalue) pairs (2), (3)
a.iterkeys() return an iterator over the mapping's keys (2), (3)
a.itervalues() return an iterator over the mapping's values (2), (3)

#字典的添加、删除、修改操作
dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}
dict["w"] = "watermelon"
del(dict["a"])
dict["g"] = "grapefruit"
print dict.pop("b")
print dict
dict.clear()
print dict

#字典的遍历
dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}
for k in dict:
    print "dict[%s] =" % k,dict[k]

#字典items()的使用
dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}
#每个元素是一个key和value组成的元组,以列表的方式输出
print dict.items()

#调用items()实现字典的遍历
dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}
for (k, v) in dict.items():
    print "dict[%s] =" % k, v

#调用iteritems()实现字典的遍历
dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}
print dict.iteritems()
for k, v in dict.iteritems():
    print "dict[%s] =" % k, v
for (k, v) in zip(dict.iterkeys(), dict.itervalues()):
    print "dict[%s] =" % k, v

#使用列表、字典作为字典的值
dict = {"a" : ("apple",), "bo" : {"b" : "banana", "o" : "orange"}, "g" : ["grape","grapefruit"]}
print dict["a"]
print dict["a"][0]
print dict["bo"]
print dict["bo"]["o"]
print dict["g"]
print dict["g"][1]

dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}
#输出key的列表
print dict.keys()
#输出value的列表
print dict.values()
#每个元素是一个key和value组成的元组,以列表的方式输出
print dict.items()

dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}
it = dict.iteritems()
print it

#字典中元素的获取方法
dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}
print dict
print dict.get("c", "apple")         
print dict.get("e", "apple")
#get()的等价语句
D = {"key1" : "value1", "key2" : "value2"}
if "key1" in D:
    print D["key1"]
else:
    print "None"

#字典的更新
dict = {"a" : "apple", "b" : "banana"}
print dict
dict2 = {"c" : "grape", "d" : "orange"}
dict.update(dict2)
print dict
#udpate()的等价语句
D = {"key1" : "value1", "key2" : "value2"}
E = {"key3" : "value3", "key4" : "value4"}
for k in E:
    D[k] = E[k]
print D
#字典E中含有字典D中的key
D = {"key1" : "value1", "key2" : "value2"}
E = {"key2" : "value3", "key4" : "value4"}
for k in E:
    D[k] = E[k]
print D

#设置默认值
dict = {}
dict.setdefault("a")
print dict
dict["a"] = "apple"
dict.setdefault("a","default")
print dict

#调用sorted()排序
dict = {"a" : "apple", "b" : "grape", "c" : "orange", "d" : "banana"}
print dict  
#按照key排序 
print sorted(dict.items(), key=lambda d: d[0])
#按照value排序 
print sorted(dict.items(), key=lambda d: d[1])

#字典的浅拷贝
dict = {"a" : "apple", "b" : "grape"}
dict2 = {"c" : "orange", "d" : "banana"}
dict2 = dict.copy()
print dict2

#字典的深拷贝
import copy
dict = {"a" : "apple", "b" : {"g" : "grape","o" : "orange"}}
dict2 = copy.deepcopy(dict)
dict3 = copy.copy(dict)
dict2["b"]["g"] = "orange"
print dict
dict3["b"]["g"] = "orange"
print dict

转载163某博客

Python Dict用法相关推荐

  1. Python高级用法总结

    Python很棒,它有很多高级用法值得细细思索,学习使用.本文将根据日常使用,总结介绍Python的一组高级特性,包括:列表推导式.迭代器和生成器.装饰器. 列表推导(list comprehensi ...

  2. python dict排序_python 字典(dict)按键和值排序

    python 字典(dict)的特点就是无序的,按照键(key)来提取相应值(value),如果我们需要字典按值排序的话,那可以用下面的方法来进行: 1 下面的是按照value的值从大到小的顺序来排序 ...

  3. python del用法_python del()函数用法 -电脑资料

    示例程序如下: >>> a = [-1, 3, 'aa', 85] # 定义一个list >>> a [-1, 3, 'aa', 85] >>> ...

  4. 技巧 | Python 字典用法详解(超全)

    文章目录 1.dict.clear() 2.dict.copy() 3.dict.fromkeys() 4.dict.get() 5.dict.items() 6.dict.keys() 7.dict ...

  5. python系列之:python基础用法

    python系列之:python基础用法 一.定义变量,并打印变量 二.Python字符串和引号用法 三.python注释 四.print输出 五.python标准数据类型 六.Python数字Num ...

  6. python高级用法技巧-Python高级用法总结

    列表推导(list comprehensions) 场景1:将一个三维列表中所有一维数据为a的元素合并,组成新的二维列表. 最简单的方法:新建列表,遍历原三维列表,判断一维数据是否为a,若为a,则将该 ...

  7. Python pandas用法

    Python pandas用法 无味之味关注 12019.01.10 15:43:25字数 2,877阅读 91,914 介绍 在Python中,pandas是基于NumPy数组构建的,使数据预处理. ...

  8. python goto 用法

    python goto 用法 pip install goto-statement from goto import with_goto @with_goto def range(start, sto ...

  9. Python SQLite 用法

    Python SQLite 用法 具体可以参考网址 代码: #导入 import sqlite3 #连接库,如果数据库不存在,那么它就会被创建,最后将返回一个数据库对象. # test.db:数据路路 ...

最新文章

  1. SQL:EXISTS的用法理解(转)
  2. 大型Web前端架构设计:面向抽象编程入门
  3. Android 屏幕旋转时Activity的变化
  4. ITK:二进制和两个图像
  5. 为什么选择Nginx
  6. 【C语言简单说】十七:数组
  7. 麦克风的喧响伪原创工具
  8. Detect to Track and Track to Detect
  9. 开发者编程时应该围着“程序”转吗?
  10. OpenGL与gl glu glut freeglut glew glfw封装库关系(十五)
  11. 小白学习一eNSP华为模拟器(3) 交换机基础配置 实验四VLAN 配置Trunk
  12. 宏基aspire拆机触摸_Acer宏基E1471G笔记本怎么拆机拆主板?
  13. 计算年龄:DATEDIF函数
  14. 龙之谷2服务器维护,龙之谷2更新后进不去游戏解决方法
  15. 计算机课外活动兴趣小组内容,学校课外兴趣小组活动总结
  16. weblogic漏洞总结复现
  17. 获取JOP卡的版本与功能信息
  18. BurpSuit在不同浏览器中配置代理
  19. ChatGPT接入微信公众号(手把手教学)
  20. Android万能遥控菜单选择添加,将小米米家万能遥控器添加到Home Assistant

热门文章

  1. MapReduce基础开发之七Hive外部表分区
  2. python引用文件的方法_[项目实践] python文件路径引用的
  3. arthas-boot.jar 工具的简单使用
  4. JavaScript 技巧篇-js增加延迟时间解决单击双击事件冲突,双击事件触发单击事件
  5. 概率论与数理统计(二)
  6. 如何去除矩阵中的NaN元素
  7. normest--2-范数的条件数估计
  8. 快速寻找满足条件的两个数
  9. 程序员编程艺术第一章(第二节)
  10. 解决oracle主键问题,解决renren-security使用oracle主键问题