内容简介:Python3x: set中union和update的区别
set中union和update方法都是将多个可迭代的对象合并,但是返回的结果和对初始对象的影响却不一样
# union() 方法
- a.union(b) 将集合a和集合b取并集,并将并集作为一个新的对象的返回, 但是不改变对象a和b
>>> a = {1,2,3}
>>> b = {3,4,5}
>>>
>>> c = a.union(b)
>>> print(c)
{1, 2, 3, 4, 5}
>>>
>>> a
{1, 2, 3}
>>>
>>> b
{3, 4, 5}
# update() 方法
- a.update(b) 将集合a和集合b取并集,并将结果保存在a中,对象b不改变,但是没有返回值
>>> a = {1,2,3}
>>> b = {3,4,5}
>>>
>>> c = a.update(b)
>>> print(c)
None
>>>
>>> a
{1, 2, 3, 4, 5}
>>>
>>> b
{3, 4, 5}
# 总结:
- union 为 return a | b
- update 为 a = a | b and return None
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Zen of CSS Design
Dave Shea、Molly E. Holzschlag / Peachpit Press / 2005-2-27 / USD 44.99
Proving once and for all that standards-compliant design does not equal dull design, this inspiring tome uses examples from the landmark CSS Zen Garden site as the foundation for discussions on how to......一起来看看 《The Zen of CSS Design》 这本书的介绍吧!