2. 并集 >>> {0,1,2,3,4,5,7,8,9}|{0,2,4,6,8} {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} >>> {0,1,2,3,4,5}|{0,2,4,6,8} {0, 1, 2, 3, 4, 5, 6, 8} 方法: s1|s2|…|sn 3. 差集 >>> {0,1,2,3,4,5,6,7,8,9}-{0,2,4,6,8} {1, 3, 5, 9, 7} >>> {0,1,2,3,4,5,6,7,8,9}-{0,2,4,6,8}-{2,3,4} {1, 5, 9, 7} 方法: s1-s2-…-sn 4. 对称差集 >>> {0,1,2,3,4,5,6,7,8,9}^{0,2,4,6,8} {1, 3, 5, 7, 9} >>> {0,1,2,3,4,5,6,7,8,9}^{0,2,4,6,8}^{1,3,5,7,9} set() 方法: s1^s2^…^sn 5. 集合的比较 ==、!=、、>= 6.6 字典 通过任意键信息查找一组数据中值信息的过程叫映射,Python语言中通过字典实现映射。Python语言中的字典可以通过大括号({})建立,建立模式如下: {:, :, … , :} 其中,键和值通过冒号连接,不同键值对通过逗号隔开。 6.6.1 字典常用操作1.字典的创建 (1)使用“=”将一个字典赋给一个变量 >>> a_dict={'Alice':95,'Beth':82,'Tom':65.5,'Emily':95} >>> a_dict {'Emily': 95, 'Tom': 65.5, 'Alice': 95, 'Beth': 82} >>> b_dict={} >>> b_dict {} (2)使用内建函数dict() >>>c_dict=dict(zip(['one', 'two', 'three'], [1, 2, 3])) >>>c_dict {'three': 3, 'one': 1, 'two': 2} >>>d_dict = dict(one = 1, two = 2, three = 3) >>>e_dict= dict([('one', 1),('two',2),('three',3)]) >>>f_dict= dict((('one', 1),('two',2),('three',3))) >>> g_dict=dict() >>> g_dict {} (3)使用内建函数fromkeys() 一般形式:dict.fromkeys(seq[, value])) >>> h_dict={}.fromkeys((1,2,3),'student’) >>> h_dict {1: 'student', 2: 'student', 3: 'student'} >>> i_dict={}.fromkeys((1,2,3)) >>> i_dict {1: None, 2: None, 3: None} >>> j_dict={}.fromkeys(()) >>> j_dict {} 2.字典元素的读取 (1)使用下标的方法 >>>a_dict={'Alice':95,'Beth':82,'Tom':65.5,'Emily':95} >>> a_dict['Tom'] 65.5 >>> a_dict[95] Traceback (most recent call last): File "", line 1, in a_dict[95] KeyError: 95 (2)使用get方法获取执行键对于的值 一般形式:dict.get(key, default=None) default是指指定的“键”值不存在时,返回的值 >>> a_dict.get('Alice') 95 >>> a_dict.get('a','address') 'address' >>> a_dict.get('a') >>> print(a_dict.get('a')) None a_dict={'Alice':95,'Beth':82,'Tom':65.5,'Emily':95} 3.字典元素的添加与修改 (1)使用 “字典名[键名]=键值”形式 >>> a_dict['Beth']=79 >>> a_dict {'Alice': 95, 'Beth': 79, 'Emily': 95, 'Tom': 65.5} >>> a_dict['Eric']=98 >>>
python组合数据类型分类_《Python程序设计与案例教程》课件chap6组合数据类型.ppt...
最新推荐文章于 2020-12-11 10:34:28 发布