Python數據類型之字典(Dictionary) 字典特征 1. 特征 可變、無序、映射、鍵值對 2. 形式 {key0:value0, key1:value1, key2:value3, ..., } key :唯一性,key的數據類型必須是固定不可變的,如數字、字元串、元組、凍結集合 valu ...
Python數據類型之字典(Dictionary)
字典特征
- 特征
可變、無序、映射、鍵值對 - 形式
{key0:value0, key1:value1, key2:value3, ..., }
key:唯一性,key的數據類型必須是固定不可變的,如數字、字元串、元組、凍結集合
value:任意python數據類型
創建字典容器對象
my_study = {'數學':'math is a kind of art', '語文':'writing', '外語':'communication'}
print(my_study)
{'數學': 'math is a kind of art', '語文': 'writing', '外語': 'communication'}
my_character = {1:'beauty', 2:'handsome', 3:'happy', 4:'good'}
print(my_character)
{1: 'beauty', 2: 'handsome', 3: 'happy', 4: 'good'}
my_test = {(1,2):12, (3,4):34, (4,5):56}
print(my_test)
{(1, 2): 12, (3, 4): 34, (4, 5): 56}
my_test_temp = {'a':66, 1:'1', (0,0):[1,2,3], ('a','b'):{'1':1,'2':2}}
print(my_test_temp)
{'a': 66, 1: '1', (0, 0): [1, 2, 3], ('a', 'b'): {'1': 1, '2': 2}}
my_good_dict = {frozenset({1,2,3}):123, frozenset({4,5,6}):456}
print(my_good_dict)
{frozenset({1, 2, 3}): 123, frozenset({4, 5, 6}): 456}
字典的常見操作
- 向字典中添加元素
I_dict = {1:'1', 2:'2', 3:'3'}
I_dict[4] = '4' # 添加4:'4'這個鍵值對
print(I_dict)
{1: '1', 2: '2', 3: '3', 4: '4'}
I_dict.setdefault('My_add') # 添加 'My_add':None這個鍵值對
print(I_dict)
{1: '1', 2: '2', 3: '3', 4: '4', 'My_add': None}
I_dict.setdefault(5,'5') # 添加5:'5'這個鍵值對,返回'5'
print(I_dict)
{1: '1', 2: '2', 3: '3', 4: '4', 'My_add': None, 5: '5'}
I_dict_1 = {'a':1,'b':2,'c':3, 1:'a'}
I_dict.update(I_dict_1) # 用I_dict_1更新當前字典,key值重覆的採用更新的值替換
I_dict
{1: 'a',
2: '2',
3: '3',
4: '4',
'My_add': None,
5: '5',
'a': 1,
'b': 2,
'c': 3}
- 查找字典元素
dict_temp = {'1':1,'2':2,'3':3,'4':4,'a':[1,2,3],'b':(1,2,3)}
print(dict_temp['a'])
[1, 2, 3]
print(dict_temp.get('2'))
2
print(dict_temp.keys())
dict_keys(['1', '2', '3', '4', 'a', 'b'])
print(dict_temp.values())
dict_values([1, 2, 3, 4, [1, 2, 3], (1, 2, 3)])
print(dict_temp.items())
dict_items([('1', 1), ('2', 2), ('3', 3), ('4', 4), ('a', [1, 2, 3]), ('b', (1, 2, 3))])
- 字典的元素刪除操作
keys = list(range(4))
values = [x for x in 'abcd']
dict_ = dict(zip(keys, values))
dict_
{0: 'a', 1: 'b', 2: 'c', 3: 'd'}
dict_.pop(2) #刪除指定的鍵並返回相應的值,如果字典中沒有該鍵則顯示 KeyError 錯誤
'c'
dict_
{0: 'a', 1: 'b', 3: 'd'}
dict_.popitem() #列印刪除的鍵值對(以元組的形式返回),隨機返回並刪除字典中的一對鍵和值(一般刪除末尾對)
(3, 'd')
dict_
Out[32]: {0: 'a', 1: 'b'}
dict_.clear() #清空字典
dict_
{ }
del dict_ #直接刪除字典
dict_
Traceback (most recent call last):
File "<ipython-input-37-247f529b331d>", line 1, in <module>
dict_
NameError: name 'dict_' is not defined
- 修改字典中的元素
dictionary = dict(zip(list(range(5)),[str(x) for x in range(5)])) #創建字典
dictionary
{0: '0', 1: '1', 2: '2', 3: '3', 4: '4'}
print(2 in dictionary) # 檢查健2是否在dictionary中
True
dictionary[2] = 'xiugai' # 修改健2對應的值'2',變為'xiugai'
print(dictionary)
{0: '0', 1: '1', 2: 'xiugai', 3: '3', 4: '4'}
- fromkeys()的例子
print(dictionary)
{0: '0', 1: '1', 2: 'xiugai', 3: '3', 4: '4'}
x = dictionary.fromkeys([1,2,3],'xyz') # fromkeys() 在原有的字典中聲明一個新的字典
x
{1: 'xyz', 2: 'xyz', 3: 'xyz'}
y = {}.fromkeys(['a','b','c'], ['a','b','c'])
y
{'a': ['a', 'b', 'c'], 'b': ['a', 'b', 'c'], 'c': ['a', 'b', 'c']}
- 字典的迴圈迭代
x={'a': 1, 'b': 2, 'c': 3, 1: 'a'}
for i in x:
print(i)
a
b
c
1
for i in x:
print(i, x[i])
a 1
b 2
c 3
1 a
for i, j in x.items():
print(i, j)
a 1
b 2
c 3
1 a