第10节--python数据结构--dictionary
来源:互联网 发布:java时间比较大小 编辑:程序博客网 时间:2024/06/05 16:36
1、dictionary结构介绍:
{key:value}
2、dictionary基本操作:
# -*- coding: utf-8 -*-#创建词典phone_book = {'Tom': 123, "Jerry": 456, 'Kim': 789}mixed_dict = {"Tom": 'boy', 11: 23.5}#访问词典里的值print("Tom's number is " + str(phone_book['Tom']))print('Tom is a ' + mixed_dict['Tom'])#修改词典phone_book['Tom'] = 999phone_book['Heath'] = 888print("phone_book: " + str(phone_book)) phone_book.update({'Ling':159, 'Lili':247})print("updated phone_book: " + str(phone_book)) #删除词典元素del phone_book['Tom']print("phone_book after deleting Tom: " + str(phone_book)) #清空词典phone_book.clear()print("after clear: " + str(phone_book))#删除词典# del phone_book# print("after del: " + str(phone_book))#不允许同一个键出现两次rep_test = {'Name': 'aa', 'age':5, 'Name': 'bb'}print("rep_test: " + str(rep_test))#键必须不可变,所以可以用数字,字符串或者元组充当,列表不行list_dict = {['Name']: 'John', 'Age':13}list_dict = {('Name'): 'John', 'Age':13}
3、dictionary内置函数&方法:
# Python字典包含了以下内置函数:# 1、cmp(dict1, dict2):比较两个字典元素(python2)。# 2、len(dict):计算字典元素个数,即键的总数。# 3、str(dict):输出字典可打印的字符串表示。# 4、type(variable):返回输入的变量类型,如果变量是字典就返回字典类型。# Python字典包含了以下内置方法:# 1、radiansdict.clear():删除字典内所有元素# 2、radiansdict.copy():返回一个字典的浅复制# 3、radiansdict.fromkeys():# 创建一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值# 4、radiansdict.get(key, default=None):# 返回指定键的值,如果值不在字典中返回default值# 5、radiansdict.has_key(key):# 如果键在字典dict里返回true,否则返回false# 6、radiansdict.items():以列表返回可遍历的(键, 值) 元组数组# 7、radiansdict.keys():以列表返回一个字典所有的键# 8、radiansdict.setdefault(key, default=None):# 和get()类似, 但如果键已经不存在于字典中,将会添加键并将值设为default# 9、radiansdict.update(dict2):把字典dict2的键/值对更新到dict里# 10、radiansdict.values():以列表返回字典中的所有值
0 0
- 第10节--python数据结构--dictionary
- Python入门教程-10 字典dictionary
- 第7节--python数据结构--list
- 第8节--python数据结构--tuple
- python基础31[数据结构list+tuple+set+dictionary]
- python Dictionary
- python Dictionary
- Python Dictionary
- Python Dictionary
- python dictionary
- python dictionary
- python---dictionary
- 第四章数据结构-Dictionary
- [从头学数学] 第244节 Python实现数据结构:列表
- [从头学数学] 第245节 Python实现数据结构:排序
- [从头学数学] 第246节 Python实现数据结构:链表
- [从头学数学] 第247节 Python实现数据结构:栈
- [从头学数学] 第248节 Python实现数据结构:队列
- 第9周项目三--稀疏矩阵的三元组表示的实现及应用(1)
- Java并发编程实践笔记(五)——chapter3(发布溢出,线程封闭)
- git 连接 github
- 更改django admin登录界面显示的"Django 管理"
- 集成ReactNative过程中的那些坑
- 第10节--python数据结构--dictionary
- 设置TextView文本超出显示省略号
- mysql函数
- Android使用fitsSystemWindows属性实现状态栏【status_bar】的适配方案
- OKHTTP之缓存配置详解
- Java集合之 ArrayList源码解析
- UDP、TCP、RTP三种协议的总结
- 基于Maven构建整合SpringMVC+Mybtis+Druid
- FileStore::mount处理过程