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. Hibernate基础入门

    Hibernate是一个开放源代码的对象关系映射框架,它将POJO与数据库表之间建立映射关系.是全自动的ORM框架,可以自动生成SQL语句并自动执行.它对JDBC进行了非常轻量级的封装,使程序员可以随 ...

  2. CentOS 7.x eth0

    1:Test Environment [root@linux-node1 ~]# cat /etc/redhat-release Red Hat Enterprise Linux Server rel ...

  3. ASP.NET MVC4.0 后台获取不大前台传来的file

    <td>选择图片</td> <td> <input type="file" id="uploadImg" name=& ...

  4. table表单制作个人简历

    应用table表单,编程个人简历表单,同时运用了跨行rowspan和跨列colspan. <!DOCTYPE html> <html> <head> <met ...

  5. 3. HTML中的容器标签

    什么是容器标签?在HTML开发中我们常常会使用一类标签作为容器放置一些内容,我们把这类标签称之为容器标签,可以作为容器标签的包括列表标签.表格标签.框架标签.布局标签,在这里我们就来总结下这些内容. ...

  6. Delphi Math单元函数

    本文转至http://blog.sina.com.cn/s/blog_976ba8a501010vvf.html 这个单元包含高性能的算术.三角.对数.统计和金融方面的计算及FPU程序函数用于补充De ...

  7. WIN10远程连接winserver2012 r2,连接失败

    背景:2012开启远程的时候,默认是勾选“仅允许运行使用网络级别身份验证的远程桌面的计算机连接”,这个选项据说比较安全,但是用win10远程的时候就报错,函数不受支持,最后通过修改win10的配置得以 ...

  8. log4j配置输出日志文件

    在测试程序时,有时候运行一次可能需要很久,把日志文件保存下来是很有必要的,本文给出了scala程序输出日志文件的方式,同时使用本人的另一篇博客中介绍的将log4j.properties放到程序jar包 ...

  9. 面试乐融集团Python开发工程师有感

    这是笔者第一次面试,,乐融集团位于朝阳区朝阳公园的乐融大厦.是下午两点的笔面试,笔者是一点半到的,然后在里面等了会,开始笔试 笔试题并不是太难,就是考的比较宽,因为笔者是校招,所以笔试题出来了数据结构 ...

  10. python三大神器之装饰器

    装饰器的形成过程 假如你要写一个计算函数执行时间的函数,代码如下: import time def func1(): print('in func1') def timer(func): def in ...