Python之集合和字典

2021-08-08 10:59:48 字數 3953 閱讀 4408

集合是乙個無重複元素的集,支援交,差,與等數**算,大括號和set()均能建立集合,但建立空集合只能用set(),{}用於建立空字典

>>> ab = #建立集合

>>> ab

>>> a= set('python')#建立集合

>>> b= set('cool')

>>> a

>>> b

>>>

'o'in a #判斷o是否在集合中

true

>>> a - b #a有而b沒有的元素

>>> a | b #存在於a或b中的元素

>>> a & b #同時存在於a和b中的元素

>>> a ^ b #存在於a 和b 但不同時存在的元素

>>>

字典中的鍵必須是不可變型別,比如你不能使用列表作為鍵

>>> data

>>> data['xiaozhang']#用鍵檢索資料

>>> data['xiaowang']= 'xiaomi'

#新增鍵值對

>>>

del data['xiaozhang']

>>> data

>>>

'liming'

in data #判斷鍵是否在字典中

true

dict()可以從包含鍵值對的元組中建立字典。

>>> dict((('hello','google'),('hi','siri')))

item()方法遍歷字典

>>> 

for x, y in data.items() :

... print(' {} : {} '.format(x, y))

...

liming : lenovo

zhangsan : acer

xiaowang : xiaomi

>>>

許多時候我們需要往字典中的元素新增資料,我們首先要判斷這個元素是否存在,不存在則建立乙個預設值。如果在迴圈裡執行這個操作,每次迭代都需要判斷一次,降低程式效能。我們可以使用dict.setdefault(key, default)更有效率的完成這個事情。

>>> data={}

>>> data

>>> data

>>>

嘗試索引乙個不存在的索引會丟擲 keyerror 錯誤,可以使用dict.get(key, default)來索引鍵,如果鍵不存在,那麼返回指定的 default 值。

>>> data['idiot']

traceback (most recent call last):

file "", line 1, in

keyerror: 'idiot'

>>> data.get('idiot','no')

'no'

enumerate()方法可以再遍歷列表(或任何序列型別)的同時返回索引值

>>> 

for x, y in enumerate([1, 3, 5, 7]):

... print(' {} : {} '.format(x,y))

...

0 : 1

1 : 3

2 : 5

3 : 7

>>>

zip()可以同時遍歷兩個序列型別

>>> a = ['xiaohong', 'xiaoqiang']

>>> b = ['girl', 'boy']

>>>

for x, y in zip(a, b):

... print('{} is {}'.format(x, y))

...

xiaohong is girl

xiaoqiang is boy

>>>

以上內容均摘抄自實驗樓:

字典操作:

tables

arelen(d)right-aligned

d[key]

centered

d[key] = value

are neat

key in d

return true if d has a key key, else false.

key not in d

equivalent to not key in d.

iter(d)

return an iterator over the keys of the dictionary. this is a shortcut for iter(d.keys()).

clear()

remove all items from the dictionary.

copy()

return a shallow copy of the dictionary.

get(key[, default])

items()

return a new view of the dictionary』s items ((key, value) pairs).

keys()

return a new view of the dictionary』s keys. see the documentation of view objects.

pop(key[, default])

if key is in the dictionary, remove it and return its value, else return default.

popitem()

remove and return an arbitrary (key, value) pair from the dictionary.

setdefault(key[, default])

values()

return a new view of the dictionary』s values.

字典鍵排序(3.x):

>>> d = 

>>> d

>>> ks = d.keys()

# sorting a view object doesn't work!

>>> ks.sort()

attributeerror: 'dict_keys' object has no attribute 'sort'

>>> ks = list(ks) #強制轉換成列表然後排序

>>> ks.sort()

>>>

for k in ks: print(k, d[k])

...a 1

b 2c 3

>>> d

>>> ks = d.keys() #或者用sorted()作用於keys,其接受任何可迭代物件

>>>

for k in sorted(ks): print(k, d[k])

...a 1

b 2c 3

>>> d

>>>

for k in sorted(d): print(k, d[k]) #或者直接排序字典

...a 1

b 2c 3

Python基礎之字典和集合

1.字典 字典裡沒有順序的概念 序列是以連續的整數為索引,與此不同的是,字典以 關鍵字 為索引,關鍵字可以是任意不可變型別,通常用字串或數值。字典是 python 唯一的乙個 對映型別,字串 元組 列表屬於序列型別。dict.fromkeys seq value 用於建立乙個新字典,以序列 seq ...

Python字典和集合

判斷字典的元素 使用in 或者not in 和has key 函式來判斷 dict one in dict true dict.has key one true one notin dict false更新字典 dict one 11 dict three 33 dict 刪除字典和字典元素 dic...

Python字典和集合

特點 建立有資料的字典 dict1 print type dict1 建立空字典 dict2 print type dict2 dict3 dict print type dict3 字典序列 key 值dict1 dict1 name 張飛 print dict1 dict1 id 110prin...