字典里的get()
get(key) #返回键的值,若无键key返回None
得到两个字典里各种键
a={1:"1",2:"2",3:"3"}
b={"aa":"a",2:"2","c":"c"}
print(a.keys() & b.keys()) #共有的键
print(a.keys() ^ b.keys()) #不共有的键
print(a.keys() - b.keys()) #a有b没有的键
print(a.items() & b.items()) #共有的键值
print(type(a.keys() & b.keys())) #返回为集合 class <'set'>
集合set
set_1={1,2,3,4,5}
for i in set_1: #集合不支持索引,只能用for
print(i)
变量的生存周期
i=0
def for i in range(10):
print(i) #i为局部变量
print(i) #0,i为全局变量
(不)可变类型
list、set、dict是可变类型
list_1[0]=100 #合法
#改变内容时list_1指向的位置没变
frozenset、tuple、str、numbers不可变类型
c=(1,2,3)
c[0]=5 #不合法
str同理
b=1
b=2
#合法但地址改变,实际操作为先释放1的空间然后指向2
可变类型可被函数改变,不可变类型不可
a=[1,2,3,4,5,6]
b=(1,2,3,4,5,6)
def func_1(x):
x[0]=0
def func_2(x):
x1=(1,2,3)
x=x1
print(a,'\n',b)
#[1, 2, 3, 4, 5, 6]
#(1, 2, 3, 4, 5, 6)
func_1(a)
func_2(b)
print(a,'\n',b)
#[0, 2, 3, 4, 5, 6]
#(1, 2, 3, 4, 5, 6)
修改列表
list.append(10) #在list最后添加34
函数可内定义函数
def func():
def add(x,y):
return x+y
return add(1,1)
print(func())
偏函数
import functools
def fx(x,y):
return x**y
fx_2=functools.partial(fx,2) #x固定为2
fx_3=functools.partial(fx,y=3) #y固定为3
print(fx(2,2))
print(fx_2(3))
print(fx_3(4))