一、创建字典:

  1. d = {
  2. "name": "morra", #字典是无序的
  3. "age": 99,
  4. "gender": 'm'
  5. }
  6.  
  7. a = dict()
  8. b = dict(k1=123,k2="morra")

二、字典常用操作:

修改或增加字典:

  1. dict = {'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'}
  2. dict['ob1']='book'
  3. dict['ob4']='cow'
  4. print(dict)
  5. {'ob3': 'printer', 'ob2': 'mouse', 'ob1': 'book','ob4':'cow}

其他常用方法:

len(a)

得到字典a中元素的个数

a[k]

取得字典a中键K所对应的值

a[k] = v

设定字典a中键k所对应的值成为v

del a[k]

使用 key从一个 dictionary中删除独立的元素。如,删除Dictionary dic中的user=’root’:del dic[“user”]

a.clear()

从一个 dictionary中清除所有元素。如,删除Dictionary dic中的所有元素:dic.clear()

a.copy()

得到字典副本

k in a

字典中存在键k则为返回True,没有则返回False

k not in a

字典中不存在键k则为返回true,反之返回False

a.has_key(k)

判断字典a中是否含有键k

a.items()

得到字典a中的键—值对list

a.keys()

得到字典a中键的list

a.update([b])

从b字典中更新a字典,如果键相同则更新,a中不存在则追加.

a.fromkeys(seq[, value])

创建一个新的字典,其中的键来自sql,值来自value

a.values()

得到字典a中值的list

a.get(k[, x])

从字典a中取出键为k的值,如果没有,则返回x

a.setdefault(k[, x])

将键为k的值设为默认值x。如果字典a中存在k,则返回k的值,如果不存在,向字典中添加k-x键值对,并返回值x

a.pop(k[, x])

取出字典a中键k的值,并将其从字典a中删除,如果字典a中没有键k,则返回值x

a.popitem()

取出字典a中键值对,并将其从字典a中删除

a.iteritems()

返回字典a所有键-值对的迭代器。

a.iterkeys()

返回字典a所有键的迭代器。

a.itervalues()

返回字典a所有值的迭代器。

注意:Dictionary中的key值是大小写敏感的。并且在同一个dictionary中不能有重复的key值。并且,Dictionary中没有元素顺序的概念。

  1. class dict(object):
  2. """
  3. dict() -> new empty dictionary
  4. dict(mapping) -> new dictionary initialized from a mapping object's
  5. (key, value) pairs
  6. dict(iterable) -> new dictionary initialized as if via:
  7. d = {}
  8. for k, v in iterable:
  9. d[k] = v
  10. dict(**kwargs) -> new dictionary initialized with the name=value pairs
  11. in the keyword argument list. For example: dict(one=1, two=2)
  12. """
  13.  
  14. def clear(self): # real signature unknown; restored from __doc__
  15. """ 清除内容 """
  16. """ D.clear() -> None. Remove all items from D. """
  17. pass
  18.  
  19. def copy(self): # real signature unknown; restored from __doc__
  20. """ 浅拷贝 """
  21. """ D.copy() -> a shallow copy of D """
  22. pass
  23.  
  24. @staticmethod # known case
  25. def fromkeys(S, v=None): # real signature unknown; restored from __doc__
  26. """
  27. dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.
  28. v defaults to None.
  29. """
  30. pass
  31.  
  32. def get(self, k, d=None): # real signature unknown; restored from __doc__
  33. """ 根据key获取值,d是默认值 """
  34. """ D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
  35. pass
  36.  
  37. def has_key(self, k): # real signature unknown; restored from __doc__
  38. """ 是否有key """
  39. """ D.has_key(k) -> True if D has a key k, else False """
  40. return False
  41.  
  42. def items(self): # real signature unknown; restored from __doc__
  43. """ 所有项的列表形式 """
  44. """ D.items() -> list of D's (key, value) pairs, as 2-tuples """
  45. return []
  46.  
  47. def iteritems(self): # real signature unknown; restored from __doc__
  48. """ 项可迭代 """
  49. """ D.iteritems() -> an iterator over the (key, value) items of D """
  50. pass
  51.  
  52. def iterkeys(self): # real signature unknown; restored from __doc__
  53. """ key可迭代 """
  54. """ D.iterkeys() -> an iterator over the keys of D """
  55. pass
  56.  
  57. def itervalues(self): # real signature unknown; restored from __doc__
  58. """ value可迭代 """
  59. """ D.itervalues() -> an iterator over the values of D """
  60. pass
  61.  
  62. def keys(self): # real signature unknown; restored from __doc__
  63. """ 所有的key列表 """
  64. """ D.keys() -> list of D's keys """
  65. return []
  66.  
  67. def pop(self, k, d=None): # real signature unknown; restored from __doc__
  68. """ 获取并在字典中移除 """
  69. """
  70. D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
  71. If key is not found, d is returned if given, otherwise KeyError is raised
  72. """
  73. pass
  74.  
  75. def popitem(self): # real signature unknown; restored from __doc__
  76. """ 获取并在字典中移除 """
  77. """
  78. D.popitem() -> (k, v), remove and return some (key, value) pair as a
  79. 2-tuple; but raise KeyError if D is empty.
  80. """
  81. pass
  82.  
  83. def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
  84. """ 如果key不存在,则创建,如果存在,则返回已存在的值且不修改 """
  85. """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
  86. pass
  87.  
  88. def update(self, E=None, **F): # known special case of dict.update
  89. """ 更新
  90. {'name':'alex', 'age': 18000}
  91. [('name','sbsbsb'),]
  92. """
  93. """
  94. D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
  95. If E present and has a .keys() method, does: for k in E: D[k] = E[k]
  96. If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v
  97. In either case, this is followed by: for k in F: D[k] = F[k]
  98. """
  99. pass
  100.  
  101. def values(self): # real signature unknown; restored from __doc__
  102. """ 所有的值 """
  103. """ D.values() -> list of D's values """
  104. return []
  105.  
  106. def viewitems(self): # real signature unknown; restored from __doc__
  107. """ 所有项,只是将内容保存至view对象中 """
  108. """ D.viewitems() -> a set-like object providing a view on D's items """
  109. pass
  110.  
  111. def viewkeys(self): # real signature unknown; restored from __doc__
  112. """ D.viewkeys() -> a set-like object providing a view on D's keys """
  113. pass
  114.  
  115. def viewvalues(self): # real signature unknown; restored from __doc__
  116. """ D.viewvalues() -> an object providing a view on D's values """
  117. pass
  118.  
  119. def __cmp__(self, y): # real signature unknown; restored from __doc__
  120. """ x.__cmp__(y) <==> cmp(x,y) """
  121. pass
  122.  
  123. def __contains__(self, k): # real signature unknown; restored from __doc__
  124. """ D.__contains__(k) -> True if D has a key k, else False """
  125. return False
  126.  
  127. def __delitem__(self, y): # real signature unknown; restored from __doc__
  128. """ x.__delitem__(y) <==> del x[y] """
  129. pass
  130.  
  131. def __eq__(self, y): # real signature unknown; restored from __doc__
  132. """ x.__eq__(y) <==> x==y """
  133. pass
  134.  
  135. def __getattribute__(self, name): # real signature unknown; restored from __doc__
  136. """ x.__getattribute__('name') <==> x.name """
  137. pass
  138.  
  139. def __getitem__(self, y): # real signature unknown; restored from __doc__
  140. """ x.__getitem__(y) <==> x[y] """
  141. pass
  142.  
  143. def __ge__(self, y): # real signature unknown; restored from __doc__
  144. """ x.__ge__(y) <==> x>=y """
  145. pass
  146.  
  147. def __gt__(self, y): # real signature unknown; restored from __doc__
  148. """ x.__gt__(y) <==> x>y """
  149. pass
  150.  
  151. def __init__(self, seq=None, **kwargs): # known special case of dict.__init__
  152. """
  153. dict() -> new empty dictionary
  154. dict(mapping) -> new dictionary initialized from a mapping object's
  155. (key, value) pairs
  156. dict(iterable) -> new dictionary initialized as if via:
  157. d = {}
  158. for k, v in iterable:
  159. d[k] = v
  160. dict(**kwargs) -> new dictionary initialized with the name=value pairs
  161. in the keyword argument list. For example: dict(one=1, two=2)
  162. # (copied from class doc)
  163. """
  164. pass
  165.  
  166. def __iter__(self): # real signature unknown; restored from __doc__
  167. """ x.__iter__() <==> iter(x) """
  168. pass
  169.  
  170. def __len__(self): # real signature unknown; restored from __doc__
  171. """ x.__len__() <==> len(x) """
  172. pass
  173.  
  174. def __le__(self, y): # real signature unknown; restored from __doc__
  175. """ x.__le__(y) <==> x<=y """
  176. pass
  177.  
  178. def __lt__(self, y): # real signature unknown; restored from __doc__
  179. """ x.__lt__(y) <==> x<y """
  180. pass
  181.  
  182. @staticmethod # known case of __new__
  183. def __new__(S, *more): # real signature unknown; restored from __doc__
  184. """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
  185. pass
  186.  
  187. def __ne__(self, y): # real signature unknown; restored from __doc__
  188. """ x.__ne__(y) <==> x!=y """
  189. pass
  190.  
  191. def __repr__(self): # real signature unknown; restored from __doc__
  192. """ x.__repr__() <==> repr(x) """
  193. pass
  194.  
  195. def __setitem__(self, i, y): # real signature unknown; restored from __doc__
  196. """ x.__setitem__(i, y) <==> x[i]=y """
  197. pass
  198.  
  199. def __sizeof__(self): # real signature unknown; restored from __doc__
  200. """ D.__sizeof__() -> size of D in memory, in bytes """
  201. pass
  202.  
  203. __hash__ = None
  204.  
  205. dict

python基本数据类型——dict的更多相关文章

  1. python基础数据类型--dict 字典

    字典 字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据.python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必 ...

  2. Python - 基础数据类型 dict 字典

    字典简介 字典在 Python 里面是非常重要的数据类型,而且很常用 字典是以关键字(键)为索引,关键字(键)可以是任意不可变类型 字典由键和对应值成对组成,字典中所有的键值对放在 { } 中间,每一 ...

  3. python:数据类型dict

    一.字典 key -->value 储存大量数据,而且是关系型数据,查询速度非常快 数据类型分类: 可变数据类型:list , dict, set 不可变的数据类型:int , bool, st ...

  4. Python 基础数据类型之dict

    字典是另一种可变容器模型,且可存储任意类型对象.字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示:d = {k ...

  5. Python基础数据类型-字典(dict)

    Python基础数据类型-字典(dict) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的每一篇都是Python3.x版本的哟 ...

  6. python基本数据类型list,tuple,set,dict用法以及遍历方法

    1.list类型 类似于java的list类型,数据集合,可以追加元素与删除元素. 遍历list可以用下标进行遍历,也可以用迭代器遍历list集合 建立list的时候用[]括号 import sys ...

  7. python 基本数据类型分析

    在python中,一切都是对象!对象由类创建而来,对象所拥有的功能都来自于类.在本节中,我们了解一下python基本数据类型对象具有哪些功能,我们平常是怎么使用的. 对于python,一切事物都是对象 ...

  8. python常用数据类型内置方法介绍

    熟练掌握python常用数据类型内置方法是每个初学者必须具备的内功. 下面介绍了python常用的集中数据类型及其方法,点开源代码,其中对主要方法都进行了中文注释. 一.整型 a = 100 a.xx ...

  9. Day02 - Python 基本数据类型

    1 基本数据类型 Python有五个标准的数据类型: Numbers(数字) String(字符串) List(列表) Tuple(元组) Dictionary(字典) 1.1 数字 数字数据类型用于 ...

随机推荐

  1. 4063: [Cerc2012]Darts

    4063: [Cerc2012]Darts Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 85  Solved: 53[Submit][Status] ...

  2. Android:NavigationView 导航抽屉

    NavigationView是一种标准的应用导航菜单,菜单栏的内容可以来自菜单栏资源文件. NavigationView最典型的应用场景是放到DrawerLayout里使用. API:https:// ...

  3. 深入理解java String 及intern

    一.字符串问题 字符串在我们平时的编码工作中其实用的非常多,并且用起来也比较简单,所以很少有人对其做特别深入的研究.倒是面试或者笔试的时候,往往会涉及比较深入和难度大一点的问题.我在招聘的时候也偶尔会 ...

  4. 基于C#的UDP通信(使用UdpClient实现,包含发送端和接收端)

    UDP不属于面向连接的通信,在选择使用协议的时候,选择UDP必须要谨慎.在网络质量令人十分不满意的环境下,UDP协议数据包丢失会比较严重.但是由于UDP的特性:它不属于连接型协议,因而具有资源消耗小, ...

  5. 搭建ftp服务器实现文件共享

    FTP服务器(File Transfer Protocol Server)是在互联网上提供文件存储和访问服务的计算机,它们依照FTP协议提供服务. FTP(File Transfer Protocol ...

  6. Struts 基本概念,优点及不同版本之间的关系

    strutx 1.x struts 是 apache 基金会的一个开源项目. struts 是一套实现 MVC的框架. MVC = 程序分层设计的思想 = Model(数据访问层1) / View(视 ...

  7. 把GIF背景变透明

    准备软件: 1.Ps cs4 2.QuickTime Player 7.74 开始: 1. 2.弹出文件选择框,但是发现不能选择GIF格式. 3.没关系,在文件名框输入*.*回车,就发现可以选择GIF ...

  8. Struts2(二)之封装请求正文、数据类型转换、数据验证

    一.封装请求正文到对象中(重点) 1.1.静态参数封装 在struts.xml文件中,给动作类注入值,使用的是setter方法 1.2.动态参数封装 通过用户表单封装请求正文参数 1.2.1.动作类作 ...

  9. 3.Maven坐标和依赖

    1.1 何为Maven坐标 正如之前所说的,Maven的一大功能就是管理项目依赖.为了能自动化地解析任何一个Java构件,Maven就必须将它们唯一标识,这就依赖管理的底层基础——坐标. 1.2 坐标 ...

  10. Java中一个方法只被一个线程调用一次

    1.想在运行时抛出异常,终止方法的运行 private final Set<Long> THREADS = new HashSet<>(); public void someM ...