Python数据类型详解(四)字典:dict


Posted in Python onMay 12, 2016

一.基本数据类型

整数:int

字符串:str(注:\t等于一个tab键)

布尔值: bool

列表:list

列表用[]

元祖:tuple

元祖用()

字典: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)
  """
  def clear(self): # real signature unknown; restored from __doc__
    """ D.clear() -> None. Remove all items from D. """
    pass

  def copy(self): # real signature unknown; restored from __doc__
    """ D.copy() -> a shallow copy of D """
    pass

  @staticmethod # known case
  def fromkeys(*args, **kwargs): # real signature unknown
    """ Returns a new dict with keys from iterable and values equal to value. """
    pass

  def get(self, k, d=None): # real signature unknown; restored from __doc__
    """ D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
    pass

  def items(self): # real signature unknown; restored from __doc__
    """ D.items() -> a set-like object providing a view on D's items """
    pass

  def keys(self): # real signature unknown; restored from __doc__
    """ D.keys() -> a set-like object providing a view on D's keys """
    pass

  def pop(self, k, d=None): # real signature unknown; restored from __doc__
    """
    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
    If key is not found, d is returned if given, otherwise KeyError is raised
    """
    pass

  def popitem(self): # real signature unknown; restored from __doc__
    """
    D.popitem() -> (k, v), remove and return some (key, value) pair as a
    2-tuple; but raise KeyError if D is empty.
    """
    pass

  def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
    """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
    pass

  def update(self, E=None, **F): # known special case of dict.update
    """
    D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
    If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
    If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
    In either case, this is followed by: for k in F: D[k] = F[k]
    """
    pass

  def values(self): # real signature unknown; restored from __doc__
    """ D.values() -> an object providing a view on D's values """
    pass

  def __contains__(self, *args, **kwargs): # real signature unknown
    """ True if D has a key k, else False. """
    pass

  def __delitem__(self, *args, **kwargs): # real signature unknown
    """ Delete self[key]. """
    pass

  def __eq__(self, *args, **kwargs): # real signature unknown
    """ Return self==value. """
    pass

  def __getattribute__(self, *args, **kwargs): # real signature unknown
    """ Return getattr(self, name). """
    pass

  def __getitem__(self, y): # real signature unknown; restored from __doc__
    """ x.__getitem__(y) <==> x[y] """
    pass

  def __ge__(self, *args, **kwargs): # real signature unknown
    """ Return self>=value. """
    pass

  def __gt__(self, *args, **kwargs): # real signature unknown
    """ Return self>value. """
    pass

  def __init__(self, seq=None, **kwargs): # known special case of dict.__init__
    """
    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)
    # (copied from class doc)
    """
    pass

  def __iter__(self, *args, **kwargs): # real signature unknown
    """ Implement iter(self). """
    pass

  def __len__(self, *args, **kwargs): # real signature unknown
    """ Return len(self). """
    pass

  def __le__(self, *args, **kwargs): # real signature unknown
    """ Return self<=value. """
    pass

  def __lt__(self, *args, **kwargs): # real signature unknown
    """ Return self<value. """
    pass

  @staticmethod # known case of __new__
  def __new__(*args, **kwargs): # real signature unknown
    """ Create and return a new object. See help(type) for accurate signature. """
    pass

  def __ne__(self, *args, **kwargs): # real signature unknown
    """ Return self!=value. """
    pass

  def __repr__(self, *args, **kwargs): # real signature unknown
    """ Return repr(self). """
    pass

  def __setitem__(self, *args, **kwargs): # real signature unknown
    """ Set self[key] to value. """
    pass

  def __sizeof__(self): # real signature unknown; restored from __doc__
    """ D.__sizeof__() -> size of D in memory, in bytes """
    pass

  __hash__ = None

三.所有字典数据类型举例

user_info = {
  0 :"zhangyanlin",
  "age" :"18",
  2 :"pythoner"
}
#获取所有的key
print(user_info.keys())
 
#获取所有的values
print(user_info.values())
 
#获取所有的key和values
print(user_info.items())
 
clear清除所有的内容
user_info.clear()
print(user_info)
 
#get 根据key获取值,如果key不存在,可以指定一个默认值
val = user_info.get('age')
print(val)

#update批量更新
test = {
  'a':111,
  'b':222
}
user_info.update(test)
print(user_info)

四.索引

#如果没有key,会报错
user_info = {
  "name" :'zhangyanlin',
  "age" :18,
  "job" :'pythoner'
}
print(user_info['name'])

五.for循环

#循环
user_info = {
  0 :"zhangyanlin",
  "age" :"18",
  2 :"pythoner"
}
for i in user_info:
  print(i)
 
#循环输出所有的键入值
for k,v in user_info.items():
  print(k)
  print(v)

以上就是本文的全部内容了,希望对大家熟练掌握Python数据结构能够有所帮助。

Python 相关文章推荐
bpython 功能强大的Python shell
Feb 16 Python
Python实现自定义顺序、排列写入数据到Excel的方法
Apr 23 Python
python利用requests库模拟post请求时json的使用教程
Dec 07 Python
python将list转为matrix的方法
Dec 12 Python
Python利用scapy实现ARP欺骗的方法
Jul 23 Python
解析PyCharm Python运行权限问题
Jan 08 Python
python GUI库图形界面开发之PyQt5输入对话框QInputDialog详细使用方法与实例
Feb 27 Python
浅谈JupyterNotebook导出pdf解决中文的问题
Apr 22 Python
对Matlab中共轭、转置和共轭装置的区别说明
May 11 Python
python利用 keyboard 库记录键盘事件
Oct 16 Python
Python实现Word文档转换Markdown的示例
Dec 22 Python
python+opencv实现目标跟踪过程
Jun 21 Python
Python匹配中文的正则表达式
May 11 #Python
Python3使用requests发闪存的方法
May 11 #Python
Python3控制路由器——使用requests重启极路由.py
May 11 #Python
Python3使用requests登录人人影视网站的方法
May 11 #Python
在Django中进行用户注册和邮箱验证的方法
May 09 #Python
Python数据类型详解(三)元祖:tuple
May 08 #Python
Python数据类型详解(二)列表
May 08 #Python
You might like
下载文件的点击数回填
2006/10/09 PHP
用PHPdig打造属于你自己的Google[图文教程]
2007/02/14 PHP
php为什么选mysql作为数据库? Mysql 创建用户方法
2007/07/02 PHP
MySQL 日期时间函数常用总结
2012/06/12 PHP
PHPMailer使用教程(PHPMailer发送邮件实例分析)
2012/12/06 PHP
php中并发读写文件冲突的解决方案
2013/10/25 PHP
PHP常量DIRECTORY_SEPARATOR原理及用法解析
2020/11/10 PHP
js中复制行和删除行的操作实例
2013/06/25 Javascript
教你用AngularJS框架一行JS代码实现控件验证效果
2014/06/23 Javascript
jQuery自动添加表单项的方法
2015/07/13 Javascript
JavaScript中使用sencha gridpanel 编辑单元格、改变单元格颜色
2015/11/26 Javascript
Javascript之面向对象--接口
2016/12/02 Javascript
老生常谈的跨域处理
2017/01/11 Javascript
jQuery插件zTree实现删除树节点的方法示例
2017/03/08 Javascript
nodejs接入阿里大鱼短信验证码的方法
2017/07/10 NodeJs
vue 运用mock数据的示例代码
2017/11/07 Javascript
微信小程序实现留言板(Storage)
2018/11/02 Javascript
Vue数据双向绑定的深入探究
2018/11/27 Javascript
JavaScript设计模式---单例模式详解【四种基本形式】
2020/05/16 Javascript
vue 手机物理监听键+退出提示代码
2020/09/09 Javascript
详解JavaScript之Array.reduce源码解读
2020/11/01 Javascript
python中logging库的使用总结
2017/10/18 Python
Python实现可设置持续运行时间、线程数及时间间隔的多线程异步post请求功能
2018/01/11 Python
python 简单照相机调用系统摄像头实现方法 pygame
2018/08/03 Python
python实现批量处理将图片粘贴到另一张图片上并保存
2019/12/12 Python
python Manager 之dict KeyError问题的解决
2019/12/21 Python
浅谈keras中的目标函数和优化函数MSE用法
2020/06/10 Python
Python实现自动装机功能案例分析
2020/10/22 Python
Debenhams百货英国官方网站:Debenhams UK
2016/07/12 全球购物
关于母亲节的感言
2014/02/04 职场文书
社区服务标语
2014/07/01 职场文书
工程造价专业求职信
2014/07/17 职场文书
2014年单位法制宣传日活动总结
2014/11/01 职场文书
小学生思想品德评语
2014/12/31 职场文书
《初涉尘世》读后感3篇
2020/01/10 职场文书
Win11如何默认打开软件界面最大化?Win11默认打开软件界面最大化的方法
2022/07/15 数码科技