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批量下载图片的三种方法
Apr 22 Python
python实现批量下载新浪博客的方法
Jun 15 Python
python实现Dijkstra算法的最短路径问题
Jun 21 Python
django框架model orM使用字典作为参数,保存数据的方法分析
Jun 24 Python
python3 selenium自动化测试 强大的CSS定位方法
Aug 23 Python
Pyqt5自适应布局实例
Dec 13 Python
Python greenlet和gevent使用代码示例解析
Apr 01 Python
Python 解决相对路径问题:&quot;No such file or directory&quot;
Jun 05 Python
Python通过format函数格式化显示值
Oct 17 Python
matplotlib 使用 plt.savefig() 输出图片去除旁边的空白区域
Jan 05 Python
Python爬虫自动化获取华图和粉笔网站的错题(推荐)
Jan 08 Python
Python趣味挑战之给幼儿园弟弟生成1000道算术题
May 28 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 date()日期时间函数详解
2010/05/16 PHP
php中将一个对象保存到Session中的方法
2015/03/13 PHP
JScript中使用ADODB.Stream判断文件编码的代码
2008/06/09 Javascript
javascript右下角弹层及自动隐藏(自己编写)
2013/11/20 Javascript
jQuery实现页面内锚点平滑跳转特效的方法总结
2015/05/11 Javascript
jQuery实现无限往下滚动效果代码
2016/04/16 Javascript
关于backbone url请求中参数带有中文存入数据库是乱码的快速解决办法
2016/06/13 Javascript
JavaScript实现刷新不重记的倒计时
2016/08/10 Javascript
最细致的vue.js基础语法 值得收藏!
2016/11/03 Javascript
浅谈Emergence.js 检测元素可见性的 js 插件
2017/11/18 Javascript
angularjs $http调用接口的方式详解
2018/08/13 Javascript
JS中数组与对象的遍历方法实例小结
2018/08/14 Javascript
详解express使用vue-router的history踩坑
2019/06/05 Javascript
layui复选框限制选择个数的方法
2019/09/18 Javascript
vue中实现点击按钮滚动到页面对应位置的方法(使用c3平滑属性实现)
2019/12/29 Javascript
JS实现图片懒加载(lazyload)过程详解
2020/04/02 Javascript
Vuex实现购物车小功能
2020/08/17 Javascript
python如何创建TCP服务端和客户端
2018/08/26 Python
Python集成开发工具Pycharm的安装和使用详解
2020/03/18 Python
torchxrayvision包安装过程(附pytorch1.6cpu版安装)
2020/08/26 Python
python简单实现9宫格图片实例
2020/09/03 Python
华纳兄弟工作室的官方授权商店:WB Shop
2018/11/30 全球购物
车间统计员岗位职责
2014/01/05 职场文书
关于人生的感言
2014/01/17 职场文书
社区母亲节活动记录
2014/03/06 职场文书
班级课外活动总结
2014/07/09 职场文书
搞笑的获奖感言
2014/08/16 职场文书
2014年督导工作总结
2014/11/19 职场文书
英文邀请函
2015/02/02 职场文书
工作迟到检讨书范文
2015/05/06 职场文书
2015年暑期社会实践报告
2015/07/13 职场文书
获奖感言怎么写
2015/07/31 职场文书
自动在Windows中运行Python脚本并定时触发功能实现
2021/09/04 Python
Window server中安装Redis的超详细教程
2021/11/17 Redis
js前端面试常见浏览器缓存强缓存及协商缓存实例
2022/06/21 Javascript
Nginx配置使用详解
2022/07/07 Servers