Set是无序不重复元素的序列,基本功能是删除重复元素和测试成员关系,

创建一个集合可以用set()或者({}),但是创建一个空集合只能用set():

s1 = set()
print("s1 is", type(s1))
s2 = ({1, 2, 3})
print("s2 is ", type(s2))
s3 = ({})
print("s3 is", type(s3))
s1 is <class 'set'>
s2 is <class 'set'>
s3 is <class 'dict'>

View Result

功能,删除序列里重复元素,测试成员关系,集合运算:

 #删除序列里重复元素
fruits1 = set({"Apple", "Orange", "Pear", "Apple"})
print("fruits1:", fruits1) #成员测试
if 'Pear' in fruits1:
print("Pear is in set")
else:
print("Pear is not in set") #成员运算
fruits2 = set({"Apple", "Banana", "Strawberry"})
print(fruits1 - fruits2) #在fruits1中但不再fruits2中
print(fruits1 | fruits2) #在fruits1或fruits2中
print(fruits1 & fruits2) #在fruits1且在fruits2中
print(fruits1 ^ fruits2) #在fruits1或fruits2中,但不同时在两个集合中

Code

 fruits1: {'Apple', 'Orange', 'Pear'}
Pear is in set
{'Orange', 'Pear'}
{'Apple', 'Orange', 'Strawberry', 'Banana', 'Pear'}
{'Apple'}
{'Strawberry', 'Banana', 'Orange', 'Pear'}

Result

Set常用方法有add(), difference(),intersection()等:

 #set常用方法
fruits2.add("Watermelon")
print("fruits2:", fruits2)
print("Fruits in fruits2 but not in fruits1 are:", fruits2.difference(fruits1)) #fruits2中有fruits1中没有
print("Fruits in fruits1 but not in fruits2 are:", fruits1.difference(fruits2)) #fruits1中有fruits2中没有
print("Fruits in fruits1 and fruits2 are:", fruits2.intersection(fruits1)) #fruits1,fruits2的交集

Code

 fruits2: {'Strawberry', 'Banana', 'Apple', 'Watermelon'}
Fruits in fruits2 but not in fruits1 are: {'Strawberry', 'Banana', 'Watermelon'}
Fruits in fruits1 but not in fruits2 are: {'Pear', 'Orange'}
Fruits in fruits1 and fruits2 are: {'Apple'}

Result

Set类的定义-其他不常用方法查询:

 class set(object):
"""
set() -> new empty set object
set(iterable) -> new set object Build an unordered collection of unique elements.
"""
def add(self, *args, **kwargs): # real signature unknown
"""
Add an element to a set. This has no effect if the element is already present.
"""
pass def clear(self, *args, **kwargs): # real signature unknown
""" Remove all elements from this set. """
pass def copy(self, *args, **kwargs): # real signature unknown
""" Return a shallow copy of a set. """
pass def difference(self, *args, **kwargs): # real signature unknown
"""
Return the difference of two or more sets as a new set. (i.e. all elements that are in this set but not the others.)
"""
pass def difference_update(self, *args, **kwargs): # real signature unknown
""" Remove all elements of another set from this set. """
pass def discard(self, *args, **kwargs): # real signature unknown
"""
Remove an element from a set if it is a member. If the element is not a member, do nothing.
"""
pass def intersection(self, *args, **kwargs): # real signature unknown
"""
Return the intersection of two sets as a new set. (i.e. all elements that are in both sets.)
"""
pass def intersection_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the intersection of itself and another. """
pass def isdisjoint(self, *args, **kwargs): # real signature unknown
""" Return True if two sets have a null intersection. """
pass def issubset(self, *args, **kwargs): # real signature unknown
""" Report whether another set contains this set. """
pass def issuperset(self, *args, **kwargs): # real signature unknown
""" Report whether this set contains another set. """
pass def pop(self, *args, **kwargs): # real signature unknown
"""
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
"""
pass def remove(self, *args, **kwargs): # real signature unknown
"""
Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError.
"""
pass def symmetric_difference(self, *args, **kwargs): # real signature unknown
"""
Return the symmetric difference of two sets as a new set. (i.e. all elements that are in exactly one of the sets.)
"""
pass def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the symmetric difference of itself and another. """
pass def union(self, *args, **kwargs): # real signature unknown
"""
Return the union of sets as a new set. (i.e. all elements that are in either set.)
"""
pass def update(self, *args, **kwargs): # real signature unknown
""" Update a set with the union of itself and others. """
pass

Class Set

基础数据类型-set的更多相关文章

  1. [.net 面向对象编程基础] (3) 基础中的基础——数据类型

    [.net 面向对象编程基础] (3) 基础中的基础——数据类型 关于数据类型,这是基础中的基础. 基础..基础..基础.基本功必须要扎实. 首先,从使用电脑开始,再到编程,电脑要存储数据,就要按类型 ...

  2. TypeScript学习指南第一章--基础数据类型(Basic Types)

    基础数据类型(Basic Types) 为了搭建应用程序,我们需要使用一些基础数据类型比如:numbers,strings,structures,boolean等等. 在TypeScript中除了Ja ...

  3. 【Swift】学习笔记(一)——熟知 基础数据类型,编码风格,元组,主张

    自从苹果宣布swift之后,我一直想了解,他一直没有能够把它的正式学习,从今天开始,我会用我的博客来驱动swift得知,据我们了解还快. 1.定义变量和常量 var  定义变量,let定义常量. 比如 ...

  4. 二、Windows基础数据类型

    六.Windows Data Types 简介: 6.1.这些数据类型都是C语言数据类型的再次的进行包装. 6.2.因为考虑到如果使用的是C中的基础数据类型可能无法表示,想表示的精准的含义. 6.3. ...

  5. java基础数据类型包装类

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  6. java.lang基础数据类型boolean、char、byte、short、int、long、float、double (JDK1.8)

    java.lang.Boolean public static int hashCode(boolean value) { return value ? 1231 : 1237; } JDK 1.8新 ...

  7. Python基础数据类型之列表和元组

    一.列表   list 列表是python中的基础数据类型之一,其他语言中也有类似于列表的数据类型,比如js中叫数组,他是以[]括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型比如: li ...

  8. Python基础数据类型之字典

      基础数据类型之字典 ps:数据类型划分:可变数据类型和不可变数据类型. 不可变数据类型:元组(tupe).布尔值(bool).整数型(int).字符串(str).不可变数据类型也称为可哈希. 可变 ...

  9. Python基础数据类型之集合以及其他和深浅copy

    一.基础数据类型汇总补充 list  在循环一个列表时,最好不要删除列表中的元素,这样会使索引发生改变,从而报错(可以从后向前循环删除,这样不会改变未删元素的索引). 错误示范: lis = [,,, ...

  10. python基础二(基础数据类型)

    一. 引子 1. 什么是数据 x=10,10是我们要存储的数据 2. 为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3.数据类型 数字 字符串 列表 元组 字 ...

随机推荐

  1. iOS之改变UIAlertViewController字体的颜色

    NSString *message = @"请确认信息是否正确?"; NSString *title = @"提示"; UIAlertController *a ...

  2. 利用MyFlash闪回丢失数据

          MyFlash is an open source tool released by Meituan-Dianping which can be used to flashback MyS ...

  3. docker build

    nginx Docfile ----------------------- FROM centos   MAINTAINER daniel   RUN yum install -y wget RUN ...

  4. ORA-00911: 无效字符 问题和解决

    1.原本java插入数据库表数据写法是这样的 String sql = "INSERT INTO AAA (id1,id2,id3,id4) VALUES ('1','2','3','4') ...

  5. 六个比较好用的php数组Array函数

    1. array_column 返回输入数组中某个单一列的值.2. array_filter 用回调函数过滤数组中的元素.3. array_map 将用户自定义函数作用到给定数组的每个值上,返回新的值 ...

  6. 使用百度编辑器--ueditor,后台接收提交编辑的内容,HTML不见了, 赋值不了,赋值之后,html暴露出来了??

    1.提交编辑好的内容, 后台post 接收发现 html 不见了,这个时候也许就是转义的问题, 既可以试试 $content = htmlspecialchars(stripslashes(input ...

  7. 关于一个flask的服务接口实战(flask-migrate,flask-script,SQLAlchemy)

    前言 最近接到一个接收前端请求的需求,需要使用python编写,之前没有写过python,很多技术没有用过,在这里做一个学习记录,如有错误,请不了赐教. Flask Api文档管理 使用Falsk A ...

  8. Hadoop1.0 与Hadoop2.0

    Hadoop1.0的局限-MapReduce •扩展性 –集群最大节点数–4000 –最大并发任务数–40000 (当 map-reduce job 非常多的时候,会造成很大的内存开销,潜在来说,也增 ...

  9. 第一篇:百问网ubuntu安装注意事项和部分配置

    目录 一.开启虚拟化技术 二.ubuntu部分设置 一.开启虚拟化技术 ​ 64位机,需要使用cpu-z.SecurAble软件来检查:CPU是否支持VT虚拟化技术 cpu-z使用(软件) 第一步:以 ...

  10. python学习笔记:第14天 内置函数补充和递归

    一.匿名函数 匿名函数主要是为了解决一些简单需求而设计的一种函数,匿名函数的语法为: lambda 形参: 返回值 先来看一个例子: # 计算n的n次方 In[2]: lst = lambda n: ...