第二章Python下的set,dictionary,none
来源:互联网 发布:4s店网络销售沟通技巧 编辑:程序博客网 时间:2024/06/04 21:13
Set
set就是一组无序的唯一的值,set可以保存任意种类的数据类型
一、创建一个set
>>> a_set = {1} ①>>> a_set{1}>>> type(a_set) ②<class 'set'>>>> a_set = {1, 2} ③>>> a_set{1, 2}1、创建只有一个值的set,将值放入{}中
2、set也是一个类
3、创建多个值的set,使用{}包围所有值并用,分隔每个值
使用list创建set
>>> a_list = ['a', 'b', 'mpilgrim', True, False, 42]>>> a_set = set(a_list) ①>>> a_set = set()②1、使用set()方法将list转换为set
使用set()方法也可以创建空set,不带任何参数
二、修改Set
add方法
>>> a_set = {1, 2}>>> a_set.add(4) ①>>> a_set{1, 2, 4}>>> len(a_set) ②3>>> a_set.add(1) ③>>> a_set{1, 2, 4}>>> len(a_set) ④31、add()方法接受一个参数,任意类型,添加到set中
2、添加后有了3个成员
3、set的值是唯一的,添加一个已经在set中的,会没有反应
update方法
>>> a_set = {1, 2, 3}>>> a_set{1, 2, 3}>>> a_set.update({2, 4, 6}) ①>>> a_set ②{1, 2, 3, 4, 6}>>> a_set.update({3, 6, 9}, {1, 2, 3, 5, 8, 13}) ③>>> a_set{1, 2, 3, 4, 5, 6, 8, 9, 13}>>> a_set.update([10, 20, 30]) ④>>> a_set{1, 2, 3, 4, 5, 6, 8, 9, 10, 13, 20, 30}1、update()方法接受一个参数必须是set,会将set原有成员添加到原有的set中
2、重复的值会忽略掉
3、update()可以接受多个set
4、update()可以接受多种不同数据类型包括list,会吧list中所有成员添加到set中
从set中删除元素
>>> a_set = {1, 3, 6, 10, 15, 21, 28, 36, 45}>>> a_set{1, 3, 36, 6, 10, 45, 15, 21, 28}>>> a_set.discard(10) ①>>> a_set{1, 3, 36, 6, 45, 15, 21, 28}>>> a_set.discard(10) ②>>> a_set{1, 3, 36, 6, 45, 15, 21, 28}>>> a_set.remove(21) ③>>> a_set{1, 3, 36, 6, 45, 15, 28}>>> a_set.remove(21) ④Traceback (most recent call last):File "<stdin>", line 1, in <module>KeyError: 211、discard()方法接受一个参数值,从set中删除这个值
2、discard()如果接受的值不在set中,什么都不会发生
3、remove()方法接受一个参数值,从set中删除这个值
4、remove()如果接受的值不在set中,会抛异常
pop()
>>> a_set = {1, 3, 6, 10, 15, 21, 28, 36, 45}>>> a_set.pop() ①1>>> a_set.pop()3>>> a_set.pop()36>>> a_set{6, 10, 45, 15, 21, 28}>>> a_set.clear() ②>>> a_setset()>>> a_set.pop() ③Traceback (most recent call last):File "<stdin>", line 1, in <module>KeyError: 'pop from an empty set'1、pop()方法将set中一个值删掉,因为set是无序的所以无法删掉按顺序删
2、clear()方法将set中的全部值删掉,使set编程空的,等同于创建一个新的 = set()
3、从空的set调用pop()会抛出异常
set的操作
>>> a_set = {2, 4, 5, 9, 12, 21, 30, 51, 76, 127, 195}>>> 30 in a_set ①True>>> 31 in a_setFalse>>> b_set = {1, 2, 3, 5, 6, 8, 9, 12, 15, 17, 18, 21}>>> a_set.union(b_set) ②{1, 2, 195, 4, 5, 6, 8, 12, 76, 15, 17, 18, 3, 21, 30, 51, 9, 127}>>> a_set.intersection(b_set) ③{9, 2, 12, 5, 21}>>> a_set.difference(b_set) ④{195, 4, 76, 51, 30, 127}>>> a_set.symmetric_difference(b_set) ⑤{1, 3, 4, 6, 8, 76, 15, 17, 18, 195, 127, 30, 51}1、使用in来判断set中是否包含这个值
2、union()方法将多个集合创建为新的集合
3、intersection()方法将两个集合中相同的元素返回为一个新集合
4、difference()方法将两个集合中不同的元素返回为一个新集合
5、symmetric_difference()方法将两个集合中互相不含有的元素返回为一个新集合
>>> a_set = {1, 2, 3}>>> b_set = {1, 2, 3, 4}>>> a_set.issubset(b_set) ①True>>> b_set.issuperset(a_set) ②True>>> a_set.add(5) ③>>> a_set.issubset(b_set)False>>> b_set.issuperset(a_set)False1、issubset ()子集,谁被谁包含谁是子集,返回True False
2、issuperset(),谁包含谁谁是超集,返回True False
Set在Boolean表达式中
1、空的set是false
2、非空的set是true,不管数据类型的问题
Dictionary
dictionary是无序的键值对集合
一、创建dictionary
>>> a_dict = {'server': 'db.diveintopython3.org', 'database': 'mysql'} ①>>> a_dict{'server': 'db.diveintopython3.org', 'database': 'mysql'}>>> a_dict['server'] ②'db.diveintopython3.org'>>> a_dict['database'] ③'mysql'>>> a_dict['db.diveintopython3.org'] ④Traceback (most recent call last):File "<stdin>", line 1, in <module>KeyError: 'db.diveintopython3.org'1、dictionary使用{}括起来,每个键值对用:号隔开
2、'server'是key,a_dict['server']的值是'db.diveintopython3.org'
3、‘database’是key,a_dict['dict']的值是'mysql'
4、可以通过键获得值但无法通过值获得键,查找不存在的键会抛异常
二、修改dictionary
>>> a_dict{'server': 'db.diveintopython3.org', 'database': 'mysql'}>>> a_dict['database'] = 'blog' ①>>> a_dict{'server': 'db.diveintopython3.org', 'database': 'blog'}>>> a_dict['user'] = 'mark' ②>>> a_dict ③{'server': 'db.diveintopython3.org', 'user': 'mark', 'database': 'blog'}>>> a_dict['user'] = 'dora' ④>>> a_dict{'server': 'db.diveintopython3.org', 'user': 'dora', 'database': 'blog'}>>> a_dict['User'] = 'mark' ⑤>>> a_dict{'User': 'mark', 'server': 'db.diveintopython3.org', 'user': 'dora', 'database': 'blog'}1、键不能重复,为已经存在的键赋值会替代原有的值
2、可以添加新的键值对,任意时刻
3、
4、新的值会替换旧的值
5、dictionary的键是区分大小写的
dictionary的值
>>> SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],... 1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']}>>> len(SUFFIXES) ①2>>> 1000 in SUFFIXES ②True>>> SUFFIXES[1000] ③['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']>>> SUFFIXES[1024] ④['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']>>> SUFFIXES[1000][3] ⑤'TB'1、len()方法返回dictionary的键的数量
2、使用in来判断键是否在dictionary之中
3、1000是键,他的值是list
4、1024是键
5、SUFFIXES[1000]是一个list,可以使用索引值选择list中的元素
dictionary在Boolean表达式中
1、空的dictionary是false
2、非空的是true
None
none是Python中的特殊常量,是一个空值,none和任何值相比永远返回false
>>> type(None)<class 'NoneType'>>>> None == FalseFalse>>> None == 0False>>> None == ''False>>> None == NoneTrue>>> x = None>>> x == NoneTrue>>> y = None>>> x == yTrue
none在boolean表达式中
在boolean表达式中,none是false 非none是true
- 第二章Python下的set,dictionary,none
- 第三章Python解析list,dictionary,set
- python学习------dictionary和set
- Python的list、tuple、set、dictionary操作总结
- Python之List、Set、Tuple、Dictionary的区别
- python下 list/dictionary
- python None的理解
- 【Python基础】之set, list, dictionary
- python学习笔记16:tuple, set, dictionary
- python基础[list,tuple,dictionary,set]
- dictionary的例子 python
- python dictionary的使用
- 王亟亟的Python学习之路(五)-dictionary,set,函数,函数参数
- [Python笔记][第二章Python序列-tuple,dict,set]
- None, Python 的 Null 对象
- python--set集合下的函数
- Python的is None 和 == None
- python基础31[数据结构list+tuple+set+dictionary]
- VC:如何获得当前程序的视图,文档指针
- sharepoint 2010学习资源汇总
- QT笔记之QColorDialog
- 有序数组合并的并行算法(有一定限制)
- C++反汇编之“认识启动函数,找到用户入口”
- 第二章Python下的set,dictionary,none
- DRP视频——前言
- gdb 常用命令
- HTML5全球定位系统Geolocation初探
- eval('?'.'>'$code)用法一些解析
- VC:如何创建属性对话框
- (iPhone/iPad开发)由经纬度计算距离
- 安装 Tokyo Cabinet 和 Tokyo Tyrant
- asp.net服务器绝对路径转换成URL相对路径