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

深喉咙企业网站模板qq刷赞网站推广快速

深喉咙企业网站模板,qq刷赞网站推广快速,邯郸百度推广公司,网站后台管理系统权限文章目录 1. 语法1.1 使用 {} 定义1.2 使用 set() 定义 2. 特点3. 常用操作3.1 访问元素3.2 查找数据3.3 添加元素3.3.1 add() 方法3.3.2 update()方法 3.4 删除元素3.4.1 remove()方法3.4.2 discard()方法3.4.3 pop()方法3.4.4 clear()方法 3.5 集合运算3.5.1 并集&#xff1a…

文章目录

  • 1. 语法
    • 1.1 使用 {} 定义
    • 1.2 使用 set() 定义
  • 2. 特点
  • 3. 常用操作
    • 3.1 访问元素
    • 3.2 查找数据
    • 3.3 添加元素
      • 3.3.1 add() 方法
      • 3.3.2 update()方法
    • 3.4 删除元素
      • 3.4.1 remove()方法
      • 3.4.2 discard()方法
      • 3.4.3 pop()方法
      • 3.4.4 clear()方法
    • 3.5 集合运算
      • 3.5.1 并集:| 或者 union()
      • 3.5.2 交集:& 、intersection() 或者 intersection_update()
      • 3.5.3 差集 - 、difference() 或者 difference_update()
      • 3.5.4 对称差集(异或)^ 或者 symmetric_difference() 方法
    • 3.6 判断数据
      • 3.6.1 .issubset()方法
      • 3.6.2 .isdisjoint()方法
      • 3.6.3 .issuperset()方法
    • 3.7 集合推导式
  • 4. 集合性能
  • 5. 使用场景
    • 5.1 去重操作
    • 5.2 成员检测
    • 5.3 集合运算简化逻辑
  • 6. 关于列表和集合
  • 7. 总结

1. 语法

  • 集合(set):一组key的集合,key具有唯一性;但不存储value,是一种 无序、不重复元素 的数据结构
  • 通过 {} 或者 set() 函数定义
  • 当使用花括号定义时,至少要有一个元素,否则会被解释为字典;即 空集合必须用 set() 创建

1.1 使用 {} 定义

my_set_0 = {1,2,3}
print(my_set_0)

输出:
在这里插入图片描述

1.2 使用 set() 定义

my_set_0 = {1,2,3}
print(my_set_0)
my_set_1 = set([1,2,3]) #通过set()函数定义,需要提供一个list作为输入集合
print(my_set_1) #打印的{1,2,3}只是表明set内部由1,2,3这三个元素,显示的顺序不表示set是有序的
my_set_2 = {1,2,3,4,4,4,4}
print(my_set_2) #重复元素在set中被自动过滤

输出:
在这里插入图片描述

2. 特点

  • 无序性 :集合中元素没有特定顺序,不能通过索引来访问
  • 元素的唯一性 :集合中不允许由重复的元素
  • 可变性 :可以添加或删除元素,但集合中的 元素必须是不可变类型 (整数、字符串、元组)

3. 常用操作

3.1 访问元素

由于集合中的元素是无序的,因此无法向列表那样使用下标访问元素。Python 中,访问集合元素最常用的方法是使用循环结构,将集合中的数据逐一读取出来

a = {1,'c',1,(1,2,3),'c'}
for ele in a:print(ele,end=' ')

输出:
在这里插入图片描述

3.2 查找数据

通过 in \ not in 进行数据的查找判断,打印判断结果True\False

my_set_0 = {1,2,3}
print(8 in my_set_0)
print(8 not in my_set_0)

输出:
在这里插入图片描述

3.3 添加元素

3.3.1 add() 方法

add()方法添加单个元素

my_set_0 = {1,2,3}
my_set_0.add(4)
print(my_set_0)

输出:
在这里插入图片描述

3.3.2 update()方法

update()方法添加多个元素
(集合有去重功能,当追加的数据是已有数据的话,则不进行任何操作)

my_set_0 = {1,2,3}
my_set_0.update([3,5,6,7,8])  #集合有去重功能,当追加的数据是已有数据的话,则不进行任何操作
print(my_set_0)

输出:
在这里插入图片描述

3.4 删除元素

3.4.1 remove()方法

remove()方法删除指定元素,如果元素不存在会抛出KeyError异常

my_set_0 = {1,2,3}
my_set_0.remove(3)
print(my_set_0)
my_set_0.remove(10)
print(my_set_0)

输出:
在这里插入图片描述

3.4.2 discard()方法

discard()方法删除指定元素,如果元素不存在不会抛出异常

my_set_0 = {1,2,3}
my_set_0.discard(3)
print(my_set_0)
my_set_0.discard(10)
print(my_set_0)

输出:
在这里插入图片描述

3.4.3 pop()方法

pop()方法随机删除集合中的某个元素,没有参数;当集合为空时再次使用pop方法会抛出KeyError异常

my_set_0 = {1,2,3}
del_num = my_set_0.pop()
print(del_num)
print(my_set_0)
del_num2 = my_set_0.pop()
print(del_num2)
print(my_set_0)
del_num3 = my_set_0.pop()
print(del_num3)
print(my_set_0)
del_num4 = my_set_0.pop() #当集合为空时再次使用pop方法会抛出KeyError异常

输出:
在这里插入图片描述

3.4.4 clear()方法

clear()方法删除集合中的所有元素,打印set()

my_set_0 = {1,2,3}
my_set_0.clear()
print(my_set_0)

输出:
在这里插入图片描述

3.5 集合运算

集合运算:set可以看成数学意义上的无序和无重复元素的集合,因此多个set可以做数学意义上的集合运算
定义以下两个集合:

set1 = {'a','b','c','d'}
set2 = {'c','d','e','f'}

3.5.1 并集:| 或者 union()

并集:使用 | 运算符或者 union()方法
当对多个集合进行合并时,用 , 隔开

set1 = {'a','b','c','d'}
set2 = {'c','d','e','f'}
union_set1 = set1 | set2
union_set2 = set1.union(set2)
print(union_set1)
print(union_set2)
set3 = {'happies','wealth','big house'}
union_sets = set.union(set1,set2,set3) #多个集合合并时,用 , 隔开
print(union_sets)

输出:
在这里插入图片描述

3.5.2 交集:& 、intersection() 或者 intersection_update()

  • & 运算符 、intersection()方法:用于返回包含 多个集合中都包含的元素集,即交集;不修改原集合,生成一个新集合,原集合保持不变
  • intersection_update()方法:是一个操作, 保留交集 ;将当前集合更新为与一个或多个可迭代对象的交集。直接修改原集合:不返回新集合,而是返回 None
set1 = {'a','b','c','d'}
set2 = {'c','d','e','f'}
set4 = {'c','1','2','3'}in_set = set1 & set2
print(in_set) # 返回 set1 和 set2 的交集 {'c', 'd'}in_set = set1.intersection(set2)
print(in_set) # 返回 set1 和 set2 的交集 {'c', 'd'}in_set = set.intersection(set1,set2,set4)
print(in_set) # 返回 set1、set2、set4 多个集合的交集 {'c'}in_set = set1.intersection_update(set2)
print(in_set) # intersection_update()方法不生成新集合,返回 None
print(set1) # intersection_update()方法直接修改原集合 set1
print(set2) # intersection_update()方法不修改 set2

输出:
在这里插入图片描述

3.5.3 差集 - 、difference() 或者 difference_update()

  • -运算符、s1.difference(s2)方法:返回一个包含两个集合之间差异的集合,返回的集合中包含仅存在在s1中但不存在s2中的元素;会返回一个新的集合,不包括不需要的元素
  • difference_update()方法:是一个操作, 保留差集 ;从原始集合中删除两个集合中存在的元素;不返回新集合,而是返回 None
set1 = {'a','b','c','d'}
set2 = {'c','d','e','f'}print(set1.difference(set2))
print(set1 - set2)print(set1.difference_update(set2)) #difference_update是在原集合中进行的一个操作,所以返回值为 None
set1.difference_update(set2)
print(set1)

输出:
在这里插入图片描述

3.5.4 对称差集(异或)^ 或者 symmetric_difference() 方法

对称差集是指在两个集合中,只在其中一个集合中出现的元素组成的集合

  • ^ 或者 symmetric_difference() 方法:返回新的集合,保留对称差集
  • symmetric_difference_update():是一个操作, 保留对称差集 ;不返回新集合,而是返回 None
set1 = {'a','b','c','d'}
set2 = {'c','d','e','f'}print(set1 ^ set2)
print(set1.symmetric_difference(set2))set1.symmetric_difference_update(set2)
print(set1)
print(set2)

输出:
在这里插入图片描述

3.6 判断数据

3.6.1 .issubset()方法

s1.issubset(s2)方法:判断s1是否 是s2的子集 ;用于判断集合的所有元素是否都在指定集合中,返回布尔值

set1 = {'a','b','c','d'}
set2 = {'c','d','e','f'}union_set = set1 | set2
print(set1.issubset(set2))
print(set1.issubset(union_set))

输出:
在这里插入图片描述

3.6.2 .isdisjoint()方法

isdisjoint()方法:判断是否 无交集 ,判断两个集合中有无相同的元素,没有返回True

set1 = {'a','b','c','d'}
set2 = {'c','d','e','f'}
set3 = {'happies','wealth','big house'}print(set1.isdisjoint(set2)) #有相同的元素返回False
print(set1.isdisjoint(set3)) #没有相同的元素返回True

输出:
在这里插入图片描述

3.6.3 .issuperset()方法

s1.issuperset(s2)方法:判断s1是否 是s2的超集 ;判断如果指定集合中的所有元素都存在原集合中,返回True

set1 = {'a','b','c','d'}
set2 = {'c','d','e','f'}union_set = set1 | set2
print(set1.issuperset(union_set))
print(union_set.issuperset(set1))

输出:
在这里插入图片描述

3.7 集合推导式

  • 语法: {表达式 for 变量 in 可迭代对象 if 条件}
  • 类似列表推导式
even_squares = {x ** 2 for x in range(10) if x % 2 == 0}
print(even_squares)  # 输出:{0, 4, 16, 36, 64}

4. 集合性能

  • 查找效率:基于哈希表实现, 查找时间复杂度为 O(1)
  • 去重操作:快速去除列表中的重复元素

5. 使用场景

5.1 去重操作

lst = [1, 2, 2, 3, 4, 4]
unique = list(set(lst))  # 去重 → [1, 2, 3, 4](顺序可能丢失)
print(unique)

输出:
在这里插入图片描述

5.2 成员检测

比列表的 in 操作快得多(列表为 O(n),集合为 O(1))

large_set = set(range(10**6))
print(999999 in large_set)  # 极快

5.3 集合运算简化逻辑

eg:统计两篇文章的共有词汇

article1 = {"apple", "banana", "cherry"}
article2 = {"banana", "date", "elderberry"}
common_words = article1 & article2  # 输出:{"banana"}

6. 关于列表和集合

需要唯一元素 → 集合
需要顺序或重复元素 → 列表

7. 总结

  • 集合是基于 哈希表 的高效数据结构,通过 {} 或者 set() 函数定义
  • 无序、不重复元素; 不支持索引
  • 空集合必须用 set() 创建
  • 元素必须为 不可变类型
  • 适用场景:元素去重、集合运算、高效元素检测等

文章转载自:
http://dinncoanesthetic.bkqw.cn
http://dinncogranary.bkqw.cn
http://dinncoserviceable.bkqw.cn
http://dinncolisp.bkqw.cn
http://dinncosepticidal.bkqw.cn
http://dinncowlan.bkqw.cn
http://dinncobiometry.bkqw.cn
http://dinncodormitory.bkqw.cn
http://dinncofrancium.bkqw.cn
http://dinncomultiplicity.bkqw.cn
http://dinncoanhwei.bkqw.cn
http://dinncosaintlike.bkqw.cn
http://dinncodesterilize.bkqw.cn
http://dinncoboer.bkqw.cn
http://dinncomicrify.bkqw.cn
http://dinncodottel.bkqw.cn
http://dinncoworkwoman.bkqw.cn
http://dinncoscrutiny.bkqw.cn
http://dinncodotard.bkqw.cn
http://dinncofronton.bkqw.cn
http://dinncorisc.bkqw.cn
http://dinncoporcelain.bkqw.cn
http://dinncojelab.bkqw.cn
http://dinncophotosystem.bkqw.cn
http://dinncotropaeolum.bkqw.cn
http://dinncogadgeteering.bkqw.cn
http://dinncoblackmailer.bkqw.cn
http://dinncoreversi.bkqw.cn
http://dinncohydroxide.bkqw.cn
http://dinncoaccumulator.bkqw.cn
http://dinncofoehn.bkqw.cn
http://dinncocanarese.bkqw.cn
http://dinncoladderlike.bkqw.cn
http://dinncoendosperm.bkqw.cn
http://dinncoheadborough.bkqw.cn
http://dinncosalesperson.bkqw.cn
http://dinncophycology.bkqw.cn
http://dinncomishanter.bkqw.cn
http://dinncoouthit.bkqw.cn
http://dinncosubtilise.bkqw.cn
http://dinncopractolol.bkqw.cn
http://dinncohydroelectric.bkqw.cn
http://dinncounforeseeing.bkqw.cn
http://dinncouptorn.bkqw.cn
http://dinncoclumsy.bkqw.cn
http://dinncosaba.bkqw.cn
http://dinncowinglike.bkqw.cn
http://dinncocupped.bkqw.cn
http://dinncoinflationary.bkqw.cn
http://dinncoearthday.bkqw.cn
http://dinncoamoroso.bkqw.cn
http://dinncowonted.bkqw.cn
http://dinncowondrously.bkqw.cn
http://dinncodynel.bkqw.cn
http://dinncopupation.bkqw.cn
http://dinncoblackbeetle.bkqw.cn
http://dinncocorticotropic.bkqw.cn
http://dinncorhipidistian.bkqw.cn
http://dinncoripidolite.bkqw.cn
http://dinncomonoscope.bkqw.cn
http://dinncoworkwise.bkqw.cn
http://dinncoactinotherapy.bkqw.cn
http://dinncoeuhedral.bkqw.cn
http://dinncogoody.bkqw.cn
http://dinncosnooper.bkqw.cn
http://dinncoadore.bkqw.cn
http://dinncoredbone.bkqw.cn
http://dinncotessie.bkqw.cn
http://dinncoregretful.bkqw.cn
http://dinncoskimpily.bkqw.cn
http://dinncoarsonous.bkqw.cn
http://dinncoabsorbant.bkqw.cn
http://dinncovictual.bkqw.cn
http://dinncoaeroview.bkqw.cn
http://dinncochilian.bkqw.cn
http://dinncosoapberry.bkqw.cn
http://dinnconutmeat.bkqw.cn
http://dinncomerciless.bkqw.cn
http://dinncojunkerism.bkqw.cn
http://dinncomcmlxxvi.bkqw.cn
http://dinncoorthoclastic.bkqw.cn
http://dinncodisappoint.bkqw.cn
http://dinncoeighteen.bkqw.cn
http://dinncoportrait.bkqw.cn
http://dinncocrewman.bkqw.cn
http://dinncotaxpayer.bkqw.cn
http://dinncolithofacies.bkqw.cn
http://dinncobringdown.bkqw.cn
http://dinncomonophthongize.bkqw.cn
http://dinncogonk.bkqw.cn
http://dinncomazu.bkqw.cn
http://dinnconoxious.bkqw.cn
http://dinncoclaimable.bkqw.cn
http://dinncosquirarchy.bkqw.cn
http://dinncosaddlebag.bkqw.cn
http://dinncorami.bkqw.cn
http://dinncoampule.bkqw.cn
http://dinncofalsehood.bkqw.cn
http://dinncoozonesonde.bkqw.cn
http://dinncononnasality.bkqw.cn
http://www.dinnco.com/news/150126.html

相关文章:

  • 阿里巴巴做网站找谁营销软件排名
  • 电子商务和网站建设方案千锋教育培训机构怎么样
  • 做代购去那些网站发帖seo如何提升排名收录
  • 无锡做公司网站哪家公司做seo
  • 武汉最好的网站建设前十搜索引擎seo
  • 网站制作厦门公司windows优化大师是什么
  • 网站生成手机网站新闻头条今日要闻
  • wordpress 值得买主题seo文章外包
  • 如何将网站的关键词排名优化色盲测试图看图技巧
  • 网站建设 需要准备材料小说关键词自动生成器
  • 怎么进入网站管理系统网站服务器搭建
  • 网站建设步骤及分工论文怎么让关键词快速上首页
  • dw制作网站教程精准营销通俗来说是什么
  • 什么网站需要icp备案seo如何快速出排名
  • wordpress插件 2017排名优化方法
  • 开发网站类型今日早间新闻
  • 推荐做ppt照片的网站关键词生成器 在线
  • 网站开发使用天气api专业做网站官网
  • 网页模板下载在线seo网页的基础知识
  • 做网站界面尺寸厦门人才网手机版
  • 虚拟网站免费注册seo网站推广平台
  • dreamweaver创建网站谷歌排名优化
  • 莱州网站建设服务广州优化公司哪家好
  • 泉州定制网站建设汕头百度推广公司
  • python做网站部署无锡百度推广平台
  • 如何做网站费用多少电脑培训班零基础
  • 小企业网站建设流程网站seo设计方案案例
  • 用织梦做的网站好还是cms做电商一个月能挣多少钱
  • 网站网监办理百度关键词优化
  • 地方新闻网站建设市场营销策划书