Python3 数据类型-集合
2024-09-03 21:33:54
在Python中集合set是基本数据类型的一种,它有可变集合(set)和不可变集合(frozenset)两种。创建集合set、集合set添加、集合删除、交集、并集、差集的操作都是非常实用的方法。
集合是可哈希的无序、可变类型,不能作为字典的键,但可以作为值使用。
一 创建集合
方法1:
set1 = {'a','b','c'}
print(type(set1)) # ---> <class 'set'> 方法2:
list1 = ['a','b','c','d']
str1 = 'python'
dict1 = {'name':'sunwk','age':1000,}
tup1 = ('Google', 'Runoob', 1997, 2000) print('list1:',set(list1)) # --> list1: {'c', 'd', 'a', 'b'}
print('str1:',set(str1)) # --> str1: {'o', 'y', 'h', 't', 'n', 'p'}
print('dict1:',set(dict1)) # --> dict1: {'age', 'name'}
print('tup1',set(tup1)) # --> tup1 {2000, 'Google', 1997, 'Runoob'} 实例1:
list1 = [[1,2],3,4]
print('list1:',set(list1)) # --> TypeError: unhashable type: 'list' '''
小结:
1、集合的创建使用set来创建或者直接使用{}括起来,和字典有些路类似,只不过结合没有键值对
2、可以把列表、字符串、字典、元组变成集合类型,总结起来就是可以把可迭代的数据类型变成结合。
3、int型是不可迭代类型,所以set(int)是不可以的。
4、set(dict)时,把字典中的键作为元素创建成结合
5、集合中每个元素必须都是不可变类型
'''
特殊应用
list1 = ['a','b','c','a']
str1 = 'array' print('list1:',set(list1)) # --> list1: {'a', 'b', 'c'}
print('str1:',set(str1)) # --> str1: {'a', 'r', 'y'} '''
集合可以去除字符串、列表、字典、元组中重复的元素。
'''
二 集合增加元素
set.add()
d = {1,2,'lvcm','zhangjm'}
d.add("sunwk") print('d -->',d)
# d --> {'zhangjm', 'lvcm', 2, 'sunwk', 1}
set.update()
f = {1,2,'lvcm'} f.update('abc')
print(f)
# {1, 2, 'a', 'lvcm', 'c', 'b'} f.update([12,'suwk'])
print(f)
# {'lvcm', 1, 2, 'suwk', 12}
小结:
- 使用add增加数据,会把添加的数据看成一个元素添加到原有集合中
- 使用update增加数据,会把添加的数据拆分成N个元素添加到原有集合中
三 集合删除元素
set.remove()
- 指定删除某个元素
- 无返回值
g = {'lvcm', 1, 2, 'suwk', 12} g.remove(2)
print(g)
#{1, 12, 'suwk', 'lvcm'}
set.pop()
- 删除元素是随机的,无法指定删除元素
- 有返回值
g = {'lvcm', 1, 2, 'suwk', 12} a = g.pop()
print(a)
print(g)
set.clear
- 清空集合
- 无返回值
g = {'lvcm', 1, 2, 'suwk', 12} g.clear()
print(g)
# set()
del
- 删除集合
- 无返回值
g = {'lvcm', 1, 2, 'suwk', 12} del g
print(g)
# NameError: name 'g' is not defined
四 集合操作符
# 等价操作 (==)
print(set('alex')==set('alleexx')) # --> True # 子集 set.issubset()(a<b)
print(set('alex')<set('alexw')) # --> True
print(set('alex')<set('alex')) # --> Flase # 父集、超集 set.issuperset() (a>b)
e = {1,2,3,4,5,6,7,8}
f = {4,5,6,7,8}
print(e.issuperset(f)) # --> True # 交集 set.intersection() (a & b)
a = {1,2,3,4,5}
b = {4,5,6,7,8}
print(a.intersection(b)) # --> {4, 5} # 并集 set.union (c | d)
c = {1,2,3,4,5}
d = {4,5,6,7,8}
print(c.union(d)) # --> {1, 2, 3, 4, 5, 6, 7, 8} # 差集 set.difference() (e-f f-e)
e = {1,2,3,4,5}
f = {4,5,6,7,8}
print(e.difference(f))#(e-f) # --> {1, 2, 3} in e but not in f
print(f.difference(e)) #(f-e) # --> {8, 6, 7} in f but not in e #对称差集 set.symmetric_difference()(e^f)
e = {1,2,3,4,5}
f = {4,5,6,7,8}
print(e.symmetric_difference(f)) # --> {1, 2, 3, 6, 7, 8}
最新文章
- .NET Core性能测试组件BenchmarkDotNet 支持.NET Framework Mono
- C 标准库系列之limits.h
- ASP.NET MVC 请求流程:Route
- 基于FPGA的通信系统实验
- UI第四节——UIImageView详解
- sourceforge免费空间申请及使用笔记
- div排序 根据《input》
- ASP.NET MVC 学习4、Controller中添加SearchIndex页面,实现简单的查询功能
- 设置UITabBarController的背景颜色
- MySQL主从复制与lvs+keepalived单点写入读负载均衡高可用实验【转】
- HDU4545+计算日期
- UVa 900 - Brick Wall Patterns
- bootstrap tooltips在 angularJS中的使用
- Docker 容器资源限制
- C# XML反序列化与序列化举例:XmlSerializer(转)
- php 汉字的首字母
- [转] webpack3最新版本配置研究(五) devtool,webpack-dev-server,CommonsChunkPlugin
- Django的rest_framework的分页组件源码分析
- bootstrap-datepicker default value
- 腾讯互动课堂(Tencent Interact Class,TIC)SDK 词汇表
热门文章
- C++切勿混用带符号类型和无符号类型
- (Oracle)自定义调用AWR&;ADDM
- Ansible实现主备模式的高可用(Keepalived)
- pom.xml文件报MavenArchiver错误 org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration)
- go加密算法:非对称加密(三)--Elliptic
- PHP对接QQ互联,超级详细!!!
- 【Hive五】Hive函数UDF
- Codeforces Round #482 (Div. 2) :B - Treasure Hunt
- python--模块之collection
- Scala快速入门到精通 视频教程 百度云网盘下载地址