python组合数据类型分类_《Python程序设计与案例教程》课件chap6组合数据类型.ppt...

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 >>>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值