字符串和数字、赋值 id一样

import copy   #提供拷贝功能
copy.copy() #原来的和现在的一起修改,不用修改时用浅copy,节省内存,复制最外层
copy.deepcopy() #只修改现在的,复制所有,除最内层

2.集合

1).无序且不重复的集合
2).访问速度快
3).天生解决重复问题
s=set()  #创建空集合专用
s={11,22,33,44}

s1=set(['alex','eric','tony'])  #转换:可以是元组、列表、字符串

s1.add('alll')                  #增加元素,原集合
s1.update()          #可增加多个元素

s2=s1.difference(['alex','eric'])#生成新的集合,删除相同元素,不改变原集合A-(AnB)

print(s2)
s1.difference_update(['alex','eric'])    #删除相同元素,修改原集合
s.discard()  #移除指定元素


s2.pop()  #1.随机删除一个元素2.把元素赋值给ret,有返回值
print(s2)
s1.remove('alll') #移除指定的,
print(s1)

old=set(old_dict.keys())
new=set(new_dict.keys())
updata_set=old.intersection(new)   #取交集,并更新自己AnB
ret=old.isdisjoint(new) #有交集是False,没交集是True
old.issubset(new) #是否是子集
old.issuperset(new)  #是否是父集
old.union(new)  #取并集

deleat_set= old.symmetric_difference(updata_set)   # 并-交,拿出所有不一样的 AuB-AnB

add_set=new.symmetric_difference(updata_set)       #并-交,

3.三元运算

用于简单的if语句

# r=1 if True else False   # r=值1 if 条件 else 值2
# print(r)

4.函数的返回值

def mail(user):
    ret=True
    try:
        n=m #失败
    except Exception:
        ret=user
    return ret    #成功返回123,失败返回456 #返回值,结束子程序
ret=mail('xxx')
print(ret)

返回给mail()

4.1两个参数

def show(rr,yy=999):  #默认参数yy=999  #默认参数必须放在最后
    print(rr,yy)
show(11,22)  ##指定参数show(yy=11,22)

 

4.2动态参数

#动态参数  *arg 多个参数,默认输出是元组
#        **arg 多个参数,默认输出是字典  show(n1=11,n2=22)   传参数要有key value
# *arg,**arg      show(11,22,33,n1=11,n2=22)前几个放进元组,后几个放进字典1.一个星在前,两个星放后面2.执行时按顺序
#                l=[11,22,33]  d={'n1':88,'n2':'hh'}     show(*l,**d)
#动态参数实现字符串格式化
'''
s1='{0} is {1}'
l=['kk','ll']
result=s1.format(*l)
'''
'''
s1='{name} is {acter}'
l={'name':'kk','ll':'hh'}
result=s1.format(**l)
'''

 4.3函数传参数

#函数传参,传引用。java,c#在形参前加ref或out,不加相当于再创建一份值

def fun(args):
    args.append(123)
    
li=[11,22]
fun(li)
print(li)

 

5.练习

5.1集合

# 数据库中原有
old_dict = {
    "#1":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 },
    "#2":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 }
    "#3":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 }
}

# cmdb 新汇报的数据
new_dict = {
    "#1":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 800},
    "#3":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 }
    "#4":{ 'hostname':'c2', 'cpu_count': 2, 'mem_capicity': 80 }
}
m=[]
n=[]
for i in old_dict:
    m.append(i)
for i in new_dict:
    n.append(i)

old_set=set(m)
new_set=set(n)
#更新
updata_set=old_set.intersection(new_dict)   #取交集
#删除
del_set=old_set.symmetric_difference(updata_set)
#新建
new=new_set.symmetric_difference(updata_set)
View Code

相关文章:

  • 2021-08-11
  • 2022-12-23
  • 2021-07-23
  • 2021-06-18
  • 2022-02-08
  • 2021-08-30
  • 2022-12-23
  • 2021-11-07
猜你喜欢
  • 2021-12-08
  • 2022-12-23
  • 2022-12-23
  • 2019-10-08
  • 2021-12-07
  • 2021-04-05
  • 2021-11-04
相关资源
相似解决方案