Set 集合

set - unordered collections of unique elements

创建一个set/一个空set

# create a new set
set1 = {1,2,3}
print(type(set1)) # result => <class 'set> set2 = set()
print(type(set2)) # create an empty set, result => <class 'set'> # as the object is iterable, we can create a set with list, tuple, dictionary etc.
list1 = [1, 2, 3]
set2 = set(list1)
print(set2, type(set2))
# {1, 2, 3} <class 'set'> tuple1 = (1, 2, 3)
set2 = set(tuple1)
print(set2, type(set2))
# {1, 2, 3} <class 'set'> dict1 = {'k1':1, 'k2':2}
set2 = set(dict1)
print(set2, type(set2))
# {'k1', 'k2'} <class 'set'> # check if below is workable to create an empty set
set3 = {}
print(type(set3))
# type result => <class 'dict'> Failed
# so the variable_name = {} only works for creating a new dictionary
set在爬虫领域应用较多,比如有很多待爬取的网页,每爬取一个新网页就添加到数据中,如果在数据里已经存在这个网页,就不爬取。 
set的功能:
# to add a new element, we can use add()
# as set only contains different elements, if we add the same element multiple times, only one will remain set1 = {1, 2, 3, } set1.add(4)
set1.add(4)
set1.add(4)
print(set1)
# result => {1, 2, 3, 4} # to clear a set
set1.clear()
print(set1)
# => set() # a.difference(b), elements in a but not in b
set1 = {1, 2, 3, 5, 8, }
set2 = {0, 1, 2, 3, }
ret1 = set1.difference(set2)
print(ret1)
# => {8, 5}
ret2 = set2.difference(set1)
print(ret2)
# => {0} # difference() function does not change the set, if we want to change it, we can use difference_update()
set0 = {-1, }
set0.difference_update(set2)
print(set0) # discard() to remove an element, if not a member, do nothing
set0 = {1, 2, 3, }
set0.discard(1)
print(set0)
set0.discard(-1)
print(set0)
# both print out: {2, 3}, no error msg # if we use remove(), when the target element is not a member, there'll be error
# set0.remove(-1)
# print(set0)
'''
Traceback (most recent call last):
set()
File "D:/NaomiPyer/1016/1017_set_functions.py", line 44, in <module>
{8, 5}
set0.remove(-1)
{0}
KeyError: -1
''' # intersection(), gets the shared info
set1 = {1, 2, 3, 5, 8, }
set2 = {0, 1, 2, 3, }
ret3 = set1.intersection(set2)
print(ret3)
# => {1, 2, 3} # a.intersection_update(b) update a with the intersection of a and b
set1 = {1, 2, 3, 5, 8, }
set2 = {0, 1, 2, 3, }
set1.intersection_update(set2)
print(set1)
# => {1, 2, 3, } # isdisjoint() returns True if two sets has no intersection
set1 = {1, 2, 3, }
set2 = {0, }
ret1 = set1.isdisjoint(set2)
print(ret1)
# => True set1 = {1, 2, 3, }
set2 = {1, }
ret1 = set1.isdisjoint(set2)
print(ret1)
# => False # issubset() report whether another set contains this set
set1 = {1, }
set2 = {0, 1, 2, 3, }
ret1 = set1.issubset(set2)
print(ret1)
# => True, set1 is the subset of set2
# issuperset() is the opposite
ret1 = set1.issuperset(set2)
ret2 = set2.issuperset(set1)
print(ret1) # False
print(ret2) # True # pop() remove and return an arbitrary set element.
# raises keyError if the set is empty
set1 = {1, 2, 3, 4, 6, }
ret1 = set1.pop()
print(set1) # => {2, 3, 4, 6}
print(ret1) # => 1
set1 = set()
ret1 = set1.pop()
print(set1)
print(ret1) # KeyError: 'pop from an empty set' - like remove(), raises KeyError # symmetric_difference()
# returns the symmetric difference of two sets as a new one
set1 = {0, 1, 2, }
set2 = {-1, 0, 3}
ret1 = set1.symmetric_difference(set2)
print(ret1)
# => {1, 2, 3, -1} # symmetric_difference_update()
# update set1 with the result
set1 = {0, 1, 2, }
set2 = {-1, 0, 3}
set1.symmetric_difference_update(set2)
print(set1)
# => {1, 2, 3, -1} # union() returns the union of sets as a new set
set1 = {0, 1, 2, }
set2 = {-1, 0, 3}
ret1 = set1.union(set2)
print(ret1)
# => {0, 1, 2, 3, -1} # update() updates the set with the union of itself and the other set
set1 = {0, 1, 2, }
set2 = {-1, 0, 3}
set1.update(set2)
print(set1)
# => {0, 1, 2, 3, -1}

关于set的方法,intersection()即为两个set的交集,difference()为补集,union() 为并集。

方法里有update的,即为在原set上直接更新。

三目运算|三元运算

if condition:

  block1

else:

  block2

name = value1 if condition else: value2

name = if condition? value1: value2

浅度copy VS 深度copy

str 一次性创建后,不能被修改,数据修改其实是要再次创建的

list 链表创建后会指定下一个元素的位置

int 和 str 的赋值,copy和deepcopy,他们所指向的都是同一内存地址

import copy

n1 = 123

n2 = n1

n3 = copy.copy(n1)

n4 = copy.deepcopy(n2)

对于其他的数据类型如list, tuple, dict, set 等,在copy的时候,内存地址是不一样的

1. 赋值

创建一个变量,变量指向原内存地址

2. 浅copy 和 深copy

浅copy 在内存中只额外创建第一层数据

deepcopy 是除了最后一层,都创建了一次

 import copy

 n1 = n1 = {
'language': 'python',
'IDE': 'PyCharm',
'operating system': ['Linux', 'Windows']
} n2 = copy.copy(n1)
n3 = copy.deepcopy(n1) print(id(n1)) #
print(id(n2)) #
print(id(n3)) #
# all id()'s are different

对比copy和deepcopy的指向

【Python全栈笔记】03 [模块二] 16-17 Oct Set 集合,三目运算的更多相关文章

  1. Python全栈开发【模块】

    Python全栈开发[模块] 本节内容: 模块介绍 time random os sys json & picle shelve XML hashlib ConfigParser loggin ...

  2. Python全栈开发【基础二】

    Python全栈开发[基础二] 本节内容: Python 运算符(算术运算.比较运算.赋值运算.逻辑运算.成员运算) 基本数据类型(数字.布尔值.字符串.列表.元组.字典) 其他(编码,range,f ...

  3. Python全栈day 03

    Python全栈day 03 一.运算符补充 in ,逻辑运算符,判断某字符或某字符串是否在一个大的字符串中,输出得到bool型数据. value = '我是中国人' v = '我' if v in ...

  4. Python全栈开发:模块

    模块,用一砣代码实现了某个功能的代码集合. 参考资源:http://www.cnblogs.com/alex3714/articles/5161349.html 类似于函数式编程和面向过程编程,函数式 ...

  5. Python全栈day21-22-23(模块)

    一,python的模块 Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句. 模块让你能够有逻辑地组织你的 Python ...

  6. 【Python全栈笔记】03 [模块二] 16-17 Oct 函数

    定义一个函数 def function_name(形式参数): 代码块 return 'Value' #如果没有写return,则默认返回None # 一个函数到return这一行就结束执行了,在re ...

  7. 【Python全栈笔记】04 [模块二] 18 Oct lambda表达式, 内置函数

    lambda表达式 lambda表达式是函数的一种简化,如下面两种函数定义方法,实际上效果是一样的. 使用lambda表达式的函数,func2是函数名,lambda: 后面的123 即为返回值. de ...

  8. 【Python全栈笔记】01 [模块二] 14-15 Oct 运算符和字符串 4-1

    编码的问题 中文版 Windows 终端是默认GBK编码格式,python2默认为ASCII编码,虽然我们注释了需要用UTF-8,但是通过命令行运行的文件如果print中文内容还是会出现乱码. Uni ...

  9. 【Python全栈笔记】08 [模块二] 20 Oct 递归 -*** 待补充

    递归 引入 递归的表现形式 下面是四个函数,互相调用返回结果 # 引入 递归的表现形式 def f1(): ' def f2(): r = f1() return r def f3(): r = f2 ...

随机推荐

  1. TestNG @Factory与 @DataProvider 结合使用进行参数化测试

    简介 TestNG是一个设计用来简化广泛的测试需求的测试框架,从单元测试到集成测试,这个是TestNG设计的出发点,不仅仅是单元测试,而且可以用于集成测试.设计目标的不同,对比junit的只适合用于单 ...

  2. 使用Starling 框架时,报错Error: Error #3669: 输入大小错误, 解决方案

    原因有二:1.IE底下,SWFOBJECT嵌入swf的时候,有瞬间的stage的width跟height是0导致的.2.stage.scaleMode = StageScaleMode.NO_SCAL ...

  3. jquery combobox

    主要实现的功能有. 1.点击标签,显示所有数据源 2.在text中输入文本,模糊匹配. 3.配置是否必须要选择. 4.添加选中时的事件. 具体描述如下. combobox原型属性:        原型 ...

  4. EA中的模板管理

    EA在导出文档的时候可以选择各种模板. 使用系统提供的模板导出的文档会稍显繁杂.这时候就需要我们自定义模板. 1. 在导出文档的dialog, 在Template一项中选择 New Template. ...

  5. 牛客网程序员面试金典:1.1确定字符互异(java实现)

    问题描述: 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,True代表所有字符全都不同, ...

  6. autoit 使用excel自带函数操作excel

    Looking into the includes <Excel.au3> helped shed some light on things. To summarize what I've ...

  7. angularJS全选功能实现

    最近在做的一个项目要增加全选和反选功能,之前只做过JQ版的全选和反选. 实现效果: 1.点击全选checkbox可以切换全选和全部清空 2.点击列表中的checkbox,当全部选中时全选选中 3.在全 ...

  8. 2016-08-16: 检测函数是否存在的C++模板

    #include <iostream> struct Hello { ; } }; struct Generic {}; // SFINAE test template <typen ...

  9. 关于Hibernate XXX is not mapped 错误

    我的实体类是这么配置的 @Entity(name="EntityName")  //必须,name为可选,对应数据库中一的个表 就会出现 XXX is not mapped.   ...

  10. 使用archlinux作为日常开发机已经半年了,随便写一下

    机器配置 CPU: Intel Core i5-6200U CPU @ 2.8GHz RAM: 6114MiB / 7421MiB Resolution: 1920x2160 在arch下常用的软件 ...