python数据结构之字典方法

字典是一种通过名字或者关键字引用的得数据结构,其键可以是数字、字符串、元组,这种结构类型也称之为映射。字典类型是Python中唯一內建的映射类型,基本的操作包括如下: 
(1)len():返回字典中键—值对的数量; 
(2)d[k]:返回关键字对于的值; 
(3)d[k]=v:将值关联到键值k上; 
(4)del d[k]:删除键值为k的项; 
(5)key in d:键值key是否在d中,是返回True,否则返回False。

一、字典的创建 
1.1 直接创建字典

 
  1. d={'one':1,'two':2,'three':3}

  2. print d

  3. print d['two']

  4. print d['three']

  5. 运算结果:

  6. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  7. {'three': 3, 'two': 2, 'one': 1}

  8. 2

  9. 3

  10. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

1.2 通过dict创建字典

 
  1. # _*_ coding:utf-8 _*_

  2. items=[('one',1),('two',2),('three',3),('four',4)]

  3. print u'items中的内容:'

  4. print items

  5. print u'利用dict创建字典,输出字典内容:'

  6. d=dict(items)

  7. print d

  8. print u'查询字典中的内容:'

  9. print d['one']

  10. print d['three']

  11. 运算结果:

  12. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  13. items中的内容:

  14. [('one', 1), ('two', 2), ('three', 3), ('four', 4)]

  15. 利用dict创建字典,输出字典内容:

  16. {'four': 4, 'three': 3, 'two': 2, 'one': 1}

  17. 查询字典中的内容:

  18. 1

  19. 3

  20. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

或者通过关键字创建字典

 
  1. # _*_ coding:utf-8 _*_

  2. d=dict(one=1,two=2,three=3)

  3. print u'输出字典内容:'

  4. print d

  5. print u'查询字典中的内容:'

  6. print d['one']

  7. print d['three']

  8. 运算结果:

  9. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  10. 输出字典内容:

  11. {'three': 3, 'two': 2, 'one': 1}

  12. 查询字典中的内容:

  13. 1

  14. 3

  15. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

二、字典的格式化字符串

 
  1. # _*_ coding:utf-8 _*_

  2. d={'one':1,'two':2,'three':3,'four':4}

  3. print d

  4. print "three is %(three)s." %d

  5. 运算结果:

  6. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  7. {'four': 4, 'three': 3, 'two': 2, 'one': 1}

  8. three is 3.

  9. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

三、字典方法 
3.1 clear函数:清除字典中的所有项

 
  1. # _*_ coding:utf-8 _*_

  2. d={'one':1,'two':2,'three':3,'four':4}

  3. print d

  4. d.clear()

  5. print d

  6. 运算结果:

  7. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  8. {'four': 4, 'three': 3, 'two': 2, 'one': 1}

  9. {}

  10. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

请看下面两个例子 
3.1.1

 
  1. # _*_ coding:utf-8 _*_

  2. d={}

  3. dd=d

  4. d['one']=1

  5. d['two']=2

  6. print dd

  7. d={}

  8. print d

  9. print dd

  10. 运算结果:

  11. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  12. {'two': 2, 'one': 1}

  13. {}

  14. {'two': 2, 'one': 1}

  15. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

3.1.2

 
  1. # _*_ coding:utf-8 _*_

  2. d={}

  3. dd=d

  4. d['one']=1

  5. d['two']=2

  6. print dd

  7. d.clear()

  8. print d

  9. print dd

  10. 运算结果:

  11. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  12. {'two': 2, 'one': 1}

  13. {}

  14. {}

  15. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

3.1.2与3.1.1唯一不同的是在对字典d的清空处理上,3.1.1将d关联到一个新的空字典上,这种方式对字典dd是没有影响的,所以在字典d被置空后,字典dd里面的值仍旧没有变化。但是在3.1.2中clear方法清空字典d中的内容,clear是一个原地操作的方法,使得d中的内容全部被置空,这样dd所指向的空间也被置空。 
3.2 copy函数:返回一个具有相同键值的新字典

 
  1. # _*_ coding:utf-8 _*_

  2. x={'one':1,'two':2,'three':3,'test':['a','b','c']}

  3. print u'初始X字典:'

  4. print x

  5. print u'X复制到Y:'

  6. y=x.copy()

  7. print u'Y字典:'

  8. print y

  9. y['three']=33

  10. print u'修改Y中的值,观察输出:'

  11. print y

  12. print x

  13. print u'删除Y中的值,观察输出'

  14. y['test'].remove('c')

  15. print y

  16. print x

  17. 运算结果:

  18. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  19. 初始X字典:

  20. {'test': ['a', 'b', 'c'], 'three': 3, 'two': 2, 'one': 1}

  21. X复制到Y:

  22. Y字典:

  23. {'test': ['a', 'b', 'c'], 'one': 1, 'three': 3, 'two': 2}

  24. 修改Y中的值,观察输出:

  25. {'test': ['a', 'b', 'c'], 'one': 1, 'three': 33, 'two': 2}

  26. {'test': ['a', 'b', 'c'], 'three': 3, 'two': 2, 'one': 1}

  27. 删除Y中的值,观察输出

  28. {'test': ['a', 'b'], 'one': 1, 'three': 33, 'two': 2}

  29. {'test': ['a', 'b'], 'three': 3, 'two': 2, 'one': 1}

  30. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

注:在复制的副本中对值进行替换后,对原来的字典不产生影响,但是如果修改了副本,原始的字典也会被修改。deepcopy函数使用深复制,复制其包含所有的值,这个方法可以解决由于副本修改而使原始字典也变化的问题。

 
  1. # _*_ coding:utf-8 _*_

  2. from copy import deepcopy

  3. x={}

  4. x['test']=['a','b','c','d']

  5. y=x.copy()

  6. z=deepcopy(x)

  7. print u'输出:'

  8. print y

  9. print z

  10. print u'修改后输出:'

  11. x['test'].append('e')

  12. print y

  13. print z

  14. 运算输出:

  15. 输出:

  16. {'test': ['a', 'b', 'c', 'd']}

  17. {'test': ['a', 'b', 'c', 'd']}

  18. 修改后输出:

  19. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  20. {'test': ['a', 'b', 'c', 'd', 'e']}

  21. {'test': ['a', 'b', 'c', 'd']}

  22. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

3.3 fromkeys函数:使用给定的键建立新的字典,键默认对应的值为None

 
  1. # _*_ coding:utf-8 _*_

  2. d=dict.fromkeys(['one','two','three'])

  3. print d

  4. 运算输出:

  5. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  6. {'three': None, 'two': None, 'one': None}

  7. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

或者指定默认的对应值

 
  1. # _*_ coding:utf-8 _*_

  2. d=dict.fromkeys(['one','two','three'],'unknow')

  3. print d

  4. 运算结果:

  5. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  6. {'three': 'unknow', 'two': 'unknow', 'one': 'unknow'}

  7. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.4 get函数:访问字典成员

 
  1. # _*_ coding:utf-8 _*_

  2. d={'one':1,'two':2,'three':3}

  3. print d

  4. print d.get('one')

  5. print d.get('four')

  6. 运算结果:

  7. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  8. {'three': 3, 'two': 2, 'one': 1}

  9. 1

  10. None

  11. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

注:get函数可以访问字典中不存在的键,当该键不存在是返回None 
3.5 has_key函数:检查字典中是否含有给出的键

 
  1. # _*_ coding:utf-8 _*_

  2. d={'one':1,'two':2,'three':3}

  3. print d

  4. print d.has_key('one')

  5. print d.has_key('four')

  6. 运算结果:

  7. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  8. {'three': 3, 'two': 2, 'one': 1}

  9. True

  10. False

  11. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

3.6 items和iteritems函数:items将所有的字典项以列表方式返回,列表中项来自(键,值),iteritems与items作用相似,但是返回的是一个迭代器对象而不是列表

 
  1. # _*_ coding:utf-8 _*_

  2. d={'one':1,'two':2,'three':3}

  3. print d

  4. list=d.items()

  5. for key,value in list:

  6. print key,':',value

  7. 运算结果:

  8. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  9. {'three': 3, 'two': 2, 'one': 1}

  10. three : 3

  11. two : 2

  12. one : 1

  13. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
 
  1. # _*_ coding:utf-8 _*_

  2. d={'one':1,'two':2,'three':3}

  3. print d

  4. it=d.iteritems()

  5. for k,v in it:

  6. print "d[%s]="%k,v

  7. 运算结果:

  8. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  9. {'three': 3, 'two': 2, 'one': 1}

  10. d[three]= 3

  11. d[two]= 2

  12. d[one]= 1

  13. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3.7 keys和iterkeys:keys将字典中的键以列表形式返回,iterkeys返回键的迭代器

 
  1. # _*_ coding:utf-8 _*_

  2. d={'one':1,'two':2,'three':3}

  3. print d

  4. print u'keys方法:'

  5. list=d.keys()

  6. print list

  7. print u'\niterkeys方法:'

  8. it=d.iterkeys()

  9. for x in it:

  10. print x

  11. 运算结果:

  12. {'three': 3, 'two': 2, 'one': 1}

  13. keys方法:

  14. ['three', 'two', 'one']

  15.  
  16. iterkeys方法:

  17. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  18. three

  19. two

  20. one

  21. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

3.8 pop函数:删除字典中对应的键

 
  1. # _*_ coding:utf-8 _*_

  2. d={'one':1,'two':2,'three':3}

  3. print d

  4. d.pop('one')

  5. print d

  6. 运算结果:

  7. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  8. {'three': 3, 'two': 2, 'one': 1}

  9. {'three': 3, 'two': 2}

  10. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.9 popitem函数:移出字典中的项

 
  1. # _*_ coding:utf-8 _*_

  2. d={'one':1,'two':2,'three':3}

  3. print d

  4. d.popitem()

  5. print d

  6. 运算结果:

  7. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  8. {'three': 3, 'two': 2, 'one': 1}

  9. {'two': 2, 'one': 1}

  10. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.10 setdefault函数:类似于get方法,获取与给定键相关联的值,也可以在字典中不包含给定键的情况下设定相应的键值

 
  1. # _*_ coding:utf-8 _*_

  2. d={'one':1,'two':2,'three':3}

  3. print d

  4. print d.setdefault('one',1)

  5. print d.setdefault('four',4)

  6. print d

  7. 运算结果:

  8. {'three': 3, 'two': 2, 'one': 1}

  9. 1

  10. 4

  11. {'four': 4, 'three': 3, 'two': 2, 'one': 1}

  12. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3.11 update函数:用一个字典更新另外一个字典

 
  1. # _*_ coding:utf-8 _*_

  2. d={

  3. 'one':123,

  4. 'two':2,

  5. 'three':3

  6. }

  7. print d

  8. x={'one':1}

  9. d.update(x)

  10. print d

  11. 运算结果:

  12. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  13. {'three': 3, 'two': 2, 'one': 123}

  14. {'three': 3, 'two': 2, 'one': 1}

  15. >>>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

3.12 values和itervalues函数:values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重复的元素

 
  1. # _*_ coding:utf-8 _*_

  2. d={

  3. 'one':123,

  4. 'two':2,

  5. 'three':3,

  6. 'test':2

  7. }

  8. print d.values()

  9. 运算结果:

  10. =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

  11. [2, 3, 2, 123]

  12. >>>

猜你喜欢

转载自blog.csdn.net/qq_38360675/article/details/84968814