【问题标题】:Why while updating a dictionary getting None? [duplicate]为什么在更新字典时没有? [复制]
【发布时间】:2016-07-21 05:38:03
【问题描述】:

我基本上是使用update方法合并两个字典。问题是当我合并python shell 时它可以工作,但在执行时不在文件中。

v = {'customer_id': '9000', 'customer_name': 'Apple  Inc'}
b = {"a": "b"}

print v.update(b)

上面的输出是None

但它在 shell 中工作。我的愚蠢错误是什么?谢谢你

【问题讨论】:

  • 在 shell 中工作 是什么意思?即使输出None
  • @JasonEstibeiro 检查图片
  • 那是因为你打印了v的值。 v.update(b) 的输出什么都没有。
  • 知道了。谢谢大家:)你们的耐心

标签: python dictionary


【解决方案1】:

v.update(b) 正在更新 b 就地v确实更新了,但是更新函数的结果是None,就是打印出来的。如果你做类似的事情

v.update(b)
print v

你会看到v(更新)

【讨论】:

    【解决方案2】:

    update 函数返回None。所以

    print v.update(b)  # this is printing out the return value of the update function.
    

    要打印出字典的更新值,只需再次打印字典即可

    print v  # This will print the updated value of v
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 2018-05-11
      • 2017-09-14
      • 1970-01-01
      • 2020-04-12
      • 2020-11-04
      相关资源
      最近更新 更多