Python之路 day2 集合的基本操作
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:ersa
'''
#集合是无序的
集合的关系测试,
增加,删除,查找等操作
'''
#列表去重 ==》转集合自动去重
list_1 = [1,3,5,7,9,11,2,4]
list_2 = [2,4,6,8,10] print(list_1,list_2) #列表转集合
list_1 = set(list_1)
list_2 = set(list_2) print("关系测试".center(70,'='))
#两个集合的交集
print( list_1.intersection(list_2) ) #并集
print( list_1.union(list_2) ) #差集==》我有你没有
print(list_1.difference(list_2)) #子集
list_3 = set([1,3,5])
print(list_3.issubset(list_1))
print(list_1.issuperset(list_3)) #对称差集==》两个集合中都没有
print(list_1.symmetric_difference(list_2)) #没有交集则返回true
list_4 = set([2,4,6])
print(list_3.isdisjoint(list_4)) print("关系运算符".center(70,'='))
#交集 &
print("%s & %s 交集:\n"%(list_1,list_3),list_1 & list_3)
#并集 |
print("%s | %s 并集:\n"%(list_1,list_2),list_1 | list_2) #差集 在 list_1 中不在 list_2中
print("%s - %s 差集:\n"%(list_1,list_2),list_1 - list_2) #对称差集 在 list_1 或 list_2中,但不会同时出现在二者中
print("%s ^ %s 对称差集:\n"%(list_1,list_2),list_1 ^ list_2) print("集合的基本操作 增、删、改、查".center(70,'=')) #增加1个
list_1.add(999)
print("%s 增 add: \n"%(list_1),list_1) #增加多个 update
list_1.update([19,29,39])
print("%s 增加多个 update \n"%(list_1),list_1) #删除 remove
list_1.remove(999)
print("%s 删除 remove \n"%(list_1),list_1) #remove 删除不存在的会报错,discard 不会报错
print("%s 删除 discard \n"%(list_1),list_1.discard('ddd'))
list_1.discard(11)
print("%s 删除 discard \n"%(list_1)) #集合的长度
print("%s 长度 \n"%(list_1),len(list_1)) #是否是集合的成员
print("%s 是否在 集合中 \n"%(list_1),999 in list_1) #是否不是集合的成员
print("%s 不在 集合中 \n"%(list_1),999 not in list_1) #删除第一个
print("%s 删除第一个 \n"%(list_1),list_1.pop())
print(list_1)
Python之路 day2 集合的基本操作的更多相关文章
- Python之路,Day2 - Python基础(转载Alex)
Day2-转自金角大王 本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存 ...
- Python之路,Day2 - Python基础2
def decode(self, encoding=None, errors=None): """ 解码 """ ""& ...
- Python之路Day2
-->the start 养成好习惯,每次上课的内容都要写好笔记. 第二天内容主要是熟悉int.long.float.str.list.dict.tuple这几个类的内建方法. 对于Python ...
- 小白的Python之路 day2 字符串操作 , 字典操作
1. 字符串操作 特性:不可修改 name.capitalize() 首字母大写 name.casefold() 大写全部变小写 name.center(50,"-") 输出 '- ...
- 小白的Python之路 day2 文件操作
文件操作 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...
- Python之路 day2 字符编码及转换
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa import sys print("sys default encodin ...
- Python之路 day2 文件基础操作
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa ''' #f,文件句柄;模式 a : append 追加文件内容 f = open( ...
- Python之路 day2 按行读文件
#1. 最基本的读文件方法: # File: readline-example-1.py file = open("sample.txt") while 1: line = fil ...
- Python之路-Day2
二进制 1bit = 一个二进制位 8bit = 1byte 循环 for while 数据类型 数字.字符串.列表.元祖.字典.集合 字符编码 文件处理 for循环: for i in range( ...
随机推荐
- [liusy.api-SMJ]-SMJ 介绍 学习阶段(二)
我选择的是 Spring + Mybatis + Jesery 无状态架构方案. JAVA 比较常用的架构 SSH spring struts2 hibernate流行一阶段后, SSM spr ...
- IISExpress 调试使用学习,使用附加到进程进行快速调试
IIS8.0 Express已经推出了,大家可以通过Microsoft Web Platform Installer 进行安装.(VS2012,VS2013已经内置了,不需安装了) 1.IIS Exp ...
- Azure Management API 之 利用 Windows Azure Management Libraries 来控制Azure platform
在此之前,我曾经发过一篇文章讲叙了如何利用Azure power shell team 提供的class library. 而就在这篇文章发布之后不久,我又发现微软发布了一个preview 版本的Wi ...
- nfc相关
nfc普通读卡写卡按厂商API操作即可,但是牵扯到NDEF的读写就另当别论了,算是二次开放了,android手机有成熟的接口,.net也有一些,github上有一个,还没研究, https://git ...
- 【转】Git图形化界面客户端大汇总
原文网址:http://my.oschina.net/amstrong/blog/159114 目录[-] 一.TortoiseGit - The coolest Interface to Git V ...
- sql查看锁与解锁
select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sys.dm_tran ...
- 遇到的java面试题
1.struts2与struts1的区别 2.声明式事务是什么,怎么实现? 3.ajax两种请求方式 4.java中string str=new string("ss")创建了个几 ...
- c++课程设计的收获。
1.cin.clear() 如果输入错误,出现不匹配,要用clear清楚错误状态. (也就是把failbit设置为0) while(cin.get()!='\n') continue;吸收缓冲区也就是 ...
- JavaScript的apply()方法和call()方法
1 <script type="text/javascript"> 2 /*定义一个人类*/ 3 function Person(name,age) 4 { 5 thi ...
- 工作队列(workqueue) create_workqueue/schedule_work/queue_work
--- 项目需要,在驱动模块里用内核计时器timer_list实现了一个状态机.郁闷的是,运行时总报错"Scheduling while atomic",网上搜了一下:" ...