python函数字典解包 python中字典的函数

python字典操作函数字典是一种通过名字或者关键字引用python函数字典解包的得数据结构python函数字典解包,其键可以是数字、字符串、元组python函数字典解包 , 这种结构类型也称之为映射 。字典类型是Python中唯一内建的映射类型,基本的操作包括如下:
(1)len():返回字典中键—值对的数量;
(2)d[k]:返回关键字对于的值;
(3)d[k]=v:将值关联到键值k上;
(4)del d[k]:删除键值为k的项;
(5)key in d:键值key是否在d中,是返回True,否则返回False 。
(6)clear函数:清除字典中的所有项
(7)copy函数:返回一个具有相同键值的新字典;deepcopy()函数使用深复制,复制其包含所有的值,这个方法可以解决由于副本修改而使原始字典也变化的问题
(8)fromkeys函数:使用给定的键建立新的字典 , 键默认对应的值为None
(9)get函数:访问字典成员
(10)has_key函数:检查字典中是否含有给出的键
(11)items和iteritems函数:items将所有的字典项以列表方式返回,列表中项来自(键,值),iteritems与items作用相似,但是返回的是一个迭代器对象而不是列表
(12)keys和iterkeys:keys将字典中的键以列表形式返回,iterkeys返回键的迭代器
(13)pop函数:删除字典中对应的键
(14)popitem函数:移出字典中的项
(15)setdefault函数:类似于get方法,获取与给定键相关联的值,也可以在字典中不包含给定键的情况下设定相应的键值
(16)update函数:用一个字典更新另外一个字典
(17) values和itervalues函数:values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重复的元素
一、字典的创建
1.1 直接创建字典
d={'one':1,'two':2,'three':3}
printd
printd['two']
printd['three']
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three':3,'two':2,'one':1}
1.2 通过dict创建字典
# _*_ coding:utf-8 _*_
items=[('one',1),('two',2),('three',3),('four',4)]
printu'items中的内容:'
printitems
printu'利用dict创建字典,输出字典内容:'
d=dict(items)
printd
printu'查询字典中的内容:'
printd['one']
printd['three']
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
items中的内容:
[('one',1), ('two',2), ('three',3), ('four',4)]
利用dict创建字典,输出字典内容:
{'four':4,'three':3,'two':2,'one':1}
查询字典中的内容:
或者通过关键字创建字典
# _*_ coding:utf-8 _*_
d=dict(one=1,two=2,three=3)
printu'输出字典内容:'
printd
printu'查询字典中的内容:'
printd['one']
printd['three']
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
输出字典内容:
{'three':3,'two':2,'one':1}
查询字典中的内容:
二、字典的格式化字符串
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3,'four':4}
printd
print"three is %(three)s."%d
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'four':4,'three':3,'two':2,'one':1}
threeis3.
三、字典方法
3.1 clear函数:清除字典中的所有项
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3,'four':4}
printd
d.clear()
printd
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'four':4,'three':3,'two':2,'one':1}
{}
请看下面两个例子
3.1.1
# _*_ coding:utf-8 _*_
d={}
dd=d
d['one']=1
d['two']=2
printdd
d={}
printd
printdd
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'two':2,'one':1}
{}
{'two':2,'one':1}
3.1.2
# _*_ coding:utf-8 _*_
d={}
dd=d
d['one']=1
d['two']=2
printdd
d.clear()
printd
printdd
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'two':2,'one':1}
{}
{}
3.1.2与3.1.1唯一不同的是在对字典d的清空处理上,3.1.1将d关联到一个新的空字典上,这种方式对字典dd是没有影响的,所以在字典d被置空后,字典dd里面的值仍旧没有变化 。但是在3.1.2中clear方法清空字典d中的内容,clear是一个原地操作的方法,使得d中的内容全部被置空,这样dd所指向的空间也被置空 。
3.2 copy函数:返回一个具有相同键值的新字典
# _*_ coding:utf-8 _*_
x={'one':1,'two':2,'three':3,'test':['a','b','c']}
printu'初始X字典:'
printx
printu'X复制到Y:'
y=x.copy()
printu'Y字典:'
printy
y['three']=33
printu'修改Y中的值,观察输出:'
printy
printx
printu'删除Y中的值,观察输出'
y['test'].remove('c')
printy
printx
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
初始X字典:
{'test': ['a','b','c'],'three':3,'two':2,'one':1}
X复制到Y:
Y字典:
{'test': ['a','b','c'],'one':1,'three':3,'two':2}
修改Y中的值,观察输出:
{'test': ['a','b','c'],'one':1,'three':33,'two':2}
{'test': ['a','b','c'],'three':3,'two':2,'one':1}
删除Y中的值,观察输出
{'test': ['a','b'],'one':1,'three':33,'two':2}
{'test': ['a','b'],'three':3,'two':2,'one':1}
注:在复制的副本中对值进行替换后 , 对原来的字典不产生影响 , 但是如果修改python函数字典解包了副本,原始的字典也会被修改 。deepcopy函数使用深复制,复制其包含所有的值,这个方法可以解决由于副本修改而使原始字典也变化的问题 。
# _*_ coding:utf-8 _*_
fromcopyimportdeepcopy
x={}
x['test']=['a','b','c','d']
y=x.copy()
z=deepcopy(x)
printu'输出:'
printy
printz
printu'修改后输出:'
x['test'].append('e')
printy
printz
运算输出:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
输出:
{'test': ['a','b','c','d']}
{'test': ['a','b','c','d']}
修改后输出:
{'test': ['a','b','c','d','e']}
{'test': ['a','b','c','d']}
3.3 fromkeys函数:使用给定的键建立新的字典,键默认对应的值为None
# _*_ coding:utf-8 _*_
d=dict.fromkeys(['one','two','three'])
printd
运算输出:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three':None,'two':None,'one':None}
或者指定默认的对应值
# _*_ coding:utf-8 _*_
d=dict.fromkeys(['one','two','three'],'unknow')
printd
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three':'unknow','two':'unknow','one':'unknow'}
3.4 get函数:访问字典成员
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
printd
printd.get('one')
printd.get('four')
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three':3,'two':2,'one':1}
1
None
注:get函数可以访问字典中不存在的键,当该键不存在是返回None
3.5 has_key函数:检查字典中是否含有给出的键
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
printd
printd.has_key('one')
printd.has_key('four')
【python函数字典解包 python中字典的函数】 运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three':3,'two':2,'one':1}
True
False
3.6 items和iteritems函数:items将所有的字典项以列表方式返回,列表中项来自(键,值),iteritems与items作用相似 , 但是返回的是一个迭代器对象而不是列表
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
printd
list=d.items()
forkey,valueinlist:
printkey,':',value
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three':3,'two':2,'one':1}
three :3
two :2
one :1
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
printd
it=d.iteritems()
fork,vinit:
print"d[%s]="%k,v
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three':3,'two':2,'one':1}
d[three]=3
d[two]=2
d[one]=1
3.7 keys和iterkeys:keys将字典中的键以列表形式返回 , iterkeys返回键的迭代器
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
printd
printu'keys方法:'
list=d.keys()
printlist
printu'\niterkeys方法:'
it=d.iterkeys()
forxinit:
printx
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three':3,'two':2,'one':1}
keys方法:
['three','two','one']
iterkeys方法:
three
two
one
3.8 pop函数:删除字典中对应的键
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
printd
d.pop('one')
printd
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three':3,'two':2,'one':1}
{'three':3,'two':2}
3.9 popitem函数:移出字典中的项
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
printd
d.popitem()
printd
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three':3,'two':2,'one':1}
{'two':2,'one':1}
3.10 setdefault函数:类似于get方法,获取与给定键相关联的值,也可以在字典中不包含给定键的情况下设定相应的键值
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
printd
printd.setdefault('one',1)
printd.setdefault('four',4)
printd
运算结果:
{'three':3,'two':2,'one':1}
{'four':4,'three':3,'two':2,'one':1}
3.11 update函数:用一个字典更新另外一个字典
# _*_ coding:utf-8 _*_
d={
'one':123,
'two':2,
'three':3
}
printd
x={'one':1}
d.update(x)
printd
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three':3,'two':2,'one':123}
{'three':3,'two':2,'one':1}
3.12 values和itervalues函数:values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重复的元素
# _*_ coding:utf-8 _*_
d={
'one':123,
'two':2,
'three':3,
'test':2
}
printd.values()
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
[2,3,2,123]
[小白自学python]如何理解与应用装包与解包?您说的装包是用元组,解包是用比如a, b, c = test_list (假设test_list为[1, 2, 3])
python中函数的作用Python 函数定义以及参数传递
1.函数定义
#形如def func(args...):
doSomething123
以关键字def 开头python函数字典解包,后面是函数名和参数下面是函数处理过程 。
举例:
def add( a, b ):
return a b12
参数可以设定默认值python函数字典解包,如:
def add( a, b=10 ): #注意:默认值参数只会运算一次
return a b12
默认值参数只会运算一次是什么意思python函数字典解包?
def func( a, b=[] ): #bpython函数字典解包的默认值指向一个空的列表python函数字典解包,每次不带默认值都会指向这块内存
b.append(a)return b
print(func(1))#向默认的空列表里加入元素1 ,默认列表里已经是[1]print(func(2))#向默认的列表里加入元素2,默认列表里已经是[1,2]print(func(3,[]))#向b指向的空列表里加入元素1,默认列表里还是[1,2]print(func(4))#向默认的列表里加入元素4,默认列表里已经是[1,2,4]'''
结果:
[1]
[1, 2]
[3]
[1, 2, 4]
'''12345678910111213141516
这下明白为什么默认参数只计算一次了吧,函数参数不传递时默认值总是指向固定的内存空间 , 就是第一次计算的空间 。
2.参数传递
def func(a, b):
print('a=%d, b=%d' % (a,b) )12
在使用函数时可以如下方式,结果都是相同的
func(10,20) #不使用参数名 , 需要按参数顺序传递func(a=10,b=20) #使用参数名可以不按顺序传递func(b=20,a=10)#结果:a=10, b=20a=10, b=20a=10, b=201234567
如果函数定义形式如下方式:
def func(*args): #这种定义会把传递的参数包成元组
print(args,type(args))
func(10,20)#结果:#(10, 20) class 'tuple'1234567
举一个和上述过程相反的例子:
def func(a,b):
print('a=%d, b=%d' % (a,b) )
a = (10, 20)
func(*a) #在调用函数使用`*`则会把元组解包成单个变量按顺序传入函数#结果:a=10, b=20123456
总结:*号在定义函数参数时 , 传入函数的参数会转换成元组,如果 *号在调用时则会把元组解包成单个元素 。
另一种定义:
def func(**kw):#使用**定义参数会把传入参数包装成字典dict
print(kw, type(kw) )
func(a=10,b=20)#这种函数在使用时必须指定参数值,使用key=value这种形式#结果:{'b': 20, 'a': 10} class 'dict'12345
相反的例子:
def func(a,b):
print('a=%d, b=%d' % (a,b) )
d = {'a':10, 'b':20 }
func(**d) #在调用时使用**会把字典解包成变量传入函数 。12345
def func(*args, **kw):#这种形式的定义代表可以接受任意类型的参数
print(args,kw )12
总结:**号在定义函数参数时,传入函数的参数会转换成字典,如果 **号在调用时则会把字典解包成单个元素 。
lambda表达式
lambda表达式就是一种简单的函数
形如 f = lambda 参数1,参数2: 返回的计算值
例如:
add = lambda x,y: x y
print(add(1,2))'''
结果:3
'''12345
python 随手记 (4) python 函数中*(star/asterisk)和**的使用 1、函数定义时
?。褂玫ジ?* 会将所有的参数 , 放入一个元组(tuple)供函数使用 。
?。褂昧礁?**将所有的关键字参数(键-值对形式),放入一个字典(dict)供函数使用 。
2.函数调用时
?。趌ist,tuple,set前加一个星号会把容器中的所有元素解包(unpack)变成位置参数 。
?。赿ict前加一个星号会把字典的键变成位置参数 。
?。赿ict前加两个星号会把字典的键值对变成关键字参数 。
1、* 的具体使用 。位置参数和可变参数的灵活使用
1.1 在函数定义时候,将所有的位置参数放入一个元祖中
1.2在函数调用时使用* , 将list、tuple、set,解包成位置参数 。
2、**的具体使用 。关键字参数和可变参数的具体使用 。
2.1 在函数定义时
3、 和 * 在函数中一起使用
注意,在*arg之后,函数只接收关键字参数
python函数字典解包的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于python中字典的函数、python函数字典解包的信息别忘了在本站进行查找喔 。

    推荐阅读