Python成长之路第二篇_字典的置函数用法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python成长之路第二篇_字典的置函数用法相关的知识,希望对你有一定的参考价值。

字典的置函数用法(字典dict字典中的key不可以重复)

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)

"""

(1)def clear(self): 清除内容

# real signature unknown; restored from __doc__

""" 清除内容 """

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

pass

(2)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

深Copy

(3)def get(self, k, d=None): 根据key获取值

# 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

(4)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.
"""

(5)def has_key(self, k): 是否有这个key

# real signature unknown; restored from __doc__

""" 是否有key """

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

return False

(6)def items(self):转换成列表

# real signature unknown; restored from __doc__

""" 所有项的列表形式 """

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

return []

列表循环

技术分享

(7)def iteritems(self):迭代

# real signature unknown; restored from __doc__

""" 项可迭代 """

""" D.iteritems() -> an iterator over the (key, value) items of D """

pass

(8)def iterkeys(self): key可迭代

# real signature unknown; restored from __doc__

""" key可迭代 """

""" D.iterkeys() -> an iterator over the keys of D """

pass

(9)def itervalues(self): value可迭代

# real signature unknown; restored from __doc__

""" value可迭代 """

""" D.itervalues() -> an iterator over the values of D """

pass

(10)def keys(self):显示所有的key列表

# real signature unknown; restored from __doc__

""" 所有的key列表 """

""" D.keys() -> list of D‘s keys """

return []

(11)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

(12)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

(13)def setdefault(self, k, d=None): 如果key不存在,则创建,如果存在,则返回已存在的值且不修改

# 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

(14)def update(self, E=None, **F):整合字典C字典更新d字典

# 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

(15)def values(self): 打印所有的val

# real signature unknown; restored from __doc__

""" 所有的值 """

""" D.values() -> list of D‘s values """

return []

(16)def viewitems(self): 查看所有项,只是将内容保存至view对象中

# real signature unknown; restored from __doc__

""" 所有项,只是将内容保存至view对象中 """

""" D.viewitems() -> a set-like object providing a view on D‘s items """

Pass

以上是关于Python成长之路第二篇_字典的置函数用法的主要内容,如果未能解决你的问题,请参考以下文章

Python成长之路第二篇_列表元组内置函数用法

Python成长之路第二篇_数据类型内置函数用法

python基础5

python成长之路第二篇_collections系列

Python成长之路第二篇Python基础之数据类型

python成长之路第三篇_初识函数