欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python常用數(shù)據(jù)結(jié)構(gòu)集合詳解

 更新時(shí)間:2022年08月26日 10:51:57   作者:小han的日常  
這篇文章主要介紹了python常用數(shù)據(jù)結(jié)構(gòu)集合詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助

set集合

集合定義與使用

集合定義:

  • --無序的唯一對(duì)象集合
  • --用大括號(hào){}包圍,對(duì)象相互之間使用逗號(hào)分隔
  • --集合是動(dòng)態(tài)的,可以隨時(shí)添加或刪除元素
  • --集合是異構(gòu)的,可以包含不同類型的數(shù)據(jù)

集合使用:創(chuàng)建

創(chuàng)建:

  • --通過使用{}填充元素
  • --通過構(gòu)造方法set()
  • --通過集合推導(dǎo)式
# 集合使用:創(chuàng)建
# 創(chuàng)建
# --通過使用{}填充元素
a = {1, 2, 3}
print(type(a), a)
# --通過構(gòu)造方法set()
b = set("1")
print(type(b), b)
c = [1, 2, 3, "happy"]
d = set(c)
print(type(d), d)
# --通過集合推導(dǎo)式
e = {i for i in c}
print(type(e), e)
# --注意:不可傳空{(diào)}來定義集合,會(huì)得到字典類型
f = {}
print(type(f))

集合使用:成員檢測(cè)

  • in:判斷元素是否在集合中存在
  • not in :判斷元素是否在集合中不存在
# 集合使用:成員檢測(cè)
# in:判斷元素是否在集合中存在
# not in :判斷元素是否在集合中不存在
a = {1, 2, 3, 4, 5}
print(1 in a)
print(6 in a)
print(2 not in a)
print(6 not in a)

集合常用方法

add()

add(item):將單個(gè)對(duì)象添加到集合中

入?yún)ⅲ?/strong>對(duì)象item

返回:None

# add()
# add(item):將單個(gè)對(duì)象添加到集合中
# 入?yún)ⅲ簩?duì)象item
# 返回:None
a = {1, 2, 3}
a.add(4)
print(a)

update()

update(iterable)

批量添加來自可迭代對(duì)象中的所有元素

入?yún)ⅲ?/strong>可迭代對(duì)象tierable

返回:None

# update()
# update(iterable)
# 批量添加來自可迭代對(duì)象中的所有元素
# 入?yún)ⅲ嚎傻鷮?duì)象iterable
# 返回:None
a = {1, 2, 3}
a.update("happy")
print(a)

remove()

remove(item):從幾個(gè)中移除指定元素item

入?yún)ⅲ?/strong>指定元素值

返回:None

如果item不存在與集合中則會(huì)引發(fā)KeyError

# remove()
# remove(item):從幾個(gè)中移除指定元素item
# 入?yún)ⅲ褐付ㄔ刂?
# 返回:None
# 如果item不存在與集合中則會(huì)引發(fā)KeyError
a = {1, 2, 3, 4}
a.remove(1)
print(a)

discard()

discard(item):從集合中移除指定對(duì)象item

入?yún)ⅲ?/strong>指定對(duì)象值

返回:None

元素item不存在沒印象,不會(huì)拋出KeyError錯(cuò)誤

# discard()
# discard(item):從集合中移除指定對(duì)象item
# 入?yún)ⅲ褐付▽?duì)象值
# 返回:None
# 元素item不存在沒印象,不會(huì)拋出KeyError錯(cuò)誤
a = {1, 2, 3, "happy"}
a.discard(2)
print(a)

pop()

pop():隨機(jī)從集合中移除并返回一個(gè)元素

入?yún)ⅲ?/strong>無

返回:被移除的元素

如果集合為空則會(huì)引發(fā)KeyError錯(cuò)誤

# pop()
# pop():隨機(jī)從集合中移除并返回一個(gè)元素
# 入?yún)ⅲ簾o
# 返回:被移除的元素
# 如果集合為空則會(huì)引發(fā)KeyError錯(cuò)誤
a = {1, 2, 3, 4, "happy"}
print(a.pop())
print(a)

clear()

clear():清空集合,移除所有元素

入?yún)ⅲ?/strong>無

返回:None

# clear()
# clear():清空集合,移除所有元素
# 入?yún)ⅲ簾o
# 返回:None
a = {1, 2, 3, "happy"}
a.clear()
print(a)

集合運(yùn)算

交集運(yùn)算

intersection()

操作符:&

# 交集運(yùn)算
# intersection()
# 操作符:&
a = {1, 2, 3}
b = {2, 3, 4}
print(a.intersection(b))
print(a & b)

并集運(yùn)算

union()

操作符:|

# 并集運(yùn)算
# union()
# 操作符:|
a = {1, 2, 3}
b = {2, 3, 4, 5}
print(a.union(b))
print(a | b)

差集運(yùn)算

difference()

操作符:-

# 差集運(yùn)算
# difference()
# 操作符:-
a = {1, 2, 3, 4}
b = {2, 3, 4, 5}
print(a.difference(b))
print(a - b)
print(b.difference(a))
print(b - a)

集合推導(dǎo)式

類似列表推導(dǎo)式,同時(shí)集合支持集合推導(dǎo)式

語法:{i for i in ... if ...}

# 集合推導(dǎo)式
# 類似列表推導(dǎo)式,同時(shí)集合支持集合推導(dǎo)式
# 語法:{i for i in ... if ...}
a = {i for i in [1, 2, 3, 4] if i % 2 == 0}
print(a)

到此這篇關(guān)于python常用數(shù)據(jù)結(jié)構(gòu)集合詳解的文章就介紹到這了,更多相關(guān)python 集合內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論