Python Set | update()
Python update() function in set adds elements from a set (passed as an argument) to the set.
Syntax : set1.update(set2)
Here set1 is the set in which set2 will be added.
Parameters : Update() method takes only a single argument. The single argument can be a set, list, tuples or a dictionary. It automatically converts into a set and adds to the set.
Return value : This method adds set2 to set1 and returns nothing.
Example of Python set update()
Example 1: Working with Python set update list
# Python program to demonstrate the
# use of update() method
list1 = [1, 2, 3]
list2 = [5, 6, 7]
list3 = [10, 11, 12]
# Lists converted to sets
set1 = set(list2)
set2 = set(list1)
# Update method
set1.update(set2)
# Print the updated set
print(set1)
# List is passed as an parameter which
# gets automatically converted to a set
set1.update(list3)
print(set1)
输出:
{1, 2, 3, 5, 6, 7}
{1, 2, 3, 5, 6, 7, 10, 11, 12}
Example 2: Python set update element in set
# Python program to demonstrate the
# use of update() method
list1 = [1, 2, 3, 4]
list2 = [1, 4, 2, 3, 5]
alphabet_set = {'a', 'b', 'c'}
# lists converted to sets
set1 = set(list2)
set2 = set(list1)
# Update method
set1.update(set2)
# Print the updated set
print(set1)
set1.update(alphabet_set)
print(set1)
输出:
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5, 'c', 'b', 'a'}
Example 3: Add elements of the dictionary to Set
number = {1, 2, 3, 4, 5}
num_Dict = {6: 'Six', 7: 'Seven', 8: 'Eight',
9: 'Nine', 10: 'Ten'}
number.update(num_Dict)
print("Updated set: ", number)
输出:
Updated set: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
转载:https://www.geeksforgeeks.org/python-set-update/
Python Set | update()的更多相关文章
- Python 字典 update() 方法
描述 Python 字典 update() 方法用于更新字典中的键/值对,可以修改存在的键对应的值,也可以添加新的键/值对到字典中. 用法与 Python dict() 函数相似. 语法 update ...
- Python MySQL Update
章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...
- python的update方法
b = {"c":0, "position":{}} b["position"]["IF"] = {} print(b) ...
- python update()
Python 字典 update() 函数把字典参数 dict2 的 key/value(键/值) 对更新到字典 dict 里. dict.update(dict2) 如果dict2里的键和dict1 ...
- python基本数据类型之集合set
一.集合的定义 set集合,是一个无序且不重复的元素集合. 集合对象是一组无序排列的可哈希的值,集合成员可以做字典中的键.集合支持用in和not in操作符检查成员,由len()内建函数得到集合的基数 ...
- Python学习笔记6-字典
定义 使用键值对, >>> person = {"name":"keven","age":15,"gender& ...
- python学习笔记6(字典)
映射:键值对的关系,键(key)映射值(value) 字典是Python唯一的映射类型 >>> phonebook = {'} >>> phonebook {'} ...
- 检测网站挂马程序(Python)
系统管理员通常从svn/git中检索代码,部署站点后通常首先会生成该站点所有文件的MD5值,如果上线后网站页面内容被篡改(如挂马)等,可以比对之前生成MD5值快速查找去那些文件被更改,为了使系统管理员 ...
- python基本数据类型——set
一.集合的定义 set集合,是一个无序且不重复的元素集合. 集合对象是一组无序排列的可哈希的值,集合成员可以做字典中的键.集合支持用in和not in操作符检查成员,由len()内建函数得到集合的基数 ...
- python多版本以及各种包管理
python多版本以及各种包管理 python 包管理 各个版本 python版本管理 由于Python有2.x和3.x两个大的版本,而且每一个工程建立所用的各种包的版本也不尽相同(如flask1.x ...
随机推荐
- Maven - 项目的JDK编译level是1.5,修改不掉??
背景 idea中的maven项目,父项目和子项目的Project Structure的language level都是1.5,怎么修改为8?尝试修改并应用后会失效,还是会自动恢复为1.5. 1.S ...
- 启动hive,报错 Name node is in safe mode.
在学习过程中,过了几天再启动虚拟机,启动hadoop后再启动别的框架会报错: Exception in thread "main" java.lang.RuntimeExcepti ...
- C#中对面向standard2.0、standard2.1的项目进行单元测试
单元测试项目的目标框架 (TargetFramework或TargetFrameworks) 不能是standard2.0或standard2.1. 这是因为.NET Standard只是一个规范,它 ...
- Python 潮流周刊#91:Python 在浏览器中的未来(摘要)
本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章.教程.开源项目.软件工具.播客和视频.热门话题等内容.愿景:帮助所有读者精进 Python 技术,并增长职 ...
- 『Plotly实战指南』--折线图绘制基础篇
在数据分析的世界中,折线图是一种不可或缺的可视化工具. 它能够清晰地展示数据随时间或其他变量的变化趋势,帮助我们快速发现数据中的模式.趋势和异常. 无论是金融市场分析.气象数据监测,还是业务增长趋势预 ...
- Golang入门:协程(goroutine)
goroutine goroutine 是 Go 的并发模型的核心概念.为了理解 goroutine,我们来定义几个术语.第一个是进程.进程是程序的实例,由计算机的操作系统运行.操作系统将一些资源(如 ...
- 探秘Transformer系列之(15)--- 采样和输出
探秘Transformer系列之(15)--- 采样和输出 目录 探秘Transformer系列之(15)--- 采样和输出 0x00 概述 0x01 Generator 1.1 Linear 1.2 ...
- 通过百度地图 API V2.0 版本,进行地图坐标系转换
注意 先阅读参考链接 瞭月 的文章,再阅读本文. 其中,请求参数中 model 的含义: amap/tencent - 即:GCJ02 火星坐标系,由中国国家测绘局制订的地理信息系统的坐标系统. 由 ...
- 史上最全EffectiveJava总结(二)
方法 49.检查参数的有效性 每次编写方法或构造函数时,都应该考虑参数存在哪些限制,并在文档中记录下来,然后在方法的开头显式地检查. 如果没有在方法开头就验证参数,可能会违反故障原子性.因为方法可能会 ...
- Ubuntu下Ibus和fcitx的奇葩问题
最近装了个Ubuntu,捉鸡的输入法令人哀伤. 于是乎,卸载了自带Ibus和fcitx出现各种问题,留爪. 首先,卸载了Ibus,会出现的问题如下: 注销/重启系统有问题 系统设置菜单选项缺失 -&g ...