Python:Python基础(一)
2016-05-08 08:23
543 查看
基本数据类型补充
setset集合,是一个无序且不重复的元素集合
1、创建set
①s = set( )
②s = { }
注:出现{}并不是指字典创建,字典里面是键值对,而set里面是无序且不重复的元素
2、转换
只要是可迭代的对象都可以转换
#列表转换成set集合 li = [11,22,33,44] s = set(li) print(s) #打印结果:{33, 11, 44, 22} #set集合转换成元祖 tu = tuple(s) print(tu) #打印结果:(33, 11, 44, 22)
3、set提供的方法
添加一个元素:add()
se = {11,22,33,} se.add(44) print(se) #打印结果:{33, 11, 44, 22}
更新:update()
注:只能添加可迭代的类型
A = {44,55} B = {22,33,44,55,66} A.update(B) print(A) #打印结果:{33, 66, 22, 55, 44} A = {44,55} A.update([4,5]) print(A) #打印结果:{5, 44, 4, 55} A = {44,55} A.update(4,5) print(A) #报错
移除元素(获取任意一个元素并删除):pop()
B = {22,33,44,55,66} ret = B.pop() print(B) print(ret) #打印结果: {66, 44, 22, 55} 33
清除内容:clear()
se = {11,22,33,} se.clear() print(se) #打印结果:set()
并集:union()
A = {44,55} B = {22,33,44,55,66} ret = A.union(B) print(ret) #打印结果:{33, 66, 22, 55, 44}
A中存在,B中不存在:difference()
A = {11,22,33} B = {22,55} r1 = A.difference(B) print(r1) #打印结果:{33, 11} #B中存在,A中不存在 A = {11,22,33} B = {22,55} r2 = B.difference(A) print(r2) #打印结果:{55}
找A中存在B中不存在的元素(从当前集合中删除和B中相同的元素),并更新:difference_update()
A = {11,22,33} B = {22,55} A.difference_update(B) print(A) #打印结果:{33, 11}
交集:intersection()
A = {11,22,33} B = {22,55} ret = A.intersection(B) print(ret) #打印结果:{22}
取交集并更新到A中:intersection_uptate()
A = {11,22,33} B = {22,55} A.intersection_update(B) print(A) #打印结果:{22}
对称交集(两个集合中不同的元素):symmetric_difference()
A = {44,55} B = {22,33,44,55,66} ret = A.symmetric_difference(B) print(ret) #打印结果:{33, 66, 22} A = {44,55} B = {22,33,44,55,66} ret = B.symmetric_difference(A) print(ret) #打印结果:{33, 66, 22}
对称交集(两个集合中不同的元素),并更新集合:symmetric_difference_update()
A = {44,55} B = {22,33,44,55,66} # ret = A.symmetric_difference(B) A.symmetric_difference_update(B) print(A) #打印结果:{33, 66, 22}
移除指定元素,不存在不报错:discard()
注:remove,不存在要报错
A = {11,22,33} A.discard(11) print(A) #打印结果:{33, 22}
是否有交集。如果没有交集,返回True,有交集返回False:isdisjoint()
A = {11,22,33} B = {22,55} ret = A.isdisjoint(B) print(ret) #打印结果:False A = {11,22,33} B = {44,55} ret = A.isdisjoint(B) print(ret) #打印结果:True
是否是子序列:issubset()
#A是B的子序列,返回True,A不是B的子序列,返回False
A = {11,22,33} B = {11,22} ret = A.issubset(B) print(ret) #打印结果:False A = {22,33} B = {11,22,33} ret = A.issubset(B) print(ret) #打印结果:True
是否是父序列:issuperset()
#A是B的父序列,返回True,A不是B的父序列,返回False
A = {11,22,33,44,55} B = {22,33} ret = A.issuperset(B) print(ret) #打印结果:True A = {44,55} B = {22,33,44,55,66} ret = A.issuperset(B) print(ret) #打印结果:False
练习:寻找差异
new_dict,old_dict,字典中的key相同的
new_dict[key]值 --> old_dict[key]的值
old中存在,new不存在,old中删除
new_dict.keys存在,在old中添加
1 old_dict = { 2 "k1": 11, 3 "k2": 22, 4 "k3": 100, 5 } 6 new_dict = { 7 "k1": 33, 8 "k4": 22, 9 "k7": 100, 10 } 11 #将字典转换成set集合并获得key 12 old_keys = set(old_dict.keys()) 13 new_keys = set(new_dict.keys()) 14 #找到相同的key 15 update_key = old_keys.intersection(new_keys) 16 #把new字典中的值替换到old里面 17 for i in update_key: 18 old_dict[i] = new_dict[i] 19 #print(old_dict) 20 #找出old中存在new中不存在的key 21 del_keys = old_keys.difference(new_keys) 22 #删除old中有的,new中没有的 23 for i in del_keys: 24 del old_dict[i] 25 #找出new中有的,old中没有的 26 add_keys = new_keys.difference(old_keys) 27 #把new中有的old中没有的添加到old中 28 for i in add_keys: 29 old_dict[i] = new_dict[i] 30 print(old_dict) #打印结果:{'k4': 22, 'k7': 100, 'k1': 33}
三元运算
三元运算(三目运算),是对简单的条件语句的缩写。#书写格式: result = 值1 if 条件 else 值2 #如果条件成立,那将值1赋值给result变量,否则,将值2赋值给result变量
示例:
name = "alex" if 1 == 1 else "eric" print(name) #打印结果:alex name = "alex" if 1 == 0 else "eric" print(name) #打印结果:eric
深浅拷贝
一、数字(int)和字符串(str)
对于数字和字符串,深拷贝、浅拷贝和赋值都一样,因为永远指向同一个内存地址。def f5(arg): ret = {} #循环所有的键值 for key,value in arg.items(): #当值的长度大于2 if len(value) > 2: ret[key] = value[0:2] else: ret[key] = value return ret dic = {"k1":"v1v1","k2":[11,22,33,44],"k3":"12"} r = f5(dic) print(r) #打印结果:{'k1': 'v1', 'k3': '12', 'k2': [11, 22]}
View Code
相关文章推荐
- python的random模块及加权随机算法的python实现
- python 中 pass 的用法
- python(2)readlines()函数的注意问题
- Python_bug收集_TypeError:'str' does not support the buffer interface
- 用 python skimage做图像处理
- Python常用知识点汇总
- Python数据类型详解(一)字符串
- Python数据类型详解(二)列表
- Python数据类型详解(三)元祖:tuple
- python下编写守护进程
- Python学习笔记--迭代器&生成器
- python数据采集-数据存储 005
- Python3和2常用语句的语法区别2.0
- Python 文件操作总结
- thrift TBinaryProtocol 数据传输格式
- Python 测试(一)—— doctest
- python各编码转换方法
- 在 Spark 中使用 IPython Notebook
- Python 时间处理函数
- leetcode(2), Nin Game详解(python)