python 字典
字典是另一种可变容器模型,且可存储任意类型对象。
字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中。
dict = {'Name': 'soyoungboy', 'Age': 27, 'School': 'Xian University of Finance and Economics'}; print "dict['Name']: ", dict['Name']; print "dict['Age']: ", dict['Age']; print "dict['School']", dict['School']
结果:
dict['Name']: soyoungboy dict['Age']: 27 dict['School'] Xian University of Finance and Economics
修改字典
#!/usr/bin/python dict = {'Name': 'soyoungboy', 'Age': 27, 'School': 'Xian University of Finance and Economics'}; dict['Age'] = 8; # 修改字段 dict['Class'] = "First"; # 新增字段 print "dict['Age']: ", dict['Age']; print "dict['Class']: ", dict['Class']; print dict
结果:
dict['Age']: 8 dict['Class']: First {'School': 'Xian University of Finance and Economics', 'Age': 8, 'Name': 'soyoungboy', 'Class': 'First'}
删除字典元素
#!/usr/bin/python # coding=utf-8 dict = {'Name': 'soyoungboy', 'Age': 27, 'School': 'Xian University of Finance and Economics'}; dict['Age'] = 8; # 修改字段 dict['Class'] = "First"; # 新增字段 print dict del dict['Age'] # 删除age字段 print "删除Age后的dict = ", dict dict.clear(); # 清空dict print dict;
结果:
{'School': 'Xian University of Finance and Economics', 'Age': 8, 'Name': 'soyoungboy', 'Class': 'First'} 删除Age后的dict = {'School': 'Xian University of Finance and Economics', 'Name': 'soyoungboy', 'Class': 'First'} {}
字典健特性:
- 不允许同一个键出现两次。创建时如果同一个键被赋值两次,后一个值会被记住;
- 键必须不可变,所以可以用数字,字符串或元组充当,所以用列表就不行;
字典内置函数&方法
内置函数demo:
#!/usr/bin/python # coding=utf-8 dict = {'Name': 'soyoungboy', 'Age': 27, 'School': 'Xian University of Finance and Economics'}; dict1 = {'Name': 'soyoungboy', 'Age': 27, 'School': 'Xian University of Finance and Economics'}; dict2 = {'Name': 'boy', 'Age': 25, 'School': 'Xian University of Finance and Economics'}; # 如果两个字典的元素相同返回0,如果字典dict1大于字典dict2返回1,如果字典dict1小于字典dict2返回-1 print cmp(dict, dict1); print cmp(dict1, dict2); print cmp(dict2, dict1); # 计算字典元素个数,即键的总数。 print len(dict); print str(dict); # 返回输入的变量类型,如果变量是字典就返回字典类型。 print type(dict['Name']) print type(dict['Age'])
结果:
0 1 -1 3 {'School': 'Xian University of Finance and Economics', 'Age': 27, 'Name': 'soyoungboy'} <type 'str'> <type 'int'>
内置方法demo:
#!/usr/bin/python # coding=utf-8 dict = {'Name': 'soyoungboy', 'Age': 27, 'School': 'Xian University of Finance and Economics'}; print dict.copy(); # 返回一个字典的浅复制 fromkeys = dict.fromkeys(dict) # 创建一个新字典,以序列 seq 中元素做字典的键,val 为字典所有键对应的初始值 print fromkeys; dict_fromkeys = dict.fromkeys(dict, 'I Love John') # 创建一个新字典,以序列 seq 中元素做字典的键,val 为字典所有键对应的初始值 print dict_fromkeys; dict = {'Name': 'soyoungboy', 'Age': 27, 'School': 'Xian University of Finance and Economics'}; print dict.get('Name', 'defaultValue'); # 返回指定键的值,如果值不在字典中返回default值,其实就是根据键获取值 print dict.get('name', 'defaultValue'); print dict.items(); # 以列表返回可遍历的(键, 值) 元组数组 print dict.keys(); # 以列表返回一个字典所有的键 dict.setdefault('Height', 'defaultValue'); # 和get()类似, 但如果键不存在于字典中,将会添加键并将值设为default print dict; dict = {'Name': 'soyoungboy', 'Age': 27, 'School': 'Xian University of Finance and Economics'}; dict2 = {'Name': 'john', 'Age': 27, 'School': 'South China Agricultural University'}; dict.update(dict2); # 把字典dict2的键/值对更新到dict里 print dict; print dict.values(); # 以列表返回字典中的所有值
结果:
{'School': 'Xian University of Finance and Economics', 'Age': 27, 'Name': 'soyoungboy'} {'School': None, 'Age': None, 'Name': None} {'School': 'I Love John', 'Age': 'I Love John', 'Name': 'I Love John'} soyoungboy defaultValue [('School', 'Xian University of Finance and Economics'), ('Age', 27), ('Name', 'soyoungboy')] ['School', 'Age', 'Name'] {'School': 'Xian University of Finance and Economics', 'Age': 27, 'Name': 'soyoungboy', 'Height': 'defaultValue'} {'School': 'South China Agricultural University', 'Name': 'john', 'Age': 27} ['South China Agricultural University', 'john', 27]