基础数据类型-set
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的更多相关文章
- [.net 面向对象编程基础] (3) 基础中的基础——数据类型
[.net 面向对象编程基础] (3) 基础中的基础——数据类型 关于数据类型,这是基础中的基础. 基础..基础..基础.基本功必须要扎实. 首先,从使用电脑开始,再到编程,电脑要存储数据,就要按类型 ...
- TypeScript学习指南第一章--基础数据类型(Basic Types)
基础数据类型(Basic Types) 为了搭建应用程序,我们需要使用一些基础数据类型比如:numbers,strings,structures,boolean等等. 在TypeScript中除了Ja ...
- 【Swift】学习笔记(一)——熟知 基础数据类型,编码风格,元组,主张
自从苹果宣布swift之后,我一直想了解,他一直没有能够把它的正式学习,从今天开始,我会用我的博客来驱动swift得知,据我们了解还快. 1.定义变量和常量 var 定义变量,let定义常量. 比如 ...
- 二、Windows基础数据类型
六.Windows Data Types 简介: 6.1.这些数据类型都是C语言数据类型的再次的进行包装. 6.2.因为考虑到如果使用的是C中的基础数据类型可能无法表示,想表示的精准的含义. 6.3. ...
- java基础数据类型包装类
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- 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新 ...
- Python基础数据类型之列表和元组
一.列表 list 列表是python中的基础数据类型之一,其他语言中也有类似于列表的数据类型,比如js中叫数组,他是以[]括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型比如: li ...
- Python基础数据类型之字典
基础数据类型之字典 ps:数据类型划分:可变数据类型和不可变数据类型. 不可变数据类型:元组(tupe).布尔值(bool).整数型(int).字符串(str).不可变数据类型也称为可哈希. 可变 ...
- Python基础数据类型之集合以及其他和深浅copy
一.基础数据类型汇总补充 list 在循环一个列表时,最好不要删除列表中的元素,这样会使索引发生改变,从而报错(可以从后向前循环删除,这样不会改变未删元素的索引). 错误示范: lis = [,,, ...
- python基础二(基础数据类型)
一. 引子 1. 什么是数据 x=10,10是我们要存储的数据 2. 为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3.数据类型 数字 字符串 列表 元组 字 ...
随机推荐
- TCP/IP协议族之链路层(二)
TCP/IP学习记录,如有错误请指正,谢谢!!! TCP/IP协议族之链路层(二) 链路层是最底层协议,主要有三个目的: 1. 为IP模块发送和接收IP数据报 2. 为ARP模块发送ARP请求和接收A ...
- 安装 vue-devtools
1. github下载 vue-devtools: git clone https://github.com/vuejs/vue-devtools 2. node install 安装包 3. vi ...
- 文件后缀与Mime类型对照表
以下是一些文件后缀(扩展名)对应的MIME类型的一个对照表,方便iis中或其他服务器对相应的文件进行解析.有些文件的后缀名没有默认解析就出现上传后无法访问或者下载的问题,这个时候就要设置文件后缀对应的 ...
- CASE WHEN 批量更新
单个值: UPDATE categories SET display_order = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END WHE ...
- Scala 语法(一)
(1)基本语法 变量 val var(可变变量): 数据类型 Byte,Char,Int,Short,Long,String(字符),Float,Double,Boolean(true,flase). ...
- 第九篇:S3C2400时钟体系
S3C2400时钟体系 S3C2400时钟配置流程 该学习板使用了外部晶振12Mhz 晶振--->经过MPLL(锁相环) --->(倍频得到)FCLK--->(对FCLK分频得到)H ...
- python网络编程之协程
本节的主题是基于单线程来实现并发,即只用一个主线程(很明显可利用的cpu只有一个)情况下实现并发,为此我们需要先回顾下并发的本质:切换+保存状态 cpu正在运行一个任务,会在两种情况下切走去执行其他的 ...
- go学习笔记-反射(Reflection)
反射(Reflection) 反射是利用reflect包实现的 反射可大大提高程序的灵活性,使得interface{}有更大的发挥余地 反射使用TypeOf和ValueOf函数从接口中获取目标对象信息 ...
- Go语言入门(一)特性、安装、环境搭建、第一个程序等
Go语言是谷歌推出的一种全新的编程语言,可以在不损失应用程序性能的情况下降低代码的复杂性.谷歌首席软件工程师罗布派克(Rob Pike)说:我们之所以开发Go,是因为过去10多年间软件开发的难度令人沮 ...
- Scala学习笔记(二)——Scala基础
1. 常用数据类型 Scala与Java有着相同的常用数据类型: Byte.Short.Int.Long.Float.Double.Chat.Boolean(只有包装类型,无原始类型) Scala继承 ...