python 数据类型 --- 集合
1. 注意列表和集合的区别 set
列表表现形式: list_1 = [1,3,4]; 集合表现形式:set_1= set()
list_1 = [1,2,3,4,23,4,2]
print(list_1,type(list_1))
list_1 = set(list_1)
print(list_1,type(list_1))
list_2 = set([2,4,6,8,10])
print(list_2,type(list_2)) #运行结果
[1, 2, 3, 4, 23, 4, 2] <class 'list'>
{1, 2, 3, 4, 23} <class 'set'>
{8, 2, 10, 4, 6} <class 'set'>
2. 集合的关系:
############################# 集合的关系测试 part ###################################
#交集
print(list_1.intersection(list_2))
#对称差集 除去两个集合的交集的那部分
print(list_1.symmetric_difference(list_2))
#并集
print(list_1.union(list_2))
#差集
# is in list_1 , but not in list_2
print(list_1.difference(list_2))
# is in list_2, but not in list_1
print(list_2.difference(list_1))
#子集
list_3 = set([6,8,10])
print(list_3.issubset(list_2))
#父集
print(list_2.issuperset(list_3))
#""" Return True if two sets have a null intersection. """
print(list_1.isdisjoint(list_3))
print(list_1.isdisjoint(list_2))
'''
"& | - ^ " 集合关系的另一种表示方法
#交集
print("交集->", list_1 & list_2)
#union
print("并集->", list_1 | list_2)
# difference
print("difference-->",list_1 - list_2) # is in list_1 but not in list_2
#对称差集
print("对称差集-->", list_1 ^ list_2)
3. 集合的方法 add , update , remove, len, in , not in , pop, discard
list_1 = (1,3,5,7)
list_2 = ([1,3,5,7])
list_3 = set([1,3,5,7])
print(list_1,type(list_1))
print(list_2,type(list_2))
print(list_3,type(list_3))
#1.添加一项 add, 添加多项update
list_3.add(9)
print("test1--",list_3)
list_3.update([11,13])
print("test2--",list_3)
# 2.移走一项
list_3.remove(11)
print("test3--",list_3)
#.3 长度
print("test4--",len(list_3))
# 4.在不在里面
print("test5---", 6 in list_3, 3 in list_3, 11 not in list_3)
# 5.删除任意的set element ,并返回
print(list_3.pop())
list_3.discard() # Remove an element from a set if it is a member.If the element is not a member, do nothing. list_3.remove() #Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError
python 数据类型 --- 集合的更多相关文章
- Python数据类型--集合(set)
Python的集合是无序.可迭代的容器对象,所有元素放在一对大括号中{},元素之间使用逗号隔开,同一集合内的元素具有唯一性,不允许重复. 集合中只能包含数字.字符串.元组等不可变类型的数据,不能包含列 ...
- python初步学习-python数据类型-集合(set)
集合 在已经学过的数据类型中: 能够索引的,如list/str,其中的元素可以重复 可变的,如list/dict,即其中的元素/键值对可以原地修改 不可变的,如str/int,即不能进行原地修改 无索 ...
- 10 Python 数据类型—集合
在Python set是基本数据类型的一种集合类型,它有可变集合(set())和不可变集合(frozenset)两种.创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方法 ...
- Python 数据类型--集合(set)
一.集合(set) 集合也是一种数据类型,一个类似列表的,无序的,不重复的.它主要有两大作用 1.把一个列表变为集合,就自动去重了,不需要写额外的代码 2.关系测试,测试两组数据之间的交际.差集.并集 ...
- Python数据类型-集合(set)
1.创建集合 集合的创建不同于前两种数据结构. 集合通过set(iterable)方法创建,参数iterable为可迭代对象. 示例代码: s1 = set('好好学习天天想上') # 将字符串分解为 ...
- python数据类型(集合)
一.集合概念 集合是一个数学概念:由一个或多个确定的元素所构成的整体叫做集合. 集合中的元素三个特征: 确定性(元素必须可hash) 互异性(去重)——将一个列表变为集合,就自动去重了 无序性(集合中 ...
- python基础3 ---python数据类型二
ython基础 一.python数据类型 ------列表(list) 1.定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素 特性:可存放多个不同类型的值:可修改指定索 ...
- python数据类型之元组、字典、集合
python数据类型元组.字典.集合 元组 python的元组与列表类似,不同的是元组是不可变的数据类型.元组使用小括号,列表使用方括号.当元组里只有一个元素是必须要加逗号: >>> ...
- Python数据类型的内置函数之tuple(元组),dict(字典),set(集合)
Python数据类型内置函数 - str(字符串) - list(列表) - tuple(元组) - dict(字典) - set(收集) tuple(元组)的操作 - (count)统计元组中元素出 ...
随机推荐
- Angular2入门系列教程2-项目初体验-编写自己的第一个组件
上一篇 使用Angular-cli搭建Angular2开发环境 Angular2采用组件的编写模式,或者说,Angular2必须使用组件编写,没有组件,你甚至不能将Angular2项目启动起来 紧接着 ...
- 使用Python保存屏幕截图(不使用PIL)
起因 在极客学院讲授<使用Python编写远程控制程序>的课程中,涉及到查看被控制电脑屏幕截图的功能. 如果使用PIL,这个需求只需要三行代码: from PIL import Image ...
- Unity3d入门 - 关于unity工具的熟悉
上周由于工作内容较多,花在unity上学习的时间不多,但总归还是学习了一些东西,内容如下: .1 根据相关的教程在mac上安装了unity. .2 学习了unity的主要的工具分布和对应工具的相关的功 ...
- .NetCore MVC中的路由(2)在路由中使用约束
p { margin-bottom: 0.25cm; direction: ltr; color: #000000; line-height: 120%; orphans: 2; widows: 2 ...
- C语言 · Torry的困惑(基本型)
问题描述 Torry从小喜爱数学.一天,老师告诉他,像2.3.5.7--这样的数叫做质数.Torry突然想到一个问题,前10.100.1000.10000--个质数的乘积是多少呢?他把这个问题告诉老师 ...
- win7安装时,避免产生100m系统保留分区的办法
在通过光盘或者U盘安装Win7操作系统时,在对新硬盘进行分区时,会自动产生100m的系统保留分区.对于有洁癖的人来说,这个不可见又删不掉的分区是个苦恼.下面介绍通过diskpart消灭保留分区的办法: ...
- 在 C# 里使用 F# 的 option 变量
在使用 C# 与 F# 混合编程的时候(通常是使用 C# 实现 GUI,F#负责数据处理),经常会遇到要判断一个 option 是 None 还是 Some.虽然 Option module 里有 i ...
- Mybatis批量删除
<delete id="deleteByStandardIds"> delete from t_standard_catalog where standard_id i ...
- C#使用GET、POST请求获取结果
C#使用GET.POST请求获取结果,这里以一个简单的用户登陆为例. 1. 使用GET请求获取结果 1.1 创建LoginHandler.aspx处理页面 protected void Page_Lo ...
- iOS app内存分析套路
iOS app内存分析套路 Xcode下查看app内存使用情况有2中方法: Navigator导航栏中的Debug navigator中的Memory Instruments 一.Debug navi ...