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 数据类型 --- 集合的更多相关文章

  1. Python数据类型--集合(set)

    Python的集合是无序.可迭代的容器对象,所有元素放在一对大括号中{},元素之间使用逗号隔开,同一集合内的元素具有唯一性,不允许重复. 集合中只能包含数字.字符串.元组等不可变类型的数据,不能包含列 ...

  2. python初步学习-python数据类型-集合(set)

    集合 在已经学过的数据类型中: 能够索引的,如list/str,其中的元素可以重复 可变的,如list/dict,即其中的元素/键值对可以原地修改 不可变的,如str/int,即不能进行原地修改 无索 ...

  3. 10 Python 数据类型—集合

    在Python set是基本数据类型的一种集合类型,它有可变集合(set())和不可变集合(frozenset)两种.创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方法 ...

  4. Python 数据类型--集合(set)

    一.集合(set) 集合也是一种数据类型,一个类似列表的,无序的,不重复的.它主要有两大作用 1.把一个列表变为集合,就自动去重了,不需要写额外的代码 2.关系测试,测试两组数据之间的交际.差集.并集 ...

  5. Python数据类型-集合(set)

    1.创建集合 集合的创建不同于前两种数据结构. 集合通过set(iterable)方法创建,参数iterable为可迭代对象. 示例代码: s1 = set('好好学习天天想上') # 将字符串分解为 ...

  6. python数据类型(集合)

    一.集合概念 集合是一个数学概念:由一个或多个确定的元素所构成的整体叫做集合. 集合中的元素三个特征: 确定性(元素必须可hash) 互异性(去重)——将一个列表变为集合,就自动去重了 无序性(集合中 ...

  7. python基础3 ---python数据类型二

    ython基础 一.python数据类型     ------列表(list) 1.定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素 特性:可存放多个不同类型的值:可修改指定索 ...

  8. python数据类型之元组、字典、集合

    python数据类型元组.字典.集合 元组 python的元组与列表类似,不同的是元组是不可变的数据类型.元组使用小括号,列表使用方括号.当元组里只有一个元素是必须要加逗号: >>> ...

  9. Python数据类型的内置函数之tuple(元组),dict(字典),set(集合)

    Python数据类型内置函数 - str(字符串) - list(列表) - tuple(元组) - dict(字典) - set(收集) tuple(元组)的操作 - (count)统计元组中元素出 ...

随机推荐

  1. 【深入浅出jQuery】源码浅析--整体架构

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...

  2. 随手记_C#验证码

    前言 最近在网上偶然看见一个验证码,觉得很有意思,于是搜了下,是使用第三方实现的,先看效果: 总体来说效果还是可以的,官方提供的SDK也比较详细,可配置性很高.在这里在简单啰嗦几句使用方式: 使用步骤 ...

  3. WPF 微信 MVVM 【续】修复部分用户无法获取列表

    看过我WPF 微信 MVVM这篇文章的朋友,应该知道我里面提到了我有一个小号是无法获取列表的,始终也没找到原因. 前两天经过GitHub上h4dex大神的指导,知道了原因,是因为微信在登录以后,web ...

  4. H5 canvas的 width、height 与style中宽高的区别

    Canvas 的width height属性 1.当使用width height属性时,显示正常不会被拉伸:如下 <canvas id="mycanvas" width=&q ...

  5. vue.js几行实现的简单的todo list

    序:目前前端框架如:vue.react.angular,构建工具fis3.gulp.webpack等等...... 可谓是五花八门,层出不穷,眼花缭乱...其实吧只要你想玩还是可以玩玩的..下面是看了 ...

  6. ios 类似微信红点显示功能

    设计思路:给UIView增加一个分类 所有的视图都可以根据需要来进行红点显示 #import <UIKit/UIKit.h> @interface UIView (CHRRedDot) @ ...

  7. WebAPI

    WebAPI的Host OWIN IIS WebAPI 的handler和Filter有啥区别? WebAPI  常用 Filters Exception Filter Timer Filter Lo ...

  8. 读过MBA的CEO更自私?《哈佛商业评论》2016年第12期。4星

    老牌管理杂志.每期都值得精度.本期我还是给4星. 以下是本书中的一些内容的摘抄: 1:他们发现在Airbnb上,如果客人姓名听起来像黑人,那么比名字像白人的客人的接受率会低16%.#45 2:对立组织 ...

  9. Struts2框架基础

    Struts2框架基础 1.Java的框架 1.1.框架简介 在大型项目开发过程中,经常会使用到一些框架,这样做好的好处是能够提高工作效率,在java中最常用的的框架就是SSH,这其实是三个框架的简称 ...

  10. nuget常用命令

    nuget命令的用法: 一.安装 1.安装指定版本类库install-package <程序包名> -version <版本号> 2.安装到指定的项目install-packa ...