Python Dict用法
申明 m={};
[python]dictionary方法说明
2007-03-13 18:10
|
#字典的添加、删除、修改操作
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用法相关推荐
- Python高级用法总结
Python很棒,它有很多高级用法值得细细思索,学习使用.本文将根据日常使用,总结介绍Python的一组高级特性,包括:列表推导式.迭代器和生成器.装饰器. 列表推导(list comprehensi ...
- python dict排序_python 字典(dict)按键和值排序
python 字典(dict)的特点就是无序的,按照键(key)来提取相应值(value),如果我们需要字典按值排序的话,那可以用下面的方法来进行: 1 下面的是按照value的值从大到小的顺序来排序 ...
- python del用法_python del()函数用法 -电脑资料
示例程序如下: >>> a = [-1, 3, 'aa', 85] # 定义一个list >>> a [-1, 3, 'aa', 85] >>> ...
- 技巧 | Python 字典用法详解(超全)
文章目录 1.dict.clear() 2.dict.copy() 3.dict.fromkeys() 4.dict.get() 5.dict.items() 6.dict.keys() 7.dict ...
- python系列之:python基础用法
python系列之:python基础用法 一.定义变量,并打印变量 二.Python字符串和引号用法 三.python注释 四.print输出 五.python标准数据类型 六.Python数字Num ...
- python高级用法技巧-Python高级用法总结
列表推导(list comprehensions) 场景1:将一个三维列表中所有一维数据为a的元素合并,组成新的二维列表. 最简单的方法:新建列表,遍历原三维列表,判断一维数据是否为a,若为a,则将该 ...
- Python pandas用法
Python pandas用法 无味之味关注 12019.01.10 15:43:25字数 2,877阅读 91,914 介绍 在Python中,pandas是基于NumPy数组构建的,使数据预处理. ...
- python goto 用法
python goto 用法 pip install goto-statement from goto import with_goto @with_goto def range(start, sto ...
- Python SQLite 用法
Python SQLite 用法 具体可以参考网址 代码: #导入 import sqlite3 #连接库,如果数据库不存在,那么它就会被创建,最后将返回一个数据库对象. # test.db:数据路路 ...
最新文章
- SQL:EXISTS的用法理解(转)
- 大型Web前端架构设计:面向抽象编程入门
- Android 屏幕旋转时Activity的变化
- ITK:二进制和两个图像
- 为什么选择Nginx
- 【C语言简单说】十七:数组
- 麦克风的喧响伪原创工具
- Detect to Track and Track to Detect
- 开发者编程时应该围着“程序”转吗?
- OpenGL与gl glu glut freeglut glew glfw封装库关系(十五)
- 小白学习一eNSP华为模拟器(3) 交换机基础配置 实验四VLAN 配置Trunk
- 宏基aspire拆机触摸_Acer宏基E1471G笔记本怎么拆机拆主板?
- 计算年龄:DATEDIF函数
- 龙之谷2服务器维护,龙之谷2更新后进不去游戏解决方法
- 计算机课外活动兴趣小组内容,学校课外兴趣小组活动总结
- weblogic漏洞总结复现
- 获取JOP卡的版本与功能信息
- BurpSuit在不同浏览器中配置代理
- ChatGPT接入微信公众号(手把手教学)
- Android万能遥控菜单选择添加,将小米米家万能遥控器添加到Home Assistant