dict内建函数梳理

函数名:clear

#clear()方法从字典中删除所有项。

#clear()方法不返回任何值(返回None)。

>>> d = {1: "one", 2: "two"}
>>> d.clear()
>>> d
{}

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

函数名:copy

#返回字典的一个浅拷贝。

>>> d = {1: "one", 2: "two"}
>>> a=d.copy()
>>> a
{1: 'one', 2: 'two'}

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

函数名:fromkeys

#从给定的元素序列中创建了一个新字典,它的值由用户提供。

#fromkeys()方法采用两个参数:

序列-元素的序列,它将被用作新字典的键。

值(可选)-值,该值被设置为字典的每个元素。

>>> keys = {'a', 'e', 'i', 'o', 'u' }
>>> vowels = dict.fromkeys(keys)
>>> vowels
{'o': None, 'i': None, 'e': None, 'a': None, 'u': None}

>>> vowels = dict.fromkeys(keys,'a')
>>> vowels
{'o': 'a', 'i': 'a', 'e': 'a', 'a': 'a', 'u': 'a'}
>>>

Returns a new dict with keys from iterableand values equal to value.

函数名:get

#返回指定键的值,如果键在字典中。

#get()方法最多使用两个参数:

在字典中搜索关键字.

值(可选)-如果未找到键,则返回值。默认值是None。

>>> person = {'name': 'Phill', 'age': 22}
>>> person.get('name')
'Phill'
>>> person.get('a')
>>> 

D.get(k[,d]) -> D[k] if k in D, elsed.  d defaults to None.

函数名:items

#返回一个对象,该对象显示一个字典的(键值)元组对。

#类似于Python 2.7中dictionary的viewitems()方法。

#返回一个对象,该对象显示给定的dictionary(键值)tuple对的 列表 。

>> sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }
>>> sales.items()
dict_items([('apple', 2), ('orange', 3), ('grapes', 4)])

D.items() -> a set-like object providinga view on D's items

函数名:keys

#返回一个对象,该对象显示字典中所有键的列表。

>>> sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }
>> sales.keys()
dict_keys(['apple', 'orange', 'grapes'])

D.keys() -> a set-like object providinga view on D's keys

函数名:pop

#从具有给定键的字典中删除并返回一个元素。

# 如果不存在,报错。

>>> sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }
>>> sales.pop('apple')
2
>>> sales
{'orange': 3, 'grapes': 4}
>>> sales.pop('1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: '1'

D.pop(k[,d]) -> v, remove specified keyand return the corresponding value.

If key is not found, d is returned ifgiven, otherwise KeyError is raised

函数名:popitem

#popitem()返回并从字典中删除一个任意元素(键值)对。

#popitem()

从字典中返回一个任意的元素(键值)对。

从字典中删除一个任意的元素(返回的相同元素)。

>>> sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }
>>> res=sales.popitem()
>>> res
('grapes', 4)
>>> sales
{'apple': 2, 'orange': 3}

D.popitem() -> (k, v), remove and returnsome (key, value) pair as a

2-tuple; but raise KeyError if D is empty.

函数名:setdefault

#setdefault()方法返回键值(如果键在字典中)。如果没有,则将键插入到字典中。

>>> person = {'name': 'Phill', 'age': 22}
>>> person.setdefault('age')
22
>>> person.setdefault('test')
>>> person
{'name': 'Phill', 'age': 22, 'test': None}

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

函数名:update

#使用来自另一个dictionary对象的元素或从一个可迭代的键/值对来更新字典。

>>> d = {1: "one", 2: "three"}
>>> d1 = {2: "two"}
>>> d.update(d1)
>>> d
{1: 'one', 2: 'two'}
>>> d.update(a='c',b='abc')
>>> d
{1: 'one', 2: 'two', 'a': 'c', 'b': 'abc'}

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 kin F:  D[k] = F[k]

函数名:values

#返回一个对象,它显示字典中所有值的列表。

>>> d
{1: 'one', 2: 'two', 'a': 'c', 'b': 'abc'}
>>> d.values()
dict_values(['one', 'two', 'c', 'abc'])

D.values() -> an object providing a viewon D's values



























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值