python:dictionary

概述

常用操作:

  • 样式:grades = {'Ana':'B', 'John':'A+', 'Denise':'A', 'Katy':'A'}
  • 赋值: grades['John'] = 'A'
  • 判断 某元素是否在dict里:
in: 'John' in grades
out: True 
  • 删除 del(grades['Ana']
  • 查找key: grades.keys() returns ['Denise','Katy','John','Ana']
  • 查找value: grades.values() returns ['A', 'A', 'A+', 'B']

values

  • any type (immutable and mutable)
  • can be duplicates 可以重复
  • dictionary values can be lists, even other dictionaries!

keys

  • must be unique
  • immutable type (int, float, string, tuple,bool)
  • actually need an object that is hashable, but think of as immutable as all immutable types are hashable
  • careful with float type as a key

else:

  • dict无顺序
    d = {4:{1:0}, (1,3):"twelve", 'const':[3.14,2.7,8.44]}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容