GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.runoob.com — Python 字典(Dictionary) | 菜鸟教程
Python 基础教程
Python 基础教程
Python 简介
Python 环境搭建
Python 中文编码
Python VS Code
Python 基础语法
Python 变量类型
Python 运算符
Python 条件语句
Python 循环语句
Python While 循环语句
Python for 循环语句
Python 循环嵌套
Python break 语句
Python continue 语句
Python pass 语句
Python Number(数字)
Python 字符串
Python 列表(List)
Python 元组
Python 字典(Dictionary)
Python 日期和时间
Python 函数
Python 模块
Python 文件I/O
Python File 方法
Python 异常处理
Python OS 文件/目录方法
Python 内置函数
Python 高级教程Python 面向对象 Python 正则表达式 Python CGI 编程 Python MySQL Python 网络编程 Python SMTP Python 多线程 Python XML 解析 Python GUI 编程(Tkinter) Python2.x 与 3.x 版本区别 Python IDE Python JSON Python AI 绘画 Python 100例 Python 测验Python 字典(Dictionary)字典是另一种可变容器模型,且可存储任意类型对象。 字典的每个键值 key:value 对用冒号 : 分割,每个键值对之间用逗号 , 分割,整个字典包括在花括号 {} 中 ,格式如下所示: d = {key1 : value1, key2 : value2 }
注意:dict 作为 Python 的关键字和内置函数,变量名不建议命名为 dict。 键一般是唯一的,如果重复最后的一个键值对会替换前面的,值不需要唯一。
>>> tinydict = {'a': 1, 'b': 2, 'b': '3'}
>>> tinydict['b'] '3' >>> tinydict {'a': 1, 'b': '3'} 值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。 一个简单的字典实例: tinydict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
也可如此创建字典: tinydict1 = { 'abc': 456 }
tinydict2 = { 'abc': 123, 98.6: 37 }
访问字典里的值把相应的键放入熟悉的方括弧,如下实例: 实例#!/usr/bin/python
tinydict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print "tinydict['Name']: ", tinydict['Name']
print "tinydict['Age']: ", tinydict['Age']
以上实例输出结果: tinydict['Name']: Zara tinydict['Age']: 7 如果用字典里没有的键访问数据,会输出错误如下: 实例#!/usr/bin/python
tinydict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
print "tinydict['Alice']: ", tinydict['Alice']
以上实例输出结果: tinydict['Alice']:
Traceback (most recent call last):
File "test.py", line 5, in <module>
print "tinydict['Alice']: ", tinydict['Alice']
KeyError: 'Alice'
修改字典向字典添加新内容的方法是增加新的键/值对,修改或删除已有键/值对如下实例: 实例#!/usr/bin/python
# -*- coding: UTF-8 -*-
tinydict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
tinydict['Age'] = 8 # 更新
tinydict['School'] = "RUNOOB" # 添加
print "tinydict['Age']: ", tinydict['Age']
print "tinydict['School']: ", tinydict['School']
tinydict['Age']: 8 tinydict['School']: RUNOOB 删除字典元素能删单一的元素也能清空字典,清空只需一项操作。 显示删除一个字典用del命令,如下实例: 实例#!/usr/bin/python
# -*- coding: UTF-8 -*-
tinydict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
del tinydict['Name'] # 删除键是'Name'的条目
tinydict.clear() # 清空字典所有条目
del tinydict # 删除字典
print "tinydict['Age']: ", tinydict['Age']
print "tinydict['School']: ", tinydict['School']
但这会引发一个异常,因为用del后字典不再存在: tinydict['Age']:
Traceback (most recent call last):
File "test.py", line 10, in <module>
print "tinydict['Age']: ", tinydict['Age']
NameError: name 'tinydict' is not defined
注:del() 方法后面也会讨论。 字典键的特性字典值可以没有限制地取任何 python 对象,既可以是标准的对象,也可以是用户定义的,但键不行。 两个重要的点需要记住:
1)不允许同一个键出现两次。创建时如果同一个键被赋值两次,后一个值会被记住,如下实例:
实例#!/usr/bin/python
tinydict = {'Name': 'Runoob', 'Age': 7, 'Name': 'Manni'}
print "tinydict['Name']: ", tinydict['Name']
以上实例输出结果: tinydict['Name']: Manni 2)键必须不可变,所以可以用数字,字符串或元组充当,所以用列表就不行,如下实例: 实例#!/usr/bin/python
tinydict = {['Name']: 'Zara', 'Age': 7}
print "tinydict['Name']: ", tinydict['Name']
以上实例输出结果: Traceback (most recent call last):
File "test.py", line 3, in <module>
tinydict = {['Name']: 'Zara', 'Age': 7}
TypeError: unhashable type: 'list'
字典内置函数&方法Python字典包含了以下内置函数:
Python字典包含了以下内置方法:
AI 思考中...
| ||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |