Python集合的常用方法,,注释很详细colle
Python集合的常用方法,,注释很详细colle
注释很详细
collection={1,2,"apple","orange","cat"}# 查看一个数是否在集合中 inprint("Tom 是否在集合中:","Tome" in collection)print("apple 是否在集合中:","apple" in collection)# 集合初始化时用set() pets={}是代表初始化了一个字典集pets=set()# 集合添加元素用add()方法,删除元素用discard()pets.add("dog")pets.add("cat")pets.add("bird")pets.add("zebra")print(pets)pets.discard("zebra")print(pets)# 集合的 与 或 非print("并集:",pets | collection )print("交集:",pets & collection)print("异或:",pets - collection)# 将一个字典集的keys转为集合,用set(字典集名)dict1={"cat":1,"dog":2,"bird":3}print(set(dict1))# 向一个集合中添加另一个集合用update()方法collection.update([12,13,14])print(collection)
for i in set("apple"): print(i,end=",")print()for j in "apple": print(j,end=",")
输出结果”
E:\Program_Files\PythonAll\workspace\MyLearnProject\venv\Scripts\python.exe E:/Program_Files/PythonAll/workspace/MyLearnProject/pythonCode/collection/collectionFunc.pyTom 是否在集合中: Falseapple 是否在集合中: True{‘dog‘, ‘cat‘, ‘bird‘, ‘zebra‘}{‘dog‘, ‘cat‘, ‘bird‘}并集: {1, 2, ‘bird‘, ‘apple‘, ‘orange‘, ‘dog‘, ‘cat‘}交集: {‘cat‘}异或: {‘dog‘, ‘bird‘}{‘dog‘, ‘cat‘, ‘bird‘}{1, 2, 12, 13, 14, ‘apple‘, ‘cat‘, ‘orange‘}l,p,e,a,a,p,p,l,e,Process finished with exit code 0
Python集合的常用方法
相关内容
- 12、python基础学习-for循环,12python基础-for, 1 #__auth
- Python3 求最大/小值及索引值 Numpy,,Python3 求最
- Python文件处理,,1. 文件的操作1.
- python版本原因导致不能安装和卸载软件,python卸载,下面
- CentOS7 安装 Python3.6,centos7python3.6,先看编译安装,编译
- Python中模拟超市抹零结账行为代码,,在IDLE中创建一个
- python 实现获取电脑IP、主机名、Mac地址,pythonmac, 1 im
- Python—MySQL线程池,,python实现版本
- Python Deque 模块,,Deque模块是Py
- threading模块,python下的多线程,threadingpython,一、GIL全局
评论关闭