崬城衞 发表于 2018-8-12 11:42:46

python--字典类型

  字典的删除
  dic.pop(key) 根据key值删除字典的元素;
  >>> dic
  {'gender': 'male', 'age': 8, 'name': 'fentiao', 'kind': 'cat'}
  >>> dic.pop("kind")   //弹出字典中key值为"kind"的元素并返回该key的元素
  'cat'
  >> dic
  {'gender': 'male', 'age': 8, 'name': 'fentiao'}

  dic.popitem() 随机删除字典元素,返回(key,value)
  >>>dic.popitem()
  ('gender', 'male')
  >>> dic
  {'age': 5, 'name': 'fentiao'}
  dic.clear() 删除字典中的所有元素
  >>> dic.clear()   //删除字典的所有元素Out:
  >>> dic
  {}
  del dic 删除字典本身
  >>> del dic                  //删除整个字典
  >>> dic
  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  NameError: name 'dic' is not defined

页: [1]
查看完整版本: python--字典类型