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通过socket查询whois的方法
Jul 18 Python
python学习之编写查询ip程序
Feb 27 Python
Python用UUID库生成唯一ID的方法示例
Dec 15 Python
详解 Python 读写XML文件的实例
Aug 02 Python
在dataframe两列日期相减并且得到具体的月数实例
Jul 03 Python
符合语言习惯的 Python 优雅编程技巧【推荐】
Sep 25 Python
python NumPy ndarray二维数组 按照行列求平均实例
Nov 26 Python
三个python爬虫项目实例代码
Dec 28 Python
tensorboard显示空白的解决
Feb 15 Python
Python使用random模块实现掷骰子游戏的示例代码
Apr 29 Python
python opencv旋转图片的使用方法
Jun 04 Python
Python访问Redis的详细操作
Jun 26 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中变量及部分适用方法
2008/03/27 PHP
基于Discuz security.inc.php代码的深入分析
2013/06/03 PHP
php的array数组和使用实例简明教程(容易理解)
2014/03/20 PHP
JAVASCRIPT keycode总结
2009/02/04 Javascript
JavaScript取得鼠标绝对位置程序代码介绍
2012/09/16 Javascript
jquery实现的图片点击滚动效果
2014/04/29 Javascript
在JS中操作时间之getUTCMilliseconds()方法的使用
2015/06/10 Javascript
jquery判断对象是否为空并遍历对象的简单实例
2016/07/26 Javascript
10分钟掌握XML、JSON及其解析
2020/12/06 Javascript
js实现一个可以兼容PC端和移动端的div拖动效果实例
2016/12/09 Javascript
关于react-router的几种配置方式详解
2017/07/24 Javascript
nodejs基于WS模块实现WebSocket聊天功能的方法
2018/01/12 NodeJs
vue添加class样式实例讲解
2019/02/12 Javascript
详解微信小程序实现跑马灯效果(附完整代码)
2019/04/29 Javascript
Vue页面手动刷新,实现导航栏激活项还原到初始状态
2020/08/06 Javascript
python网络编程学习笔记(七):HTML和XHTML解析(HTMLParser、BeautifulSoup)
2014/06/09 Python
Python的网络编程库Gevent的安装及使用技巧
2016/06/24 Python
python中如何使用朴素贝叶斯算法
2017/04/06 Python
基于Django用户认证系统详解
2018/02/21 Python
Python即时网络爬虫项目启动说明详解
2018/02/23 Python
python 实现创建文件夹和创建日志文件的方法
2019/07/07 Python
如何更改 pandas dataframe 中两列的位置
2019/12/27 Python
torchxrayvision包安装过程(附pytorch1.6cpu版安装)
2020/08/26 Python
Python logging日志库空间不足问题解决
2020/09/14 Python
使用Pytorch搭建模型的步骤
2020/11/16 Python
用Python 执行cmd命令
2020/12/18 Python
CSS3+js实现简单的时钟特效
2015/03/18 HTML / CSS
德国著名廉价网上药店:Shop-Apotheke
2017/07/23 全球购物
What's the difference between an interface and abstract class? (接口与抽象类有什么区别)
2012/10/29 面试题
大学毕业生通用自荐信范文
2013/10/31 职场文书
文科生自我鉴定
2014/02/15 职场文书
行政求职信
2014/07/04 职场文书
项目负责人岗位职责
2015/02/15 职场文书
三八节祝酒词
2015/08/11 职场文书
导游词之扬州大明寺
2019/10/09 职场文书
Java 超详细讲解十大排序算法面试无忧
2022/04/08 Java/Android