Python3基础之字典
程序员文章站
2022-04-04 16:29:27
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
上一篇: python 基础篇03
下一篇: python爬虫入门urllib库的使用