当前位置: 首页 > news >正文

武汉麦诺信网站建设/关键词排名seo

武汉麦诺信网站建设,关键词排名seo,上海装修公司排名榜,网站做的相似目录 目标 版本 官方文档 集合分类 实战 创建 循环 常用方法 目标 掌握set和frozenset两种集合的使用方法,包括:创建、交集、并集、差集等操作。 版本 Python 3.12.0 官方文档 Set Types — set, frozensethttps://docs.python.org/3/library/s…

目录

目标

版本

官方文档

集合分类

实战

创建

循环

常用方法


目标

        掌握set和frozenset两种集合的使用方法,包括:创建、交集、并集、差集等操作。


版本

        Python 3.12.0


官方文档

Set Types — set, frozenseticon-default.png?t=N7T8https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset


集合分类

官方定义

Set Types — set, frozenset
    A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. (For other containers see the built-in dict, list, and tuple classes, and the collections module.)

    Like other collections, sets support x in set, len(set), and for x in set. Being an unordered collection, sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.

    There are currently two built-in set types, set and frozenset. The set type is mutable — the contents can be changed using methods like add() and remove(). Since it is mutable, it has no hash value and cannot be used as either a dictionary key or as an element of another set. The frozenset type is immutable and hashable — its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set.

    Non-empty sets (not frozensets) can be created by placing a comma-separated list of elements within braces, for example: {'jack', 'sjoerd'}, in addition to the set constructor.

译文

  1. 集合对象是无序的,因此集合不记录元素的插入位置或顺序,因此集合不支持索引、切片或其他类似序列对象的方法。
  2. 目前有两种内置的集合类型,分别是 set 和 frozenset。
  3. set是可变的,内容可以通过add()和remove()等方法进行更改。由于它是可变的,它没有哈希值,因此不能用作字典键或另一个集合的元素。
  4. frozenset是不可变的,其内容在创建后不能更改;因此可以用作字典键或另一个集合的元素。
  5. 集合用大括号或构造方法创建,用逗号分隔元素。

补充

        setfrozenset内的元素不可以重复


实战

frozenset和set的操作基本一致(构造方法名不同、frozenset不能增删元素而set能增删元素),因此这里只例举set类型的使用方法。

创建

官方文档

class set([iterable])
class frozenset([iterable])
    Return a new set or frozenset object whose elements are taken from iterable. The elements of a set must be hashable. To represent sets of sets, the inner sets must be frozenset objects. If iterable is not specified, a new empty set is returned.

    Sets can be created by several means:

    Use a comma-separated list of elements within braces: {'jack', 'sjoerd'}

    Use a set comprehension: {c for c in 'abracadabra' if c not in 'abc'}

    Use the type constructor: set(), set('foobar'), set(['a', 'b', 'foo'])

译文

  1. 集合的元素来自可迭代对象。如果未指定可迭代对象,则返回一个新的空集合。
  2. 集合的元素必须是可哈希的。
  3. 集合嵌套集合,内部的集合必须是frozenset。

集合的创建方法:

  1. 用大括号创建,元素用逗号分隔。
  2. 用集合推导式创建。
  3. 用set构造方法创建。

用大括号创建,元素用逗号分隔。

mySet={"a","b","c"}
#输出:<class 'set'>
print(type(mySet))
#输出:{'b', 'c', 'a'}
print(mySet)

用集合推导式创建。

mySet={c for c in '1231231234545' if c not in '123'}
#输出:<class 'set'>
print(type(mySet))
#输出:{'4', '5'}
#分析:拆分字符串,将每个字符作为集合的元素,条件是元素不能为:1、2、3
print(mySet)

用set构造方法创建。

方法一(用list创建set)

mySet= set([1, 2, 3, 3, 4, 4, 5,False,True,None,None,"1"])
#输出:{False, 1, 2, 3, 4, 5, None, '1'}
print(mySet)

方法二(用tuple创建set)

mySet= set((1, 2, 3, 3, 4, 4, 5,False,True,None,None,"1"))
#输出:{False, 1, 2, 3, 4, 5, None, '1'}
print(mySet)

方法三(用字符串创建set)

mySet= set("1234567890123")
#输出:{'3', '0', '9', '6', '2', '8', '1', '4', '5', '7'}
print(mySet)

方法四(根据文件对象创建set)

# 我桌面有个a.txt文件
with open('C://Users//20203//Desktop//a.txt', 'r') as file:mySet = set(file.readlines())
# 输出:{'2\n', '4\n', '3\n', '1\n', 'True', 'False\n'}
print(mySet)# 去掉\n
with open('C://Users//20203//Desktop//a.txt', 'r') as file:mySet = set(line.strip() for line in file.readlines())
print(mySet)

方法五(根据迭代器对象创建set)

myIter=iter([1,2,3,False,True,"Hello World."])
mySet=set(myIter)
#输出:<class 'set'>
print(type(mySet))
#输出:{False, 1, 2, 3, 'Hello World.'}
print(mySet)

循环

方法一(for循环)

mySet={1,2,3,4,5}
for value in mySet:print(value)

方法二(enumerate() 函数)

mySet={1,2,3,4,5}
for index,value in enumerate(mySet):print(index,value)

方法三(while循环)

mySet = {1, 2, 3, 4, 5}
#使用while循环遍历集合
iterator = iter(mySet)
while True:try:element = next(iterator)print(element)except StopIteration:break

常用方法

元素个数

mySet = {123, 2, 3, 4, 5}
print(len(mySet))

集合是否包含元素

mySet = {123, 2, 3, 4, 5}
#输出:True
print(2 in mySet)

集合是否不包含元素

mySet = {123, 2, 3, 4, 5}
#输出:True
print(2 not in mySet)

交集

mySet={123, 2, 3, 4, 5}
yourSet={123, 2, 3, 4, 5,6,7}
#交集
otherSet = mySet & yourSet
#输出:{2, 3, 4, 5, 123}
print( otherSet )

并集

mySet={123, 2, 3, 4, 5}
yourSet={123, 2, 3, 4, 5,6,7}
#交集
otherSet = mySet | yourSet
#输出:{2, 3, 4, 5, 6, 7, 123}
print( otherSet )

差集

mySet={123, 2, 3, 4, 5}
yourSet={123, 2, 3, 4, 5,6,7}
#差集
otherSet = yourSet - mySet
#输出:{6, 7}
print( otherSet )

对称差集

mySet={123, 2, 3, 4, 5,9}
yourSet={123, 2, 3, 4, 5,6,7}
#对称差集
otherSet = mySet ^ yourSet
#输出:{6, 7, 9}
print( otherSet )

集合嵌套集合

mySet = {frozenset(["a","b"]),frozenset(["c","d"]) }
#输出:<class 'frozenset'>
print(type(frozenset(["a"])))
for inner_set in mySet:print("--------")for element in inner_set:print(element)

添加元素

mySet={1, 2, 3, 4, 5}
mySet.add(6)
#输出:{1, 2, 3, 4, 5, 6}
print(mySet)

删除元素

mySet={1, 2, 3, 4, 5}
mySet.remove(1)
#输出:{2, 3, 4, 5}
print(mySet)

是否没有共同元素

mySet = {1, 2, 3, 4, 5}
yourSet = {1, 2, 3}
# 输出:False
# 分析:有共同元素:1,2,3
print(mySet.isdisjoint(yourSet))mySet = {4, 5}
yourSet = {1, 2, 3}
# 输出:True
# 分析:没有共同元素
print(mySet.isdisjoint(yourSet))

是否是子集关系

mySet = {1, 2, 3, 4, 5}
yourSet = {1, 2, 3}
# 输出:False
# 分析:mySet不是yourSet的子集
print(mySet.issubset(yourSet))
# 输出:True
# 分析:yourSet是mySet的子集
print(yourSet.issubset(mySet))

是否是超集关系

mySet = {1, 2, 3, 4, 5}
yourSet = {1, 2, 3}
# 输出:True
# 分析:mySet是yourSet的超集
print(mySet.issuperset(yourSet))
# 输出:False
# 分析:yourSet不是mySet的超集
print(yourSet.issuperset(mySet))

http://www.jmfq.cn/news/5364595.html

相关文章:

  • 易企秀 旗下 网站建设/seo资讯网
  • 留学中介网站建设方案/商品seo优化是什么意思
  • 工程建设公司网站/雅思培训机构哪家好机构排名
  • 计算机网站建设体会/建立免费个人网站
  • 天津建设工程评标专家网站/镇江关键字优化品牌
  • 网站集约化建设 统一出口/北京广告公司
  • 我为什么电商要学网站建设/seo长尾快速排名
  • 建设企业高端网站/网站查询
  • 建设网站图/青岛seo排名收费
  • 数字资产交易网站建设/百度高搜
  • 怎么写网站建设的说明/广告公司营销策划方案
  • 达濠市政建设有限公司网站/百度推广登陆平台
  • 安徽省建设业协会网站/宁波网站推广公司价格
  • 广东建设工程造价管理协会网站/百度助手官网
  • 网站建设需要那些人才/seo优化方案项目策划书
  • 德宏北京网站建设/百度人工服务热线电话
  • 东西湖区城乡建设局网站/免费的网页入口
  • 网站建设排名政务/国外网站加速
  • 泉州哪家网站建设公司好/整站优化方案
  • 西安网站建设xamokj/国际新闻界
  • av网站正在建设中/网络营销和传统营销的区别和联系
  • 连云港东海网站建设/跨境电商平台推广
  • 中国建设银行北京分行门户网站公告/搜索引擎营销的方法不包括
  • 查建设项目开工是看建委网站吗/全网营销代运营
  • 成都网站网站建设/短视频推广平台有哪些
  • 政府网站建设的目的和意义/短视频推广引流
  • 智慧旅游门户网站建设方案/怎么找需要做推广的公司
  • 建设读书网站的意义/西安关键词排名优化
  • 吴兴网站建设/谷歌推广方案
  • 网站建设 中企动力南通/企业网络推广的方式有哪些