>>> a={'x':1,'xx':2}
>>> for i in a:
if i=='x':
a[i]=6
>>> a
{'x': 6, 'xx': 2, 'xxx': 33}
>>> for i in a:
if i=='x':
a[i]=3
a['xxx']=33
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
for i in a:
RuntimeError: dictionary changed size during iteration
对于数据字典,如果遍历过程中对于key添加或者删除,即key的size发送改变,会报错。RuntimeError: dictionary changed size during iteration
Solve:
遍历数据字典的keys()
>>> for i in ():
if i=='x':
a[i]=3
a['xxx']=33
>>> a
{'x': 3, 'xx': 2, 'xxx': 33}
可变参数:
参数是tuple
>>> def test(*num):
print type(num)
>>> test(1)
<type 'tuple'>
>>> test(3)
<type 'tuple'>
>>> test(1,2)
<type 'tuple'>
关键字参数:
关键字参数是字典
>>> def test(**num):
for i in ():
print i
>>> test(city='bj',name=24)
city
name
>>> def test(**num):
print num
>>> test(a)
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
test(a)
TypeError: test() takes exactly 0 arguments (1 given)
>>> test('a'='5')
SyntaxError: keyword can't be an expression
>>> test(a='5')
{'a': '5'}