python字典更新信息_Python字典中的更新方法

不同之处在于第二种方法不起作用:

>>> {}.update(1, 2)

Traceback (most recent call last):

File "", line 1, in

TypeError: update expected at most 1 arguments, got 2

dict.update()期望找到一个可重复的键值对,关键字参数或另一个字典:

Update the dictionary with the key/value pairs from other, overwriting existing keys. Return None.

update() accepts either another dictionary object or an iterable of key/value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key/value pairs: d.update(red=1, blue=2).

map()是一种通过将第二个参数(和后续参数)的元素应用于第一个参数(必须是可调用的)来生成序列的内置方法。除非您的键对象是可调用的,并且值对象是序列,否则您的第一个方法也将失败。

工作地图()应用程序演示:

>>> def key(v):

... return (v, v)

...

>>> value = range(3)

>>> map(key, value)

[(0, 0), (1, 1), (2, 2)]

>>> product = {}

>>> product.update(map(key, value))

>>> product

{0: 0, 1: 1, 2: 2}

这里map()只是生成键值对,它满足dict.update()期望值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值