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)统计元组中元素出 ...
随机推荐
- Web性能优化:图片优化
程序员都是懒孩子,想直接看自动优化的点:传送门 我自己的Blog:http://cabbit.me/web-image-optimization/ HTTP Archieve有个统计,图片内容已经占到 ...
- react入门参考资料--step by step
ReactJS简介 React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript MVC 框架,都不满意,就决定自己写一套,用来架设 Instagram 的网站.做出 ...
- 深入.NET平台和C#编程总结大全
对于初学者的你,等到你把这个看完之后就更清楚地认知.NET和C#编程了,好了废话不多说,开始吧! ...
- App解读
一直不懂别人口中说的原生开发.混合式开发.今天突然看了一篇文章讲解的是什么叫做原生App?移动 Web App?混合APP?分享给大家. 原生App是专门针对某一类移动设备而生的,它们都是直接安装到设 ...
- 如何dos命令打开服务窗口?
1.输入services.msc点击<确定>进入服务窗口.如图:
- JavaScript 写计算器改进版
<html><head><style> .short{height:50px;width:55px;float:left;}</style></h ...
- web.xml中welcome-file-list的作用
今天尝试使用struts2+ urlrewrite+sitemesh部署项目,结果发现welcome-file-list中定义的欢迎页不起作用: <welcome-file-list> & ...
- JS中给正则表达式加变量
前不久同事询问我js里面怎么给正则中添加变量的问题,遂写篇博客记录下. 一.字面量 其实当我们定义一个字符串,一个数组,一个对象等等的时候,我们习惯用字面量来定义,例如: var s = &quo ...
- Lesson 23 A new house
Text I had a letter from my sister yesterday. She lives in Nigeria. In her letter, she said that she ...
- iOS开发系列--地图与定位
概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...