python基础数据类型--集合(set)

集合是一个数学概念由一个或多个确定的元素所构成的整体叫做集合

集合中的三个特征

1.确定性(元素必须死可hash)

2.互异性(去重)

3.无序性(集合中的元素没有先后之分)如集合{1,2,3}和集合{2,3,1}算作一个集合

注意  集合存在的意义就是去重和关系运算

一、集合的创建

set1 = {1,2,3}

set2 = set({1,2,3})

单个元素的增加add(),     add的作用相当于列表中的append

序列的增加:update(),    update类似于extend方法,update方法可以支持同时传入多个参数

a = {1,2}
a.update([3,4],[1,2,7]) #迭代加 去重
>>>>a
{1, 2, 3, 4, 7}
a.update('hello')    #迭代加
>>>>a
{1, 2, 3, 4, 7, 'h', 'o', 'e', 'l'}

a.add('hello') #追加
>>>a
{1, 2, 3, 4, 7, 'h', 'o', 'hello', 'e', 'l'}

  

删除

集合删除单个元素有两种方法:

元素不在原集合里

set.discard(x)  不会抛出异常

set.remove(x)  会抛出keyError错误

a = {1,2,3,4}

a.discard(1)
print(a)
a.discard(1) #不会报错
print(a)
a.remove(1) #报错
print(a) >>>> {2, 3, 4}
{2, 3, 4}
Traceback (most recent call last):
File "D:/untitled/假期/2018-2-9/基础知识五--集合.py", line 19, in <module>
a.remove(1)
KeyError: 1

pop():由于集合是无序的,pop返回的结果不能确定,且当集合为空时调用pop会抛出KeyError错误,

clear(): 清空集合

a = {3,'a',2.1,1}
a.pop()
a.pop() #删除是无序的
a.clear() #清空
print(a)
a.pop() #报错
print(a) >>>>
set()
Traceback (most recent call last):
File "D:/untitled/假期/2018-2-9/基础知识五--集合.py", line 28, in <module>
a.pop()
KeyError: 'pop from an empty set'

查  

只能用for循环

for i in set1:
print(i)

集合的运算

# ①交集
set1 = {'a', 'b', 'c', 'd', '1', '4'}
set2 = {'1', '2', '3', '4', 'b', 'c'}
print(set1 & set2)
>>> {'c', '4', '1', 'b'}
print(set1.intersection(set2))
>>> {'c', '4', '1', 'b'} # ②反交集
set1 = {'a', 'b', 'c', 'd', '1', '4'}
set2 = {'1', '2', '3', '4', 'b', 'c'}
print(set1 ^ set2)
>>> {'d', '2', 'a', '3'}
print(set1.symmetric_difference(set2))
>>> {'d', '2', 'a', '3'} # ③并集
set1 = {'a', 'b', 'c', 'd', '1', '4'}
set2 = {'1', '2', '3', '4', 'b', 'c'}
print(set1 | set2)
>>> {'1', 'b', '2', '3', 'c', 'd', '4', 'a'}
print(set1.union(set2))
>>> {'1', 'b', '2', '3', 'c', 'd', '4', 'a'} # ④差集
set1 = {'a', 'b', 'c', 'd', '1', '4'}
set2 = {'1', '2', '3', '4', 'b', 'c'}
print(set1 - set2)
>>> {'d', 'a'}
print(set1.difference(set2))
>>> {'d', 'a'} print(set2 - set1)
>>> {'2', '3'}
print(set2.difference(set1))
>>> {'2', '3'} # ⑤子集与超集
set1 = {'A','B','C','a','b','c'}
set2 = {'A','B','C'}
print(set2 < set1)
>>> True
print(set2.issubset(set1))
>>> True print(set1 > set2)
>>> True
print(set1.issuperset(set2))
>>> True

  

不可变集合

# 不可变集合
set_frozen = frozenset({'A', 'B', 'C'})
print(set_frozen)
>>> frozenset({'C', 'B', 'A'})

python基础数据类型--集合(set)的更多相关文章

  1. Python基础数据类型-列表(list)和元组(tuple)和集合(set)

    Python基础数据类型-列表(list)和元组(tuple)和集合(set) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的 ...

  2. Python基础数据类型之集合

    Python基础数据类型之集合 集合(set)是Python基本数据类型之一,它具有天生的去重能力,即集合中的元素不能重复.集合也是无序的,且集合中的元素必须是不可变类型. 一.如何创建一个集合 #1 ...

  3. Python 入门之Python基础数据类型及其方法

    Python 入门之Python基础数据类型 1. 整型:int 用于计算,用于比较 (在赋值的时候先执行等号右边的内容) 1.1 整数的加 a = 10 b = 20 print(a + b) 结果 ...

  4. 图解python | 基础数据类型

    作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/56 本文地址:http://www.showmeai.tech/article-det ...

  5. Python基础数据类型-字符串(string)

    Python基础数据类型-字符串(string) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的每一篇都是Python3.x版 ...

  6. python基础数据类型考试题

    Python基础数据类型考试题 考试时间:两个半小时                      满分100分(80分以上包含80分及格) 一,基础题. 1,简述变量命名规范(3分) 2,字节和位的关系 ...

  7. 1--Python 入门--Python基础数据类型

    一.Python基础语法 初次使用Python,首先要明确三点: Python的标识符(例如变量名.函数名等),可用字母.数字和下划线构成,不能以数字开头,且区分大小写. Python对于缩进敏感.在 ...

  8. python 基础数据类型之list

    python 基础数据类型之list: 1.列表的创建 list1 = ['hello', 'world', 1997, 2000] list2 = [1, 2, 3, 4, 5 ] list3 = ...

  9. Python基础数据类型-字典(dict)

    Python基础数据类型-字典(dict) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的每一篇都是Python3.x版本的哟 ...

随机推荐

  1. Redis 事务在 SpringBoot 中的应用 (io.lettuce.core.RedisCommandExecutionException: ERR EXEC without MULTI)

    我们在 SpringBoot 中使用 Redis 时,会引入如下的 redis starter <dependency> <groupId>org.springframewor ...

  2. ES5 Object.assign 低版本浏览器内核兼容问题

    var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { ...

  3. python关键日期计算

    在coding的过程中有时候会需要用到一些特殊日期,比如说是一个月的最后一天的日期,昨天的日期等等. def first_day_of_next_month(self, old_date): old_ ...

  4. 编写安全 PHP 应用程序的七个习惯

    编写安全 PHP 应用程序的七个习惯   在提及安全性问题时,需要注意,除了实际的平台和操作系统安全性问题之外,您还需要确保编写安全的应用程序.在编写 PHP 应用程序时,请应用下面的七个习惯以确保应 ...

  5. SpringTest

    Spring Test 1.对junit的一个扩展   必须先导入junit jar包 2.简化获取bean的步骤 它的底层也是IOC容器 3.IOC的全部 junit的全部  junit的版本必须是 ...

  6. PaperReading20200226

    CanChen ggchen@mail.ustc.edu.cn   To share or not share Motivation: With the publiaction of NAS101, ...

  7. PaperReading20200223

    CanChen ggchen@mail.ustc.edu.cn   AdaBatch Motivation: Current stochastic gradient descend methods u ...

  8. JAVA 数据库操作工具类----sqllite

    package com.asc.db; import android.content.ContentValues; import android.content.Context; import and ...

  9. ROS学习笔记6-理解主题

    本文来源于:http://wiki.ros.org/ROS/Tutorials/UnderstandingTopics ROS主题假设turtlesim节点已经运行,打开一个新终端,使用如下命令运行键 ...

  10. Spring事务原理分析-部分二

    Spring事务原理分析-部分二 说明:这是我在蚂蚁课堂学习了余老师Spring手写框架的课程的一些笔记,部分代码代码会用到余老师的课件代码.这不是广告,是我听了之后觉得很好. 课堂链接:Spring ...