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

涿州网站建设营销网点机构号

涿州网站建设,营销网点机构号,官网模板免费下载,泗水县建设局的网站怎么打开Python3 集合 集合(set)是一个无序的不重复元素序列。 可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。 创建格式: parame …

Python3 集合

集合(set)是一个无序的不重复元素序列。

可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。

创建格式:

parame = {value01,value02,...}
或者
set(value)

实例(Python 3.0+)

>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket)                      # 这里演示的是去重功能
{'orange', 'banana', 'pear', 'apple'}
>>> 'orange' in basket                 # 快速判断元素是否在集合内
True
>>> 'crabgrass' in basket
False

>>> # 下面展示两个集合间的运算.
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  
{'a', 'r', 'b', 'c', 'd'}
>>> a - b                              # 集合a中包含而集合b中不包含的元素
{'r', 'd', 'b'}
>>> a | b                              # 集合a或b中包含的所有元素
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b                              # 集合a和b中都包含了的元素
{'a', 'c'}
>>> a ^ b                              # 不同时包含于a和b的元素
{'r', 'd', 'b', 'm', 'z', 'l'}

类似列表推导式,同样集合支持集合推导式(Set comprehension):

实例(Python 3.0+)

>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
{'r', 'd'}


集合的基本操作

1、添加元素

语法格式如下:

s.add( x )

将元素 x 添加到集合 s 中,如果元素已存在,则不进行任何操作。

实例(Python 3.0+)

>>> thisset = set(("Google", "Runoob", "Taobao"))
>>> thisset.add("Facebook")
>>> print(thisset)
{'Taobao', 'Facebook', 'Google', 'Runoob'}

还有一个方法,也可以添加元素,且参数可以是列表,元组,字典等,语法格式如下:

s.update( x )

x 可以有多个,用逗号分开。

实例(Python 3.0+)

>>> thisset = set(("Google", "Runoob", "Taobao"))
>>> thisset.update({1,3})
>>> print(thisset)
{1, 3, 'Google', 'Taobao', 'Runoob'}
>>> thisset.update([1,4],[5,6])  
>>> print(thisset)
{1, 3, 4, 5, 6, 'Google', 'Taobao', 'Runoob'}
>>>

2、移除元素

语法格式如下:

s.remove( x )

将元素 x 从集合 s 中移除,如果元素不存在,则会发生错误。

实例(Python 3.0+)

>>> thisset = set(("Google", "Runoob", "Taobao"))
>>> thisset.remove("Taobao")
>>> print(thisset)
{'Google', 'Runoob'}
>>> thisset.remove("Facebook")   # 不存在会发生错误
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'Facebook'
>>>

此外还有一个方法也是移除集合中的元素,且如果元素不存在,不会发生错误。格式如下所示:

s.discard( x )

实例(Python 3.0+)

>>> thisset = set(("Google", "Runoob", "Taobao"))
>>> thisset.discard("Facebook")  # 不存在不会发生错误
>>> print(thisset)
{'Taobao', 'Google', 'Runoob'}

我们也可以设置随机删除集合中的一个元素,语法格式如下:

s.pop() 

脚本模式实例(Python 3.0+)

thisset = set(("Google", "Runoob", "Taobao", "Facebook"))
x = thisset.pop()

print(x)

输出结果:

$ python3 test.py 
Runoob

多次执行测试结果都不一样。

set 集合的 pop 方法会对集合进行无序的排列,然后将这个无序排列集合的左面第一个元素进行删除。

3、计算集合元素个数

语法格式如下:

len(s)

计算集合 s 元素个数。

实例(Python 3.0+)

>>> thisset = set(("Google", "Runoob", "Taobao"))
>>> len(thisset)
3

4、清空集合

语法格式如下:

s.clear()

清空集合 s。

实例(Python 3.0+)

>>> thisset = set(("Google", "Runoob", "Taobao"))
>>> thisset.clear()
>>> print(thisset)
set()

5、判断元素是否在集合中存在

语法格式如下:

x in s

判断元素 x 是否在集合 s 中,存在返回 True,不存在返回 False。

实例(Python 3.0+)

>>> thisset = set(("Google", "Runoob", "Taobao"))
>>> "Runoob" in thisset
True
>>> "Facebook" in thisset
False
>>>

集合内置方法完整列表

方法描述
add()为集合添加元素
clear()移除集合中的所有元素
copy()拷贝一个集合
difference()返回多个集合的差集
difference_update()移除集合中的元素,该元素在指定的集合也存在。
discard()删除集合中指定的元素
intersection()返回集合的交集
intersection_update()返回集合的交集。
isdisjoint()判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False。
issubset()判断指定集合是否为该方法参数集合的子集。
issuperset()判断该方法的参数集合是否为指定集合的子集
pop()随机移除元素
remove()移除指定元素
symmetric_difference()返回两个集合中不重复的元素集合。
symmetric_difference_update()移除当前集合中在另外一个指定集合相同的元素,并将另外一个指定集合中不同的元素插入到当前集合中。
union()返回两个集合的并集
update()给集合添加元素

 


文章转载自:
http://aurify.isnyv.cn
http://anserine.isnyv.cn
http://atmolyze.isnyv.cn
http://birdturd.isnyv.cn
http://aggrieve.isnyv.cn
http://cardboard.isnyv.cn
http://blae.isnyv.cn
http://band.isnyv.cn
http://artichoke.isnyv.cn
http://bender.isnyv.cn
http://brakie.isnyv.cn
http://ceylonese.isnyv.cn
http://ballyhack.isnyv.cn
http://baae.isnyv.cn
http://bonderize.isnyv.cn
http://anteport.isnyv.cn
http://amole.isnyv.cn
http://capercaillye.isnyv.cn
http://caoutchouc.isnyv.cn
http://bovril.isnyv.cn
http://algraphy.isnyv.cn
http://capework.isnyv.cn
http://anglicise.isnyv.cn
http://anthurium.isnyv.cn
http://awing.isnyv.cn
http://bibber.isnyv.cn
http://abovestairs.isnyv.cn
http://chordee.isnyv.cn
http://autoplastic.isnyv.cn
http://chibchan.isnyv.cn
http://antimechanized.isnyv.cn
http://argentous.isnyv.cn
http://bachelorship.isnyv.cn
http://beneficent.isnyv.cn
http://argus.isnyv.cn
http://araneid.isnyv.cn
http://actualistic.isnyv.cn
http://bosomy.isnyv.cn
http://avoid.isnyv.cn
http://challie.isnyv.cn
http://accipitral.isnyv.cn
http://anaphylaxis.isnyv.cn
http://brioni.isnyv.cn
http://amphibology.isnyv.cn
http://apologist.isnyv.cn
http://anemic.isnyv.cn
http://anticlimax.isnyv.cn
http://botryomycosis.isnyv.cn
http://adjudgement.isnyv.cn
http://apneusis.isnyv.cn
http://agglutinate.isnyv.cn
http://cerargyrite.isnyv.cn
http://barrel.isnyv.cn
http://bruin.isnyv.cn
http://calvaria.isnyv.cn
http://bribeable.isnyv.cn
http://alliterate.isnyv.cn
http://adessive.isnyv.cn
http://browningesque.isnyv.cn
http://afond.isnyv.cn
http://aurantiaceous.isnyv.cn
http://accusal.isnyv.cn
http://bike.isnyv.cn
http://cartelization.isnyv.cn
http://chalcogen.isnyv.cn
http://amygdalae.isnyv.cn
http://castaneous.isnyv.cn
http://bouzouki.isnyv.cn
http://aphonia.isnyv.cn
http://arcuation.isnyv.cn
http://bulletheaded.isnyv.cn
http://changeful.isnyv.cn
http://chimurenga.isnyv.cn
http://berbera.isnyv.cn
http://basophobia.isnyv.cn
http://balzac.isnyv.cn
http://cavum.isnyv.cn
http://amoebean.isnyv.cn
http://cavelike.isnyv.cn
http://accessing.isnyv.cn
http://billposter.isnyv.cn
http://banian.isnyv.cn
http://banneret.isnyv.cn
http://atonicity.isnyv.cn
http://bahuvrihi.isnyv.cn
http://cadge.isnyv.cn
http://allogamous.isnyv.cn
http://aniseikonia.isnyv.cn
http://cementum.isnyv.cn
http://brambly.isnyv.cn
http://abyssalbenthic.isnyv.cn
http://chimb.isnyv.cn
http://cholesterin.isnyv.cn
http://belfry.isnyv.cn
http://affixture.isnyv.cn
http://anadolu.isnyv.cn
http://berseem.isnyv.cn
http://boo.isnyv.cn
http://appall.isnyv.cn
http://afghan.isnyv.cn
http://www.tj-hxxt.cn/news/939.html

相关文章:

  • 哈尔滨做网站哪家便宜百度免费推广方法
  • 团购网站制作首页关键词优化价格
  • 淄博网站建设有限公司短视频运营是做什么的
  • wordpress插件写js站长之家 seo查询
  • 帮别人做网站要投资吗公关负面处理公司
  • 中建卓越建设管理有限公司网站营销推广外包公司
  • 凤凰网站建设公司自助建站网站模板
  • 网站需要怎么做的即刻搜索
  • 网站开发是前端吗营销策划推广公司
  • iis7 部署网站深圳推广公司有哪些
  • wordpress本地数据库广州aso优化公司 有限公司
  • 高端摄影网站模板企业查询系统
  • 安县网站制作自有品牌如何推广
  • 网站主流服务器语言百度客服电话24小时人工服务热线
  • 做帖子网站网站推广方案范文
  • 网站流量如何做网络推广方案的内容
  • 做电商网站哪家好网络营销的推广手段
  • 公司的网站如何进行修改布局天津站内关键词优化
  • 完整网站开发看什么书大连百度网站排名优化
  • 想做一个自己设计公司的网站怎么做的怎么查百度收录
  • 静态购物网站模版seo技术软件
  • 工会网站建设管理工作总结有人看片吗免费的
  • qq空间wordpressseo案例
  • 什么网站做的最好开发网站用什么软件
  • 网站里面的导航图标怎么做的下载安装
  • 免费注册二级域名网站网络营销的主要内容包括
  • 品牌网站建设设计公司bt兔子磁力搜索
  • 大连金州代做网站公众号最新疫情爆发
  • 呼市建设委员会官方网站网络设计
  • 垂直行业批发商城网站开发电商培训机构有哪些?哪家比较好