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)统计元组中元素出 ...
随机推荐
- 避免重复造轮子的UI自动化测试框架开发
一懒起来就好久没更新文章了,其实懒也还是因为忙,今年上半年的加班赶上了去年一年的加班,加班不息啊,好了吐槽完就写写一直打算继续的自动化开发 目前各种UI测试框架层出不穷,但是万变不离其宗,驱动PC浏览 ...
- 学点HTTP知识
不学无术 又一次感觉到不学无术,被人一问Http知识尽然一点也没答上来,丢人丢到家了啊.平时也看许多的技术文章,为什么到了关键时刻就答不上来呢? 确实发现一个问题,光看是没有用的,需要实践.看别人说的 ...
- const extern static 终极指南
const extern static 终极指南 不管是从事哪种语言的开发工作,const extern static 这三个关键字的用法和原理都是我们必须明白的.本文将对此做出非常详细的讲解. co ...
- c#语言规范
0x00 分类 C#语言规范主要有两个来源,即我们熟知的ECMA规范和微软的规范.尽管C#的ECMA规范已经前后修订4次,但其内容仅仅到C# 2.0为止.所以慕容为了方便自己和各位方便查询,在此将常见 ...
- 缓存工具类CacheHelper
代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...
- ASP.NET MVC一次删除多笔记录
批量删除数据记录,如下面的截屏: 先选中想删除的记录,然后点一下删除铵钮,系统将把选中的记录一次性删除.在此,Insus.NET不想每删除一笔记录连接一次数据库. 因此需要把选择的记录一次上传至服务器 ...
- thinkphp数据的查询和截取
public function NewsList(){ $this->assign('title','news'); $p = I('page',1); $listRows = 6; $News ...
- PHP 设计模式概述
一.设计模式(Design pattern)是什么? 设计模式是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. ...
- Android Weekly Notes Issue #235
Android Weekly Issue #235 December 11th, 2016 Android Weekly Issue #235 本期内容包括: 开发一个自定义View并发布为开源库的完 ...
- Toast显示图文界面——Android开发之路1
Toast的多种使用方法 Toast其实是一个功能特别强大的组件,不仅仅可以吐司一个文本内容,还可以吐司图片以及图文混排的界面.具体用法如下: 第一种:简单的纯文本内容的吐司: Toast.makeT ...