Python字典方法总结

时间:2023-02-10 21:25:27

1.清空字典中元素清空,dict变为{}

L.clear()-> None.  Remove all items from L

1
2
3
4
>>> L ={'shaw':23,'sam':36,"eric":40}
>>> L.clear()
>>> print L
{}

2. 返回一个字典的浅复制

L.copy()-> a shallow copy of L

1
2
3
>>> L ={'shaw':23,'sam':36,"eric":40}
>>> L.copy()
{'shaw': 23, 'sam': 36,'eric': 40}

3. 用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值(默认为“None”)

Lict.fromkeys(S[,v])-> New Lict with keys from S and values equal to v. v defaults toNone.

1
2
3
4
5
6
7
>>> seq =('shaw','sam','stiven')
>>> name =dict.fromkeys(seq)
>>> print "listone:{}".format(name)
listone :{'stiven': None,'shaw': None, 'sam': None}
>>> name =dict.fromkeys(seq,1000)
>>> print"listone :{}".format(name)
listone :{'stiven': 1000,'shaw': 1000, 'sam': 1000}

4.返回指定键的值,如果值不在字典中返回默认值(None)

D.get(k[,d])-> D[k] if k in D, else d.  d defaultsto None.

1
2
3
4
5
>>> L ={'shaw':23,'sam':36,"eric":40}
>>> L.get('shaw')
23
>>> printL.get('stiven')
None

5.用于判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false

L.has_key(k) -> True if D has a key k,else False

1
2
3
4
5
>>> L ={'shaw':23,'sam':36,"eric":40}
>>> L.has_key('sam')
True
>>>L.has_key('linux')
False

6. 以列表的方式返回可遍历的(键, 值) 元组(键值对)

L.items()-> list of D's (key, value) pairs, as 2-tuples

1
2
3
>>> L ={'shaw':23,'sam':36,"eric":40}
>>> L.items()
[('shaw', 23), ('sam', 36),('eric', 40)]

7. 以列表的方式返回一个字典所有的键

L.keys()-> a set-like object providing a view on L's keys

1
2
3
>>> L ={'shaw':23,'sam':36,"eric":40}
>>> L.keys()
['shaw', 'sam', 'eric']

8. 删除某个键值对

D.pop(k[,d])-> v, remove specified key and return the corresponding value. If key is notfound, d is returned if given, otherwise KeyError is raised

1
2
3
4
5
>>> L ={'shaw':23,'sam':36,"eric":40}
>>> L.pop('sam')
36
>>> L
{'shaw': 23, 'eric': 40}

9. 默认删除字典中第一个键值对

D.popitem()-> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty.

1
2
3
4
5
>>> L ={'shaw':23,'sam':36,"eric":40}
>>> L.popitem()
('shaw', 23)
>>> L
{'sam': 36, 'eric': 40}

10. setdefault()方法和get()方法类似,如果键不已经存在于字典中,将会添加键并将值设为默认值(如果dict中已有a,则不会被覆盖)

D.setdefault(k[,d]) ->D.get(k,d), also set D[k]=d if k not in D

1
2
3
4
5
6
7
8
9
10
11
12
>>> L ={'shaw':23,'sam':36,"eric":40}
>>>L.setdefault('stiven')
>>> L
{'stiven': None, 'shaw': 23,'sam': 36, 'eric': 40}
>>>L.setdefault('mira',65)
65
>>> L
{'stiven': None, 'mira': 65,'shaw': 23, 'sam': 36, 'eric': 40}
>>>L.setdefault('shaw',18)
23
>>> L
{'stiven': None, 'mira': 65,'shaw': 23, 'sam': 36, 'eric': 40}

11. 把字典dict2的键/值对更新到dict里

L.update()

1
2
3
4
5
>>> L ={'shaw':23,'sam':36,"eric":40}
>>> A ={'book':45,'apple':13}
>>> L.update(A)
>>> L
{'book': 45, 'apple': 13,'shaw': 23, 'sam': 36, 'eric': 40}

12.返回dic所有的值

L.values(…)

1
2
3
>>> L ={'book':45,'apple':13}
>>> L.values()
[45, 13]

Python字典方法总结的更多相关文章

  1. Python 字典方法

    访问字典的值 字典中的 键/值 实际上就是一种映射关系,只要知道了 “键”,就肯定知道 “值”. >>> my_dict = dict(name = 'zhangsan',other ...

  2. python字典方法

    本文参考自<python基础教程 (第二版)> 操作 语法 举例 结果 建立字典 dict() 1.以关键字参数建立字典 2.以其他映射作为参数建立字典 1.d = dict(name=' ...

  3. Python字典方法copy&lpar;&rpar;和deepcopy&lpar;&rpar;的区别

    from copy import deepcopy # import deepcopy模块 d = {} d['name'] = ['black', 'guts'] # d = {'name': [' ...

  4. python字典copy&lpar;&rpar;方法

    python 字典的copy()方法表面看就是深copy啊,明显独立 d = {'a':1, 'b':2} c = d.copy() print('d=%s c=%s' % (d, c)) Code1 ...

  5. Python 字典&lpar;Dictionary&rpar; get&lpar;&rpar;方法

    描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...

  6. Python 字典&lpar;Dictionary&rpar; setdefault&lpar;&rpar;方法

    描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: ...

  7. python 字典内置方法get应用

    python字典内置方法get应用,如果我们需要获取字典值的话,我们有两种方法,一个是通过dict['key'],另外一个就是dict.get()方法. 今天给大家分享的就是字典的get()方法. 这 ...

  8. Python 字典&lpar;Dictionary&rpar; has&lowbar;key&lpar;&rpar;方法

    描述 Python 字典(Dictionary) has_key() 函数用于判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false. 语法 has_key()方法语法:dic ...

  9. Python 字典 fromkeys&lpar;&rpar;方法

    Python 字典 fromkeys() 方法用于创建一个新的字典,并以可迭代对象中的元素分别作为字典中的键,且所有键对应同一个值,默认为None. fromkeys() 方法语法: 1 dict.f ...

随机推荐

  1. (转)学习使用Jmeter做压力测试&lpar;一&rpar;--压力测试基本概念

    一.性能测试的概念 性能测试是通过自动化的测试工具模拟多种正常峰值及异常负载条件来对系统的各项性能指标进行测试.负载测试和压力测试都属于性能测试,两者可以结合进行. 通过负载测试,确定在各种工作负载下 ...

  2. 关于STM32的抢占式优先级说明。——Arvin

    关于STM32的中断设置.--Arvin 中断 STM32 很多人在配置STM32中断时对固件库中的这个函数NVIC_PriorityGroupConfig()配置优先级分组方式,会很不理解,尤其是看 ...

  3. Android优化——UI检视利器:Hierarchy Viewer

    在Android的SDK工具包中,有很多十分有用的工具,可以帮助程序员开发和测试Android应用程序,大大提高其工作效率.其中的一款叫 Hierachy Viewer的可视化调试工具,可以很方便地在 ...

  4. GetKeyState和GetAsyncKeyState以及GetKeyboardState函数的用法与区别

    GetKeyState.GetAsyncKeyState.GetKeyboardState函数的区别: 1.BOOL GetKeyboardState( PBYTE lpKeyState );获得所有 ...

  5. Inside Microsoft SQL Server 2008&colon; T-SQL Querying 读书笔记之查询优化

    一. 自顶向下优化方法论 1. 分析实例级别的等待 在实例级找出什么类型的等待占用大部分的时间,通过sys.dm_os_wait_stats select wait_type, --等待类型 wait ...

  6. php configure help

    `configure' configures this package to adapt to many kinds of systems. Usage: ./configure [OPTION].. ...

  7. 一种解决eclipse中安装maven出错的方法

    1.安装步骤:https://jingyan.baidu.com/article/a17d5285feb4dd8099c8f26e.html 2.安装第三步的解决办法:m2e   路径换成  http ...

  8. Python 爬虫性能相关

    性能相关 在编写爬虫时,性能的消耗主要在IO请求中,当单进程单线程模式下请求URL时必然会引起等待,从而使得请求整体变慢. import requests def fetch_async(url): ...

  9. python向config、ini文件读取写入

    config读取操作 cf = configparser.ConfigParser() # 实例化对象 cf.read(filename) # 读取文件 cf.sections() # 读取secti ...

  10. Linux 最小化安装后IP的配置(DHCP获取IP地址)

    图形化Linux的DHCP好配置,我就不讲了.主要将一下Linux最小化安装后IP的配置: linux最小化安装后没有ifconfig这个命令: yum install net-tools.x86_6 ...