python3字典詳解 python3中字典詳解

2021-10-25 19:40:25 字數 3458 閱讀 9727

字典(dict)

1. 建立字典的幾種方式

class dict(**kwarg)

class dict(iterable, **kwarg)

使用上面的方法構建字典`

方法1構建字典`:

a = dict(one=1, two=2, three=3)

a輸出結果:

方法2構建字典:

a = dict()

a輸出結果:

方法3構建字典:

d = dict([('two', 2), ('one', 1), ('three', 3)])

print('d =', d)

e = dict(zip(['one', 'two', 'three'], [1, 2, 3]))

print('e =', e)

輸出結果:

d =

e =

方法4構建字典:

d =

d輸出結果:

方法5構建字典:

# 建立乙個空字典

a = dict()

# 通過賦值語句構造字典

a['one'] = 1

a['two'] = 2

a['three'] = 3

a輸出結果:

2. 對字典可以運用的一些操作

len(d): 返回字典的長度

a = dict(one=1, two=2, three=3)

len(a)

輸出結果:

d[key1]:返回字典中key等於key1的值

a = dict(one=1, two=2, three=3)

a['one']

輸出結果:

del d[key]:刪除字典中key為key的值

a = dict(one=1, two=2, three=3)

del a['two']

a輸出結果:

key in d 或者 key not in d:判斷字典中有沒有key為key的值

a = dict(one=1, two=2, three=3)

print('one' in a)

print('four' in a)

輸出結果:

true

false

iter(d):返回字典中key的迭代器

a = dict(one=1, two=2, three=3)

d_iter = iter(a)

[x for x in d_iter]

輸出結果:

['one', 'three', 'two']

3. 字典中的方法

dic.clear():將字典清空

a = dict(one=1, two=2, three=3)

a.clear()

print(a)

輸出結果:

dic.copy():淺複製複製乙個字典。淺拷貝不會拷貝子物件,所以原始資料改變,子物件也會改變

a = dict(one=1, two=2, three=3)

b = a.copy()

print('a =', a)

print('b =', b)

# 更改b中的值

b['four'] = 4

print('updated:')

print('a =', a)

print('b =', b)

# 另外一種情況

print()

x =

y = x.copy()

y["c"].remove("asd")

y["a"] = "***"

print('x=', x)

print('y=', y)

輸出結果:

a =

b =

updated:

a =

b =

x= y=

dic.items():返回字典中(key,value)對的迭代器

a = dict(one=1, two=2, three=3)

print(a.items())

輸出結果:

dict_items([('one', 1), ('three', 3), ('two', 2)])

dic.keys():返回字典中key的迭代器

a = dict(one=1, two=2, three=3)

print(a.keys())

輸出結果:

dict_keys(['one', 'three', 'two'])

dic.values():返回字典中值的迭代器

a = dict(one=1, two=2, three=3)

print(a.values())

輸出結果:

dict_values([1, 3, 2])

dic.update([other]):更新字典

a = dict(one=1, two=2, three=3)

# 使用引數

a.update(four=4)

print(a)

# 使用字典來更新

other =

a.update(other)

print(a)

# 使用迭代器

a.update([('seven', 7),('eight', 8)])

print(a)

# 注意上面使用字典和迭代器來更新字典時,需要增加的字典和迭代器的長度大於2,否則會出現錯誤

輸出結果:

dic.popitem():隨機刪除一項,並返回鍵值對

a = dict(one=1, two=2, three=3)

print(a.popitem())

print('a =', a)

輸出結果:

('one', 1)

a =

dic.pop(key[,default):刪除並返回給定鍵的值,並刪除鍵值對

a = dict(one=1, two=2, three=3)

print(a.pop('one'))

print(a.pop('four', 4))

輸出結果:

dic.get(key[,default]):返回給定key的值,如果字典中沒有key,則返回default值

a = dict(one=1, two=2, three=3)

print(a.get('two'))

print(a.get('four'), 3)

輸出結果:

none 3

dic.setdefault(key[,default]):和dic.get()類似,如果沒有key,則將新增到字典中

a = dict(one=1, two=2, three=3)

print(a.setdefault('two'))

print(a.setdefault('four', 4))

print(a)

輸出結果:

python3字典遍歷 python3字典遍歷

python版本 python3.7 info infog.get name 得到字典info中name的值 info.keys 得到字典info中所有的鍵,結果是乙個物件 dict keys name age 需要注意在python2中該操作得到的是乙個列表 遍歷key for temp in i...

python3 字典問題詳解

1.1 定義字典 dict 1.2 根據鍵值訪問對應元素值 dict name 1.3 遍歷所有鍵 值對 for k,v in dict.items 1.4 遍歷所有鍵 for k in dict.keys 1.5 遍歷所有值 for v in dict.values 1.6 刪除以 name 為鍵...

python3字典排序

說實話,對字典進行排序,這個說法本身就有問題,實際上,你無法對操縱字典說,字典,在你的底層實現裡,你就得按照我指定的順序來排列,如果這樣的話,字典就喪失了它的速度優勢,它也不是乙個字典了.好了,廢話不多說,我這裡稍微記錄一下我的做法吧.python2裡面原來是有dict.iteritems這樣乙個函...