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中使用Queue和Condition进行线程同步的方法
Jan 19 Python
Python切片知识解析
Mar 06 Python
详解Python中的__getitem__方法与slice对象的切片操作
Jun 27 Python
python读写json文件的简单实现
Apr 11 Python
Python数据预处理之数据规范化(归一化)示例
Jan 08 Python
Pandas之groupby( )用法笔记小结
Jul 23 Python
详解Python中正则匹配TAB及空格的小技巧
Jul 26 Python
Pycharm+Python+PyQt5使用详解
Sep 25 Python
PyQt5多线程防卡死和多窗口用法的实现
Sep 15 Python
浅谈matplotlib默认字体设置探索
Feb 03 Python
Python使用socket去实现TCP客户端和TCP服务端
Apr 12 Python
Python用any()函数检查字符串中的字母以及如何使用all()函数
Apr 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/11/27 PHP
CI框架常用方法小结
2016/05/17 PHP
thinkPHP js文件中U方法不被解析问题的解决方法
2016/12/05 PHP
PHP5.5安装PHPRedis扩展及连接测试方法
2017/01/22 PHP
js 键盘记录实现(兼容FireFox和IE)
2010/02/07 Javascript
button没写type=button会导致点击时提交
2014/03/06 Javascript
javascript window.open打开新窗口后无法再次打开该窗口问题的解决方法
2014/04/12 Javascript
jquery操作对象数组元素方法详解
2014/11/26 Javascript
使用jquery动态加载js文件的方法
2014/12/24 Javascript
分享javascript、jquery实用代码段
2016/10/20 Javascript
easyui-combobox 实现简单的自动补全功能示例
2016/11/08 Javascript
js生成随机数方法和实例
2017/01/17 Javascript
完美解决spring websocket自动断开连接再创建引发的问题
2017/03/02 Javascript
微信小程序商城项目之侧栏分类效果(1)
2017/04/17 Javascript
vue实现页面加载动画效果
2017/09/19 Javascript
详解Koa中更方便简单发送响应的方式
2018/07/20 Javascript
VUE预渲染及遇到的坑
2018/09/03 Javascript
jQuery实现数字自动增加或者减少的动画效果示例
2018/12/11 jQuery
原生js实现碰撞检测
2020/03/12 Javascript
python实现简单神经网络算法
2018/03/10 Python
python的常用模块之collections模块详解
2018/12/06 Python
Python命名空间的本质和加载顺序
2018/12/17 Python
Python可迭代对象操作示例
2019/05/07 Python
Pytorch之finetune使用详解
2020/01/18 Python
python+appium+yaml移动端自动化测试框架实现详解
2020/11/24 Python
html5 初试 indexedDB(推荐)
2016/07/21 HTML / CSS
一些网络技术方面的面试题
2014/05/01 面试题
总经理职责
2013/12/22 职场文书
《小壁虎借尾巴》教学反思
2014/02/16 职场文书
总经理助理的职责
2014/03/14 职场文书
论文评语大全
2014/04/29 职场文书
会议接待欢迎标语
2014/10/08 职场文书
奠基仪式致辞
2015/07/30 职场文书
数据库之SQL技巧整理案例
2021/07/07 SQL Server
Python jiaba库的使用详解
2021/11/23 Python
剑指Offer之Java算法习题精讲二叉树的构造和遍历
2022/03/21 Java/Android