元組和集合

2021-10-21 14:51:38 字數 4929 閱讀 9683

集合pyhton內建的資料結構之一,是乙個不可變序列,沒有增刪改操作。包括字串和元組。

可變序列具有增刪改操作,且經過該操作後,記憶體位址不發生更改。

# 直接小括號,元素與元素之間用逗號分隔

t =(

'hello'

,'good'

,'morning'

)t2 =

'hello'

,'good',10

# 小括號可以省略

print

(t)print

(t2,

type

(t2)

)# 使用內建函式tuple()

t=tuple((

'hello'

,'good'

,'morning'))

print

(t)# 只包含乙個元素的元組需要使用逗號和小括號t=(

10,)# 逗號不可省略,否則就是原資料型別

print

(t)# 空元組

t1=(

)t2=

tuple()

print

(t1,t2)

('hello', 'good', 'morning')

('hello', 'good', 10) ('hello', 'good', 'morning')

(10,)

() ()

為什麼要將元組設計成不可變序列?

在多工環境下,同時操作物件時不需要加鎖,因此,在程式中盡量使用不可變序列。

注意事項:元組中儲存的是物件的引用

a)如果元組中物件本身不可物件,則不能再引用其他物件;

b)如果元組中的物件是可變物件,則可變物件的引用不允許改變,但資料可以改變

# 元組是可迭代物件,可以使用for...in進行遍歷

t =(

'hello'

,'good'

,100

)for item in t:

print

(item,

type

(item)

)

hello good 100
# 可以根據索引提取元組元素

t =(

'hello'

,'good'

,100

)print

(t[0

])

hello
python語言提供的內建資料結構,與列表、字典一樣都屬於可變的序列;集合是沒有value的字典

# 直接{}建立,元素與元素之間用逗號隔開

s =print

(s,type

(s))

s_1 =

# 集合中的元素不可重複

print

(s_1)

# 使用內建函式set()

s =set

(range(5

))print

(s,type

(s))

s_1=

set([1

,2,3

,4,5

])# 將列表轉化為集合

s_2=

set((1

,2,3

,4,67

))# 將元組轉化為集合,集合是無序的

s_3=

set(

('python'))

# 將轉化為集合,集合是無序的

s_4=

set(

)# 將集合轉化為集合,集合是無序的

print

(s_1)

print

(s_2)

print

(s_3)

print

(s_4)

# 定義乙個空集合

s_5 =

set(

)print

(s_5,

type

(s_5)

)s_6 =

# 建立的是空字典

print

(s_6,

type

(s_6)

)

set() {}
# 判斷某元素是否在集合內,in / not in

s =set

(range(5

))print(0

in s)

print(6

notin s)

true

true

# 新增元素

# 呼叫add()方法,一次新增乙個元素

s =s.add(9)

print

(s)s.update(

)# 重複的不加入,可新增列表/元組

print

(s)# 呼叫update()方法至少調價乙個元素

# 集合元素的刪除操作

s =# 一次刪除乙個指定元素,若元素不存在則返回keyerror

s.remove(6)

print

(s)# 呼叫discard()方法,一次刪除乙個指定元素,若元素不存在,不會報錯

s.discard(99)

print

(s)

# 呼叫pop()方法,一次只刪除乙個任意元素

s.pop(

)print

(s)

# 呼叫clear()方法,清空集合

s.clear(

)print

(s)

set()
# 兩個集合是否相等,可以使用==或!=進行判斷

s1 =

s2 =

print

(s1==s2)

print

(s1!=s2)

true

false

# 乙個集合是否是另乙個集合的子集,可以呼叫方法issubset進行判斷

s1 =

s2 =

s3 =

print

(s1.issubset(s2)

)print

(s3.issubset(s2)

)

true

false

# 乙個集合是否是另乙個集合的超集,可以呼叫方法issuperset進行判斷

s1 =

s2 =

s3 =

print

(s2.issuperset(s1)

)print

(s2.issuperset(s3)

)

true

false

# 兩個集合是否沒有交集,可以呼叫方法isdisjoint進行判斷

s1 =

s2 =

s3 =

print

(s1.isdisjoint(s3)

)print

(s2.isdisjoint(s3)

)

true

false

s2 =

s3 =

# 交集

print

(s2.intersection(s3)

)print

(s2 & s3)

# 並集

s2 =

s3 =

print

(s2.union(s3)

)print

(s2|s3)

# 差集

s2 =

s3 =

print

(s2.difference(s3)

)print

(s2-s3)

print

(s3.difference(s2)

)print

(s3-s2)

# 對稱差集

元組和集合

tul new 12,13,14,abc 元組 元組裡面的元素可以是不同的型別,元組可以不需要加括號。元組中只有乙個元素,型別會顯示是個整形。但是要得到是個元組,必須加上 號 例tul new 12,print tul new 0 訪問指定的元素 修改元組,在python中不支援修改其中的元素 例 ...

元組,字典和集合

字典 dict 集合 set 集合的運算 元組是乙個不可變序列,一般當我們不希望儲存的資料改變時,我們使用元組,其他情況一般使用列表。tup 1,2,4,5,6 tup 3 12 typeerror tuple object does not support item assignment prin...

Python元組 列表和集合

atuple 3,2,6 sorted atuple 2,3,6 atuple.sort traceback most recent call last file line 1,in atuple.sort attributeerror tuple object has no attribute s...