python--15 字典:当索引不好用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python--15 字典:当索引不好用相关的知识,希望对你有一定的参考价值。

字典是python唯一的影射类型 hash

  >>> brand = [‘李宁‘, ‘耐克‘, ‘阿迪达斯‘]
  >>> slogan = [‘一切皆有可能‘, ‘Just do it‘,‘Impossible is nothing‘]
  >>> print(‘李宁的口号是:‘,slogan[brand.index(‘李宁‘)])
  李宁的口号是: 一切皆有可能

 字典不是序列类型 ,是映射类型

 字符串 列表 元组是序列类型

创建和访问索引   标志性符号--花括号

  创建字典方式一:key value

  >>> dict1 = {‘李宁‘:‘一切皆有可能‘,‘耐克‘:‘Just do it‘, ‘阿迪达斯‘:‘Impossible is nothing‘}
  >>> dict1
  {‘李宁‘: ‘一切皆有可能‘, ‘耐克‘: ‘Just do it‘, ‘阿迪达斯‘: ‘Impossible is nothing‘}
  >>> print(‘耐克口号是:‘, dict1[‘耐克‘])
  耐克口号是: Just do it

  >>> dict2 = {1:‘one‘,2:‘two‘,3:‘three‘}
  >>> dict2[3]
  ‘three‘

  创建空字典
  >>> dict3 = {}
  >>> dict3
  {}
  >>> dict3 = dict()
  >>> dict3
  {}

  通过dict()创建

  >>> help(dict)

  class dict(object)
  | dict() -> new empty dictionary
  | dict(mapping) -> new dictionary initialized from a mapping object‘s
  | (key, value) pairs
  | dict(iterable) -> new dictionary initialized as if via:
  | d = {}
  | for k, v in iterable:
  | d[k] = v
  | dict(**kwargs) -> new dictionary initialized with the name=value pairs
  | in the keyword argument list. For example: dict(one=1, two=2)

   dict(mapping) -> new dictionary initialized from a mapping object‘s  
  >>> dict3 = dict((‘F‘,70),(‘i‘,105),(‘s‘,115),(‘h‘,104),(‘C‘,67))
  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  TypeError: dict expect ed at most 1 arguments, got 5
  >>> dict3 = dict(((‘F‘,70),(‘i‘,105),(‘s‘,115),(‘h‘,104),(‘C‘,67)))
  >>> dict3
  {‘F‘: 70, ‘i‘: 105, ‘s‘: 115, ‘h‘: 104, ‘C‘: 67}

  (key, value) pairs

  >>> dict4 = dict(code = ‘编程改变世界‘, draw = ‘每一笔都是一个世界‘ )
  >>> dict4
  {‘code‘: ‘编程改变世界‘, ‘draw‘: ‘每一笔都是一个世界‘}
  >>> dict4 = dict(‘code‘ = ‘编程改变世界‘, draw = ‘每一笔都是一个世界‘ )
  File "<stdin>", line 1
  SyntaxError: keyword can‘t be an expression

 

有则更改,没有则创建

  >>> dict4[‘code‘] = ‘学习编程就可以改变世界‘
  >>> dict4[‘student‘] = ‘天才第一步,却是....‘
  >>> dict4
  {‘code‘: ‘学习编程就可以改变世界‘, ‘draw‘: ‘每一笔都是一个世界‘, ‘student‘: ‘天才第一步,却是....‘}

dict() 工厂函数(类型)  str(), int(),list(),tuple() ...

fromkeys(...) 会重新创建新的字典

  dict.fromkeys(s[,v]) -> New dict with keys from S and values equal to v (v defaults to None)

  >>> dict1 = {}

  >>> dict1.fromkeys((1,2,3))
  {1: None, 2: None, 3: None}
  >>> dict1.fromkeys((1,2,3),‘Number‘)
  {1: ‘Number‘, 2: ‘Number‘, 3: ‘Number‘}


  >>> dict1.fromkeys((1,2,3),(‘one‘,‘two‘,‘three‘))
  {1: (‘one‘, ‘two‘, ‘three‘), 2: (‘one‘, ‘two‘, ‘three‘), 3: (‘one‘, ‘two‘, ‘three‘)}
  >>> dict1.fromkeys((1,3),‘数字‘)
  {1: ‘数字‘, 3: ‘数字‘}
  >>> dict1
  {}

keys(),values(),items()

  >>> dict1 = dict1.fromkeys(range(32),‘赞‘)
  >>> dict1
  {0: ‘赞‘, 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: ‘赞‘, 31: ‘赞‘}

  >>> for eachkey in dict1.keys():
  ... print(eachkey)
  ...

  >>> for eachvalue in dict1.values():
  ... print(eachvalue)
  ...

 返回的是元组

  >>> for eachitem in dict1.items():
  ... print(eachitem)
  ...

  

  >>> print(dict1[31])
  赞

 访问不存在的元素

  >>> print(dict1[32])
  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  KeyError: 32

  >>> dict1.get(32)
  >>> print(dict1.get(32))\
  ...
  None
  >>> print(dict1.get(32))
  None
  >>> print(dict1.get(32,‘没有‘))
  没有

 判断键在没在字典中 成员操作符 in  not in

  >>> 31 in dict1
  True
  >>> 33 in dict1
  False

 序列匹配的是值

 clear() 清空字典  存用户信息时会有风险

  >>> dict1.clear()
  >>> dict1
  {}
  >>> dict1 = {}
  >>> a = {‘姓名‘: ‘俊杰‘}
  >>> b = a
  >>> b
  {‘姓名‘: ‘俊杰‘}
  >>> a = {}
  >>> a
  {}
  >>> b
  {‘姓名‘: ‘俊杰‘}

  >>> a = b
  >>> a
  {‘姓名‘: ‘俊杰‘}
  >>> b
  {‘姓名‘: ‘俊杰‘}
  >>> a.clear()
  >>> a
  {}
  >>> b
  {}

 copy()  

 赋值只是贴了一个标签,复制创建新的复制域

  >>> a = {1:‘one‘, 2:‘two‘,3:‘three‘}
  >>> b = a.copy()
  >>> c = a
  >>> c
  {1: ‘one‘, 2: ‘two‘, 3: ‘three‘}
  >>> a
  {1: ‘one‘, 2: ‘two‘, 3: ‘three‘}
  >>> b
  {1: ‘one‘, 2: ‘two‘, 3: ‘three‘}
  >>> id(a)
  139890364904936
  >>> id(b)
  139890303134744
  >>> id(c)
  139890364904936

 pop() popitem() 随机弹

  >>> a.pop(2)
  ‘two‘
  >>> a.popitem()
  (3, ‘three‘)

 setdefalt(key[,value])

  >>> a
  {1: ‘one‘}
  >>> a.setdefault(‘小白‘)
  >>> a
  {1: ‘one‘, ‘小白‘: None}  
  >>> a.setdefault(5,‘five‘)
  ‘five‘
  >>> a
  {1: ‘one‘, ‘小白‘: None, 5: ‘five‘}

 a.update(b)  通过b的key对应的属性去修改a,有则修改 没有则添加

  >>> b = {‘小白‘: ‘狗‘}
  >>> a.update(b)
  >>> a
  {1: ‘one‘, ‘小白‘: ‘狗‘, 5: ‘five‘}

 

  >>> b = {‘小黄‘ : ‘da狗‘}
  >>> a.update(b)
  >>> a
  {1: ‘one‘, ‘小白‘: ‘狗‘, 5: ‘five‘, ‘小黄‘: ‘da狗‘}

 



























































































































以上是关于python--15 字典:当索引不好用的主要内容,如果未能解决你的问题,请参考以下文章

Python基础--字典:当索引不好用时

《python基础教程》第4章字典:当索引不好用时 读书笔记

字典(当索引不好用时)

字典:当索引不好用时

第4章 字典:当索引不好用时

读书笔记--《Python基础教程第二版》-- 第四章 字典:当索引不好用时