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. js浮点型,整型操作方法汇总(进行中)

    浮点数操作方法如下: 1. Math.ceil()用作向上取整.(ceil 天花板) 2. Math.floor()用作向下取整. (floor 地板) (js 中取整底层原理是位运算的取反~运算,运 ...

  2. LightOJ 1203--Guarding Bananas(二维凸包+内角计算)

    1203 - Guarding Bananas    PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 M ...

  3. (Linux学习笔记一:压缩)[20180209]

    学习笔记一:压缩 2015年2月5日 上午 10:23 压缩命令 压缩文件的扩展名大多是*.tar.*.tar.gz.*.tgz.*.gz.*.Z.*.bz2 常见的压缩命令gzip与bzip2,其中 ...

  4. linux下的学习之路下的小困难

    centos下源码安装python3wget --no-check-certificate https://www.python.org/ftp/python/3.6.2/Python-3.6.2.t ...

  5. 微信小程序车牌号码模拟键盘输入

    微信小程序车牌号码模拟键盘输入练习, 未经允许,禁止转载,抄袭,如需借鉴参考等,请附上该文章连接. 相关资料参考:https://blog.csdn.net/littlerboss/article/d ...

  6. 在WIN7下安装运行mongodb

    1).下载MongoDB http://downloads.mongodb.org/win32/mongodb-win32-i386-2.4.5.zip 下载Windows 32-bit版本并解压缩, ...

  7. KEIL MDK-ARM Version 5.26正式版开发工具下载

    Keil MDK最新版本已经出来啦,ARM开发工具MDK-ARM Version 5.26地址:http://www.myir-tech.com/soft.asp?id=1141,需要的可以去下载哦 ...

  8. Python学习3——变量如何存储数据

    数值类型:包括整型.浮点型 变量名字代表的是存储地址. num01 = 100 print(id(num01)) #输出变量num01存储的内存地址,输出的是十进制值 num02 = num01 pr ...

  9. 北京Uber优步司机奖励政策(1月5日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  10. Android ObjectOutputStream Serializable引发的血案

    遇到一个问题 安装后第二次进app,闪退 重现步骤 [前置条件] 打包分支:dev_7.13 手机:vivo NEX 8.1.0 [步骤] 安装三星app----同意用户协议进入书城---连续点击ba ...