Python 3 教程 Python Set update() 方法

mack · 2022-03-14 15:04:24 · 热度: 15

描述

update() 方法用于修改当前集合,可以添加新的元素或集合到当前集合中,如果添加的元素在集合中已存在,则该元素只会出现一次,重复的会忽略。

语法

update() 方法语法:

set.update(set)

参数

  • set -- 必需,可以是元素或集合
  • 返回值

    无。

    实例

    合并两个集合,重复元素只会出现一次:

    实例 1

    x = {"apple", "banana", "cherry"} y = {"google", "codercto", "apple"} x.update(y) print(x)

    输出结果为:

    {'banana', 'apple', 'google', 'codercto', 'cherry'}

查看更多 Python 集合 内置函数

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册