Python-set、深浅拷贝、三元运算符、函数

Set  无序,不可重复的集合  {}

  创建set集合

tmp = {'1','22','a',5}  
tmp = set() #创建空set集合 tmp = set(iterable) #使用可迭代对象创建set集合

  add() 添加一个元素

tmp = {'alex' , 'eric' , 19 , '777'}
tmp.add('ok')
print(tmp)    #打印:{'eric', 19, 'alex', 'ok', '777'}

  update()  添加多个元素,将序列中每个元素添加到set集合中

tmp = {'alex' , 'eric' , 19 , '777'}
tmp_str = "Good"
tmp.update(tmp_str)
print(tmp)    #打印:{'G', 'o', 19, '777', 'alex', 'eric', 'd'}

tmp = {'alex' , 'eric' , 19 , '777'}
tmp_list = ["Tom" , "Ray"]
tmp.update(tmp_list)
print(tmp)    #打印:{'Ray', 'Tom', 19, '777', 'alex', 'eric'}

tmp = {'alex' , 'eric' , 19 , '777'}
tmp_tuple = ("Tom" , "Ray")
tmp.update(tmp_tuple)
print(tmp)    #打印:{'Ray', 'Tom', 19, '777', 'alex', 'eric'}

tmp = {'alex' , 'eric' , 19 , '777'}
tmp_dict = {"k1":"v1","k2":2}
tmp.update(tmp_dict)
print(tmp)    #打印:{'k1', 'k2', 19, '777', 'alex', 'eric'}

  clear()  清除集合的所有元素

tmp = {'alex' , 'eric' , 19 , '777'}
tmp.clear()
print(tmp)    #打印: set()

  pop()  获取随机元素并删除

tmp = {'alex' , 'eric' , 19 , '777'}
tmp_pop = tmp.pop()
print(tmp_pop)    #打印:777
print(tmp)    #打印:{19, 'eric', 'alex'}

  remove()  删除指定的元素,如元素不存在,会报错 

tmp = {'alex' , 'eric' , 19 , '777'}
tmp.remove('777')
print(tmp)      #打印: {19, 'eric', 'alex'}
tmp = {'alex' , 'eric' , 19 , '777'} tmp.remove(123)   print(tmp) #报错:KeyError: 123

  discard()  删除指定的元素,如元素不存在,不报错

tmp = {'alex' , 'eric' , 19 , '777'}
tmp.discard('eric')
print(tmp)  #打印:{'alex', 19, '777'}

  获取集合A与集合B中相同的元素 (交集)

setA = {'alex', 'eric', 19, '777'}
setB = {'alex', 'eric'}
tmp_intersection = setA.intersection(setB)
print(tmp_intersection)    #打印:{'alex', 'eric'}

  使用集合A与集合B中相同的元素,覆盖更新集合A

setA = {'alex', 'eric', 19, '777'}
setB = {'alex', 'eric'}
setA.intersection_update(setB)
print(setA)    #打印:{'eric', 'alex'}

  获取集合A中与集合B不同的元素

setA = {'alex', 'eric', 19, '777'}
setB = {'alex', 'eric'}
tmp_difference = setA.difference(setB)
print(tmp_difference)    #打印:{19, '777'}

  使用集合A中与集合B不同的元素,覆盖更新集合A

setA = {'alex', 'eric', 19, '777'}
setB = {'alex', 'eric'}
setA.difference_update(setB)
print(setA)    #打印:{19, '777'}

  获取集合A与集合B中所有不同的的元素(非交集)

setA = {'alex', 'eric', 19, '777'}
setB = {'alex', 'eric','Tom','Fire'}
tmp_symmetric_difference = setA.symmetric_difference(setB)
print(tmp_symmetric_difference)    #打印:{'Fire', 'Tom', 19, '777'}

  使用集合A与集合B中所有不同的的元素,覆盖更新集合A

setA = {'alex', 'eric', 19, '777'}
setB = {'alex', 'eric', 'Tom', 'Fire'}
setA.symmetric_difference_update(setB)
print(setA)  # 打印: {19, 'Fire', 'Tom', '777'}

  获取集合A与集合B中所有元素(重复元素只取一次)

setA = {'alex', 'eric', 19, '777'}
setB = {'alex', 'eric',}
tmp_union = setA.union(setB)
print(tmp_union)  # 打印: {'alex', 19, '777', 'eric'}

  判断集合B是否为集合A的子序列(B中是否与A有相同的元素),返回True/False

setA = {'alex', 'eric', 19, '777'}
setB = {'alex', 'eric',}
tmp = setB.issubset(setA)
print(tmp)  # 打印: True

  判断集合A是否为集合B的父序列(A中是否包含B中的元素),返回True/False

setA = {'alex', 'eric', 19, '777'}
setB = {'alex', 'eric',}
tmp = setA.issuperset(setB)
print(tmp)  # 打印: True

  判断集合A与集合B中是否没有重复值,返回True/False

setA = {'alex', 'eric', 19, '777'}
setB = {'alex', 'eric',}
tmp = setA.isdisjoint(setB)
print(tmp)  # 打印: False

 

深浅拷贝

  对于数字、字符串而言,赋值、深浅拷贝没有意义,因为内存指向同一内存地址。

  对于列表、元祖、字典而言,进行赋值、深浅拷贝时,内存地址的的变化是不同的。

  引入copy模块

import copy

  赋值:只是创建新的变量,还是指向相同的内存地址

tmpA = {"k1": "alex", "k2": "eric", "k3": ["alex", 123]}
tmpB = tmpA
print(id(tmpA))  # 打印: 内存地址为:4302955528
print(id(tmpB))  # 打印: 内存地址为:4302955528

  浅拷贝:只是对最外层分配新的内存地址,其余都使指向相同内存地址

import copy
tmpA = {"k1": "alex", "k2": "eric", "k3": ["alex", 123]} tmpB = copy.copy(tmpA) print(id(tmpA)) # 打印: 内存地址:4302955528 print(id(tmpB)) # 打印: 内存地址:4315444360 print(id(tmpA["k1"])) # 打印: 内存地址:4315444360 print(id(tmpB["k1"])) # 打印: 内存地址:4315444360 print(id(tmpA["k2"])) # 打印: 内存地址:4315384216 print(id(tmpB["k2"])) # 打印: 内存地址:4315384216 print(id(tmpA["k3"][0])) # 打印: 内存地址:4315382536 print(id(tmpB["k3"][0])) # 打印: 内存地址:4315382536

  深拷贝:不处理整数、字符串级别的,其余都分配新的内存地址

import copy

tmpA = {"k1": "alex", "k2": ["abc","404"], "k3": ["alex", 123]}
tmpB = copy.deepcopy(tmpA)
print(id(tmpA))  # 打印: 内存地址:4315353640
print(id(tmpB))  # 打印: 内存地址:4315353880

print(id(tmpA["k1"]))   # 打印: 内存地址:4315382536
print(id(tmpB["k1"]))   # 打印: 内存地址:4315382536
print(id(tmpA["k2"]))   # 打印: 内存地址:4315865736
print(id(tmpB["k2"]))   # 打印: 内存地址:4315865672
print(id(tmpA["k3"][0]))    # 打印: 内存地址:4315382536
print(id(tmpB["k3"][0]))    # 打印: 内存地址:4315382536

 

 三元运算符

值1 if 条件 else 值2

if 条件:

  值1

else:

  值2

tmp = "alex" if 2>1 else "eric"
print(tmp)  #打印: alex

 

 

函数 

  创建

def 函数名(形式参数):

  函数体

  return

形式参数可以是单个,可以多个,可以没有; return 可以不写,默认返回None

  调用

函数名()

函数名(实际参数)

def tmpFunc1():
    print("hello")


def tmpFunc2(args):
    print(args)


def tmpFunc3(args1, args2):
    print(args1, args2)


def tmpFunc4(*args):
    print(args)


tmpFunc1()  #打印:hello
tmpFunc2("ok")  #打印:ok
tmpFunc3("hello", "world")  #打印:hello world
tmpFunc4("hello", "world", "welcome", "to", 512) #打印:('hello', 'world', 'welcome', 'to', 512)

 

posted @ 2016-05-09 08:46  阿金study  阅读(80)  评论(0)    收藏  举报