python基本数据类型——dict
一、创建字典:
- d = {
- "name": "morra", #字典是无序的
- "age": 99,
- "gender": 'm'
- }
- a = dict()
- b = dict(k1=123,k2="morra")
二、字典常用操作:
修改或增加字典:
- dict = {'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'}
- dict['ob1']='book'
- dict['ob4']='cow'
- print(dict)
- {'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中没有元素顺序的概念。
- 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(S, v=None): # real signature unknown; restored from __doc__
- """
- dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.
- v defaults to None.
- """
- pass
- def get(self, k, d=None): # real signature unknown; restored from __doc__
- """ 根据key获取值,d是默认值 """
- """ D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
- pass
- def has_key(self, k): # real signature unknown; restored from __doc__
- """ 是否有key """
- """ D.has_key(k) -> True if D has a key k, else False """
- return False
- def items(self): # real signature unknown; restored from __doc__
- """ 所有项的列表形式 """
- """ D.items() -> list of D's (key, value) pairs, as 2-tuples """
- return []
- def iteritems(self): # real signature unknown; restored from __doc__
- """ 项可迭代 """
- """ D.iteritems() -> an iterator over the (key, value) items of D """
- pass
- def iterkeys(self): # real signature unknown; restored from __doc__
- """ key可迭代 """
- """ D.iterkeys() -> an iterator over the keys of D """
- pass
- def itervalues(self): # real signature unknown; restored from __doc__
- """ value可迭代 """
- """ D.itervalues() -> an iterator over the values of D """
- pass
- def keys(self): # real signature unknown; restored from __doc__
- """ 所有的key列表 """
- """ D.keys() -> list of D's keys """
- return []
- 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__
- """ 如果key不存在,则创建,如果存在,则返回已存在的值且不修改 """
- """ 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
- """ 更新
- {'name':'alex', 'age': 18000}
- [('name','sbsbsb'),]
- """
- """
- D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
- If E present and has a .keys() method, does: for k in E: D[k] = E[k]
- If E present and lacks .keys() method, 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() -> list of D's values """
- return []
- def viewitems(self): # real signature unknown; restored from __doc__
- """ 所有项,只是将内容保存至view对象中 """
- """ D.viewitems() -> a set-like object providing a view on D's items """
- pass
- def viewkeys(self): # real signature unknown; restored from __doc__
- """ D.viewkeys() -> a set-like object providing a view on D's keys """
- pass
- def viewvalues(self): # real signature unknown; restored from __doc__
- """ D.viewvalues() -> an object providing a view on D's values """
- pass
- def __cmp__(self, y): # real signature unknown; restored from __doc__
- """ x.__cmp__(y) <==> cmp(x,y) """
- pass
- def __contains__(self, k): # real signature unknown; restored from __doc__
- """ D.__contains__(k) -> True if D has a key k, else False """
- return False
- def __delitem__(self, y): # real signature unknown; restored from __doc__
- """ x.__delitem__(y) <==> del x[y] """
- pass
- def __eq__(self, y): # real signature unknown; restored from __doc__
- """ x.__eq__(y) <==> x==y """
- pass
- def __getattribute__(self, name): # real signature unknown; restored from __doc__
- """ x.__getattribute__('name') <==> x.name """
- pass
- def __getitem__(self, y): # real signature unknown; restored from __doc__
- """ x.__getitem__(y) <==> x[y] """
- pass
- def __ge__(self, y): # real signature unknown; restored from __doc__
- """ x.__ge__(y) <==> x>=y """
- pass
- def __gt__(self, y): # real signature unknown; restored from __doc__
- """ x.__gt__(y) <==> x>y """
- 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): # real signature unknown; restored from __doc__
- """ x.__iter__() <==> iter(x) """
- pass
- def __len__(self): # real signature unknown; restored from __doc__
- """ x.__len__() <==> len(x) """
- pass
- def __le__(self, y): # real signature unknown; restored from __doc__
- """ x.__le__(y) <==> x<=y """
- pass
- def __lt__(self, y): # real signature unknown; restored from __doc__
- """ x.__lt__(y) <==> x<y """
- pass
- @staticmethod # known case of __new__
- def __new__(S, *more): # real signature unknown; restored from __doc__
- """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
- pass
- def __ne__(self, y): # real signature unknown; restored from __doc__
- """ x.__ne__(y) <==> x!=y """
- pass
- def __repr__(self): # real signature unknown; restored from __doc__
- """ x.__repr__() <==> repr(x) """
- pass
- def __setitem__(self, i, y): # real signature unknown; restored from __doc__
- """ x.__setitem__(i, y) <==> x[i]=y """
- pass
- def __sizeof__(self): # real signature unknown; restored from __doc__
- """ D.__sizeof__() -> size of D in memory, in bytes """
- pass
- __hash__ = None
- dict
python基本数据类型——dict的更多相关文章
- python基础数据类型--dict 字典
字典 字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据.python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必 ...
- Python - 基础数据类型 dict 字典
字典简介 字典在 Python 里面是非常重要的数据类型,而且很常用 字典是以关键字(键)为索引,关键字(键)可以是任意不可变类型 字典由键和对应值成对组成,字典中所有的键值对放在 { } 中间,每一 ...
- python:数据类型dict
一.字典 key -->value 储存大量数据,而且是关系型数据,查询速度非常快 数据类型分类: 可变数据类型:list , dict, set 不可变的数据类型:int , bool, st ...
- Python 基础数据类型之dict
字典是另一种可变容器模型,且可存储任意类型对象.字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示:d = {k ...
- Python基础数据类型-字典(dict)
Python基础数据类型-字典(dict) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的每一篇都是Python3.x版本的哟 ...
- python基本数据类型list,tuple,set,dict用法以及遍历方法
1.list类型 类似于java的list类型,数据集合,可以追加元素与删除元素. 遍历list可以用下标进行遍历,也可以用迭代器遍历list集合 建立list的时候用[]括号 import sys ...
- python 基本数据类型分析
在python中,一切都是对象!对象由类创建而来,对象所拥有的功能都来自于类.在本节中,我们了解一下python基本数据类型对象具有哪些功能,我们平常是怎么使用的. 对于python,一切事物都是对象 ...
- python常用数据类型内置方法介绍
熟练掌握python常用数据类型内置方法是每个初学者必须具备的内功. 下面介绍了python常用的集中数据类型及其方法,点开源代码,其中对主要方法都进行了中文注释. 一.整型 a = 100 a.xx ...
- Day02 - Python 基本数据类型
1 基本数据类型 Python有五个标准的数据类型: Numbers(数字) String(字符串) List(列表) Tuple(元组) Dictionary(字典) 1.1 数字 数字数据类型用于 ...
随机推荐
- 4063: [Cerc2012]Darts
4063: [Cerc2012]Darts Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 85 Solved: 53[Submit][Status] ...
- Android:NavigationView 导航抽屉
NavigationView是一种标准的应用导航菜单,菜单栏的内容可以来自菜单栏资源文件. NavigationView最典型的应用场景是放到DrawerLayout里使用. API:https:// ...
- 深入理解java String 及intern
一.字符串问题 字符串在我们平时的编码工作中其实用的非常多,并且用起来也比较简单,所以很少有人对其做特别深入的研究.倒是面试或者笔试的时候,往往会涉及比较深入和难度大一点的问题.我在招聘的时候也偶尔会 ...
- 基于C#的UDP通信(使用UdpClient实现,包含发送端和接收端)
UDP不属于面向连接的通信,在选择使用协议的时候,选择UDP必须要谨慎.在网络质量令人十分不满意的环境下,UDP协议数据包丢失会比较严重.但是由于UDP的特性:它不属于连接型协议,因而具有资源消耗小, ...
- 搭建ftp服务器实现文件共享
FTP服务器(File Transfer Protocol Server)是在互联网上提供文件存储和访问服务的计算机,它们依照FTP协议提供服务. FTP(File Transfer Protocol ...
- Struts 基本概念,优点及不同版本之间的关系
strutx 1.x struts 是 apache 基金会的一个开源项目. struts 是一套实现 MVC的框架. MVC = 程序分层设计的思想 = Model(数据访问层1) / View(视 ...
- 把GIF背景变透明
准备软件: 1.Ps cs4 2.QuickTime Player 7.74 开始: 1. 2.弹出文件选择框,但是发现不能选择GIF格式. 3.没关系,在文件名框输入*.*回车,就发现可以选择GIF ...
- Struts2(二)之封装请求正文、数据类型转换、数据验证
一.封装请求正文到对象中(重点) 1.1.静态参数封装 在struts.xml文件中,给动作类注入值,使用的是setter方法 1.2.动态参数封装 通过用户表单封装请求正文参数 1.2.1.动作类作 ...
- 3.Maven坐标和依赖
1.1 何为Maven坐标 正如之前所说的,Maven的一大功能就是管理项目依赖.为了能自动化地解析任何一个Java构件,Maven就必须将它们唯一标识,这就依赖管理的底层基础——坐标. 1.2 坐标 ...
- Java中一个方法只被一个线程调用一次
1.想在运行时抛出异常,终止方法的运行 private final Set<Long> THREADS = new HashSet<>(); public void someM ...