RuntimeError: dictionary changed size during iteration,可变参数,关键字参数

>>> 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 a.keys():
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 num.keys():
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'}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值