導航:首頁 > 編程語言 > python實驗字典集合函數

python實驗字典集合函數

發布時間:2022-08-21 23:16:08

python怎麼創建集合

創建集合

創建集合可以使用大括弧{}來創建,元素間用逗號分隔,但是不能用它來創建空集合,因為{}創建的是空字典。

如 set1 = {1,2,3}

也可以使用set()函數來創建集合,其中的參數為可迭代對象即可(比如字元串、列表、元組、迭代器等),前提是元素中必須是不可變類型。

② 求助一個Python問題,用字典與集合的知識解下面這個題,感謝大佬幫忙🙏

course1 = set(('李雷', '張玉', '王曉剛', '陳紅靜', '方向', '司馬清'))

course2 = set(('施然', '李芳芳', '劉瀟', '方向', '孫一航', '黃煌'))

course3 = set(('陳紅靜', '方向', '劉培良', '張玉', '施小冉', '司馬清'))

d={}

data=[x for y in [course1,course2,course3]

for x in y]for x in data:

if x in d:

d[x]+=1

else:

d[x]=1

print(d,' ')

print('這個班還有 %d 學生沒有選課(by set)' % (25-len(course1|course2|course3)))

print('這個班還有 %d 學生沒有選課(by dict)' % (25-len(d)))

print('有 %d 位學生同時選修了2門課' % list(d.values()).count(2))

print('有 %d 位學生同時選修了3門課' % list(d.values()).count(3))

print('有 %d 位學生同時選修了1門課' % list(d.values()).count(1))

③ 8.Python內置函數_________可以返回列表、元組、字典、集合、字元串以及range對象

len()
0.0

④ Python中字典的內建函數用法是什麼

字典內置函數&方法
Python字典包含了以下內置函數:
1 cmp(dict1, dict2)
比較兩個字典元素。
2 len(dict)
計算字典元素個數,即鍵的總數。
3 str(dict)
輸出字典可列印的字元串表示。
4 type(variable)
返回輸入的變數類型,如果變數是字典就返回字典類型。

⑤ python集合

*事先說明:以下代碼及結果來自本設備Python控制台,在不同設備上可能結果有區別,望自己嘗試為妙


集合(set),是一種Python里的(class),

集合類似於列表(list)可更改可迭代(iterable),但是元素不重復

定義集合使用花括弧{},例如

>>> s = {"apple", "banana", "strawberry", "watermelon"}

警告!!!如果使用空括弧

>>> a = {}

>>> a.__class__

<class 'dict'>

a將成為一個字典

想要定義空集合,

請使用類名。

>>> a = set()

類名定義也可以把迭代轉換為集合:

>>> b = set("abc")

>>> b

{'a', 'b', 'c'}

但是,保存後它是無序的。

>>> s

{'banana', 'watermelon', 'strawberry', 'apple'}

(結果僅供參考,在不同設備上略有不同)

下面介紹它的性質:

1. 可更改:

>>> s.add("lemon")

>>> s

{'banana', 'strawberry', 'lemon', 'watermelon', 'apple'}


>>> s.update(("orange", "grape"))

>>> s

{'banana', 'strawberry', 'orange', 'lemon', 'watermelon', 'apple', 'grape'}

警告!!!如果使用字元串,字元串也會被迭代:

>>> a = set()

>>> a.update("apple")

>>> a

{'a', 'p', 'e', 'l'}


>>> s.remove("lemon")

>>> s

{'banana', 'strawberry', 'orange', 'watermelon', 'apple', 'grape'}

>>> s.remove("cat")

Traceback (most recent call last):

File "<stdin>", line 1, in <mole>

s.remove("cat")

KeyError: 'cat'


>>> s.discard("grape")

>>> s

{'banana', 'strawberry', 'orange', 'watermelon', 'apple'}

>>> s.discard("dog")

>>> s

{'banana', 'strawberry', 'orange', 'watermelon', 'apple'}

2. 可迭代:

>>> for x in s:

... print(x)


banana

strawberry

orange

watermelon

apple


3. 可以使用 len 函數獲取集合長度;

>>> len(s)

5

可以使用in關鍵字檢查一個元素是否在集合內,將返回邏輯值(bool)

>>> "apple" in s

True

>>> "candy" in s

False

4.集合具有不重復性,因此它會自動去重:

>>> c = set("Hello World!")

>>> c

{' ', 'e', 'l', 'H', '!', 'r', 'W', 'o', 'd'}

5. 集合的運算

>>> d = set("abcdef")

>>> e = set("efghij")

>>> d

{'c', 'e', 'a', 'b', 'f', 'd'}

>>> e

{'h', 'e', 'g', 'j', 'f', 'i'}

>>> d - e # 集合d去掉集合e含有的元素,或者說集合d包含而集合e不包含的元素(不改變原集合)

{'a', 'b', 'd', 'c'}

>>> d | e # 集合d,e的所有元素

{'c', 'e', 'h', 'g', 'a', 'b', 'j', 'f', 'd', 'i'}

>>> d & e # 集合d,e都包含的元素

{'f', 'e'}

>>> d ^ e # 不同時出現在集合d, e中的元素

{'c', 'g', 'h', 'a', 'b', 'j', 'd', 'i'}

注意!!!

字元串、列表通用的+和*不適用於集合

>>> "abc" + "def"

'abcdef'

>>> [1, 2, 3] + [4, 5, 6]

[1, 2, 3, 4, 5, 6]

>>> d + e

Traceback (most recent call last):

File "<stdin>", line 1, in <mole>

d + e

TypeError: unsupported operand type(s) for +: 'set' and 'set'

>>> "a" * 5

'aaaaa'

>>> [1] * 3

[1, 1, 1]

>>> d*3

Traceback (most recent call last):

File "<stdin>", line 1, in <mole>

d * 2

TypeError: unsupported operand type(s) for *: 'set' and 'int'

(作者的小觀點:既然集合是不能重復的,那麼乘以、重復是沒有意義的)

⑥ python中集合的特點和注意點

1、集合概念

Python中的集合,是一個無序的、沒有重復項的集。它支持數學概念上的集合操作,如交集、並集、補集和差集。集合是可變的,可以在其中添加或刪除項。集合用花括弧「{}」括起來,並用逗號「,」來分隔其中的項。

2、創建集合

可以使用花括弧「{}」創建集合,集合會自動去除重復的項。下面的集合包含了幾種用字元串表示的水果:

⑦ Python字典中幾個常用函數總結

1、get() 返回指定鍵的值,如果值不在字典中返回default值。
語法:dict.get(key,default=None)
參數:
key 字典中要查找的鍵。
default 如果指定鍵的值不存在時,返回該默認值值。
例:
dict={'Name':'alex','Age':21}
print("Name is:%s"% dict.get('Name')+"\n"+ "Age is:%d"% dict.get('Age'))
顯示結果為:
Name is:alex
Age is:21

2、update() 將一個字典中的值更新到另一個字典中。
語法:dict.update(dict2)
參數:
dict2 添加到指定字典dict里的字典。
例:
dict={'Name':'alex','Age':21}
dict2={'Sex':'female'}
dict.update(dict2)
print("Value is %s" % dict)
顯示結果為:
Value is {'Name': 'alex', 'Age': 21, 'Sex': 'female'}

⑧ python集合的作用有哪些

集合可以像元組一樣,設置不可改變的類型;也可以默認像字典,列表一樣,可以迭代改變;同時集合里的元素可以是列表,元組,字典。


1、python集合的作用——創建字典


可使用大括弧 { } 或者 set() 函數創建集合,注意:創建一個空集合必須用 set() 而不是 { },因為 { } 是用來創建一個空字典。


>>> my_set = set(('apple',))


>>> my_set


{'apple'}


2、python集合的作用——添加元素(add(),update())


# add 作為一個整體添加到集合中


my_set = set()


my_set.add("abc")


print(my_set)


#update 是把要傳入的元素拆分,做為個體傳入到集合中


my_set = set()


my_set.update("abc")


print(my_set)


3、python集合的作用——刪除元素(remove(),pop(),discard())


#remove 如果集合種有,則直接刪除;如果沒有,程序報錯 格式:集合名.remove(元素)


my_set = {11, 13, 15}


my_set.remove(13)


print(my_set) my_set.remove(131)


print(my_set)


#pop 隨機刪除集合中的元素 如果集合中沒有元素則程序報錯


my_set = {11, 13, 15}


my_set.pop()


print(my_set)


#discard 如果集合中元素存在,直接刪除; 如果元素不存在 不做任何操作 格式: 集合名.discard(元素)


my_set = {11, 13, 15}


my_set.discard(13)


print(my_set)


my_set.discard(131)


print(my_set)


4、python集合的作用——交集( & )


set1 = {9, 2, 3, 4}


set2 = {3, 4, 5, 16}


new_set = set1 & set2


print(new_set)


# result {3, 4}


5、python集合的作用——並集( | )


set1 = {1, 2, 3, 4}


set2 = {3, 5, 5, 6}[page]


new_set = set1 | set2


print(new_set)


# result {1, 2, 3, 4, 5, 6}


6、python集合的作用——差集(-)


項在前一個集合中,但不在後一個集合中。


set1 = {1, 2, 3, 4}


set2 = {3, 4, 5, 6}


new_set = set1 - set2


print(new_set)


# result {1, 2}


7、python集合的作用——對稱差集( ^ )


即項在前一個集合或後一個集合中,但不會同時出現在二者中。即交集減去並集。


set1 = {1, 2, 3, 4}


set2 = {3, 4, 5, 6}


new_set = set1 ^ set2


print(new_set)


# result {1,2,5,6}


8、python集合的作用——子集判斷


set1 = { 3, 4}


set2 = {3, 4, 5, 6}


# 判斷set1是否是set2的子集


print(set1.issubset(set2))


# result True


9、python集合的作用——父集判斷


set1 = { 3, 4}


set2 = {3, 4, 5, 6}


# 判斷set1是否是set2的父集


print(set1.issuperset(set2))


# result False


10、python集合的作用——迭代和枚舉


s={34,56,76,34,56,54,43,23,56}


for i in s:


print(i) ##迭代輸出其內容


for i,v in enumerate(s):


print('index: %s,value: %s' %(i,v))


"""


result:


index: 0,value: 34


index: 1,value: 43


index: 2,value: 76


index: 3,value: 54


index: 4,value: 23


index: 5,value: 56


"""


可觀察出,集合會自動過濾掉相同元素。


python集合的作用都有哪些?原來這些功能才是最實用的,集合可以像元組一樣,設置不可改變的類型;也可以默認像字典,列表一樣,可以迭代改變;同時集合里的元素可以是列表,元組,字典,你能處理好嗎?如果您還擔心自己入門不順利,可以點擊本站的其他文章進行學習。

閱讀全文

與python實驗字典集合函數相關的資料

熱點內容
pdf手寫筆 瀏覽:173
別永遠傷在童年pdf 瀏覽:984
愛上北斗星男友在哪個app上看 瀏覽:414
主力散戶派發源碼 瀏覽:665
linux如何修復伺服器時間 瀏覽:55
榮縣優途網約車app叫什麼 瀏覽:473
百姓網app截圖是什麼意思 瀏覽:222
php如何嵌入html 瀏覽:811
解壓專家怎麼傳輸 瀏覽:743
如何共享伺服器的網路連接 瀏覽:132
程序員簡易表白代碼 瀏覽:167
什麼是無線加密狗 瀏覽:63
國家反詐中心app為什麼會彈出 瀏覽:68
cad壓縮圖列印 瀏覽:102
網頁打開速度與伺服器有什麼關系 瀏覽:863
android開發技術文檔 瀏覽:65
32單片機寫程序 瀏覽:52
三星雙清無命令 瀏覽:839
漢壽小程序源碼 瀏覽:345
易助erp雲伺服器 瀏覽:533