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 相关文章推荐
树莓派中python获取GY-85九轴模块信息示例
Dec 05 Python
netbeans7安装python插件的方法图解
Dec 24 Python
Python深入学习之特殊方法与多范式
Aug 31 Python
Python的pycurl包用法简介
Nov 13 Python
Python将多个excel文件合并为一个文件
Jan 03 Python
python简单实现操作Mysql数据库
Jan 29 Python
详解Django中间件执行顺序
Jul 16 Python
Python3随机漫步生成数据并绘制
Aug 27 Python
python实现梯度下降算法
Mar 24 Python
python 找出list中最大或者最小几个数的索引方法
Oct 30 Python
Python smtp邮件发送模块用法教程
Jun 15 Python
10行Python代码实现Web自动化管控的示例代码
Aug 14 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
php上传功能集后缀名判断和随机命名(强力推荐)
2015/09/10 PHP
详谈PHP中public,private,protected,abstract等关键字的用法
2017/12/31 PHP
php插入mysql数据返回id的方法
2018/05/31 PHP
PHP利用缓存处理用户注册时的邮箱验证,成功后用户数据存入数据库操作示例
2019/12/31 PHP
event.srcElement 用法笔记e.target
2009/12/18 Javascript
JavaScript中链式调用之研习
2011/04/07 Javascript
利用JS自动打开页面上链接的实现代码
2011/09/25 Javascript
你的 mixin 真的兼容 ECMAScript 5 吗?
2013/04/11 Javascript
php+js实现倒计时功能
2014/06/02 Javascript
Javascript让DEDECMS告别手写Tag
2014/09/01 Javascript
jQuery删除一个元素后淡出效果展示删除过程的方法
2015/03/18 Javascript
javascript实现拖放效果
2015/12/16 Javascript
微信小程序  简单实例(阅读器)的实例开发
2016/09/29 Javascript
关于bootstrap日期转化,bootstrap-editable的简单使用,bootstrap-fileinput的使用详解
2017/05/12 Javascript
JavaScript冒泡算法原理与实现方法深入理解
2020/06/04 Javascript
基于Echarts图表在div动态切换时不显示的解决方式
2020/07/20 Javascript
python实现代理服务功能实例
2013/11/15 Python
Python编写电话薄实现增删改查功能
2016/05/07 Python
python实现Zabbix-API监控
2018/09/17 Python
python GUI库图形界面开发之PyQt5图片显示控件QPixmap详细使用方法与实例
2020/02/27 Python
python 判断txt每行内容中是否包含子串并重新写入保存的实例
2020/03/12 Python
python pandas dataframe 去重函数的具体使用
2020/07/20 Python
python开根号实例讲解
2020/08/30 Python
python爬虫破解字体加密案例详解
2021/03/02 Python
web字体加载方案优化小结
2019/11/29 HTML / CSS
新加坡网上化妆品店:Best Buy World
2018/05/18 全球购物
80年代复古T恤:TruffleShuffle
2018/07/02 全球购物
史上最全面的Java面试题汇总!
2015/02/03 面试题
英语专业应届生求职信范文
2013/11/15 职场文书
教师个人读书活动总结
2014/07/08 职场文书
报到证办理个人委托书
2014/10/06 职场文书
2014年公务员转正工作总结
2014/11/07 职场文书
铁人纪念馆观后感
2015/06/16 职场文书
追悼会答谢词范文
2015/09/29 职场文书
初中班主任心得体会
2016/01/07 职场文书
Python实现自动玩连连看的脚本分享
2022/04/04 Python