Python之字典方法
def clear(self): # 清除所有内容
""" D.clear() -> None. Remove all items from D. """
pass def copy(self): # 浅复制
""" D.copy() -> a shallow copy of D """ 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): # 根据字典的key获取值,如果key不存在,返回None。
""" D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
pass def items(self): # 获取字典所有的键值对
""" D.items() -> a set-like object providing a view on D's items """
pass
example:
dic = {
1: 'abc',
2: 'def',
3: 'hjk'
} for k,v in dic.items():
print(k,v)
1 abc #返回结果
2 def
3 hjk
def keys(self): # 获取字典所有的key
""" D.keys() -> a set-like object providing a view on D's keys """
pass def pop(self, k, d=None): # 获取并在字典中移除,可指定移除。
"""
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): # 获取并在字典中移除,只能从尾部移除。
"""
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): # 批量更新
"""
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]
"""
example:
dic = {
1: 'abc',
2: 'def',
3: 'hjk'
} test = {
4: 'lmn',
5: 'opq'
}
dic.update(test)
print(dic)
{1: 'abc', 2: 'def', 3: 'hjk', 4: 'lmn', 5: 'opq'} #返回结果 def values(self): # 获取字典所有的值
""" D.values() -> an object providing a view on D's values """
pass
Python之字典方法的更多相关文章
- Python dict字典方法完全攻略(全)
我们知道,Python 字典的数据类型为 dict,我们可使用 dir(dict) 来查看该类型包含哪些方法,例如: >>> dir(dict)['clear', 'copy', ' ...
- python 通用字典方法
版本1 方法 # 不传返回所有属性,传入props只返回传入的对应属性 def m_dict(obj, props=[]): result = {} target = obj else props f ...
- Python——字典与字典方法
字典是一种通过名字或者关键字引用的得数据结构,其键可以是数字.字符串.元组,这种结构类型也称之为映射.字典类型是Python中唯一內建的映射类型,基本的操作包括如下: (1)len():返回字典中键— ...
- Python基础 第四章 字典(2)字典方法&章小结
1. clear 方法clear删除所有的字典项,就地执行,什么都不返回(或者说返回None) d = {} d['name'] = 'Gumby' d['age'] = 42 print(d) re ...
- python将字典列表导出为Excel文件的方法
将如下的字典列表内容导出为Excel表格文件形式: 关于上图字典列表的写入,请参考文章:https://blog.csdn.net/weixin_39082390/article/details/ ...
- Python常用数据结构-字典——2.1 字典方法 keys()
python字典常用方法: keys() # 获取所有的键 values() # 获取所有的值 items() # 获 ...
- Python 迭代器 & __iter__方法
转载来自: http://blog.csdn.net/bluebird_237/article/details/38894617 迭代器就是重复地做一些事情,可以简单的理解为循环,在python中实现 ...
- Python中sorted()方法
Python中sorted()方法的用法 1.先说一下iterable,中文意思是迭代器. Python的帮助文档中对iterable的解释是:iteralbe指的是能够一次返回它的一个成员的对象.i ...
- python类及其方法
python类及其方法 一.介绍 在 Python 中,面向对象编程主要有两个主题,就是类和类实例类与实例:类与实例相互关联着:类是对象的定义,而实例是"真正的实物",它存放了类中 ...
随机推荐
- leetcode实战
leetcode记录 两数之和 题目 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...
- 【云短信】腾讯&阿里
腾讯 : https://github.com/qcloudsms/qcloudsms_csharp 安装nuget包: using qcloudsms_csharp; using System.Co ...
- Git Learning2 GitHub upload
1.在自己的github上创建一个仓库 2.git remote add [name] [link] 使用git来增加一个link的别名 3.git push [linkname] [分支名] 4.g ...
- DAY18 常用模块(二)
一.随机数:RANDOM 1.(0,1)小数:random.random() 2.[1,10]整数:random.randint(1,10) 3.[1,10)整数:random.randrang(1, ...
- Python进行JSON格式化输出,以及汉字显示问题
格式化输出 转载地址 https://blog.csdn.net/real_tino/article/details/76422634 问题分析: Python下json手法的json在打印查看时, ...
- 括号生成(Java实现)
题目: 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: [ "((()))", "(()( ...
- [数据结构] 2.2 Huffman树
注:本文原创,转载请注明出处,本人保留对未注明出处行为的责任追究. 1.Huffman树是什么 Huffman树也称为哈夫曼编码,是一种编码方式,常用于协议的制定,以节省传输空间. A - F字母,出 ...
- hdoj3251
这题告诉我们,最小割需:满流,S断不能到T端P4126,hdoj3987 #include <iostream> #include <cstdio> #include < ...
- 成功解决internal/modules/cjs/loader.js:596 throw err; ^ Error: Cannot find module 'express'
^ Error: Cannot find module 'express'根据提示我们就可以知道,没有找到express这个模块,解决办法就是:npm install express
- 安装Windows 和 Linux双系统(vmware) Centos7
这里我安装的是Windows + Centos 7,如果是要安装Centos 6,步骤一样 一.安装Windows和Linux双系统需要先安装Windows然后安装Linux 解释:这里解释下为什么要 ...