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]}

你可能感兴趣的:(python:dictionary)