首页 > 代码库 > python3字典方法统计

python3字典方法统计

1、key()

  • 官方说明:
技术分享
    def keys(self): # real signature unknown; restored from __doc__
        """ D.keys() -> a set-like object providing a view on D‘s keys """
        pass
View Code

描述:取出字典中所有的键

参数:

返回值:返回这个字典所有的键

  • 示例:
d = {‘name‘:‘william‘,‘age‘:30,‘sex‘:‘male‘}
d2 = d.keys()  # 取出字典中所有的键
print(type(d2),d2)

  输出结果:

技术分享
<class dict_keys> dict_keys([name, age, sex])
View Code

 

2、values()

  • 官方说明:
技术分享
    def values(self): # real signature unknown; restored from __doc__
        """ D.values() -> an object providing a view on D‘s values """
        pass
View Code

描述:取出字典中所有的值

参数:

返回值:返回这个字典中所有的值

  • 示例:
d = {‘name‘:‘william‘,‘age‘:30,‘sex‘:‘male‘}
d2 = d.values()  # 取出字典中所有的值
print(type(d2),d2)

  输出结果:

技术分享
<class dict_values> dict_values([william, 30, male])
View Code

 

3、clear()

  • 官方说明:
技术分享
    def clear(self): # real signature unknown; restored from __doc__
        """ D.clear() -> None.  Remove all items from D. """
        pass
View Code

描述:清空字典

参数:

返回值:None(原字典会被修改)

  • 示例:
d = {‘name‘:‘william‘,‘age‘:30,‘sex‘:‘male‘}
d.clear()    # 清空字典
print(type(d),d)

  输出结果:

技术分享
<class dict> {}
View Code

4、pop()

描述:删除指定的键值

参数:指定的key

返回值:返回被删除键的值

  • 示例:
d = {‘name‘:‘william‘,‘age‘:30,‘sex‘:‘male‘}
d2 = d.pop(‘name‘) # 删除指定的键
print(type(d2),d2) # 打印返回被删除的键的值
print(type(d),d)  

  输出结果:

技术分享
<class str> william
<class dict> {age: 30, sex: male}
View Code

 

5、copy()

  • 官方说明:
技术分享
    def copy(self): # real signature unknown; restored from __doc__
        """ D.copy() -> a shallow copy of D """
        pass
View Code

描述:复制当前字典

参数:

返回值:得到一个新的字典

  • 示例:
d = {‘name‘:‘william‘,‘age‘:30,‘sex‘:‘male‘}
d2 = d.copy()  # 复制当前字典
print(type(d2),d2) # 打印得到的新字典
print(type(d),d)

  输出结果:

技术分享
<class dict> {name: william, age: 30, sex: male}
<class dict> {name: william, age: 30, sex: male}
View Code

 

6、update()

  • 官方说明:
技术分享
    def update(self, E=None, **F): # known special case of dict.update
        """
        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]
        """
        pass
View Code

描述:扩展字典

参数:要添加的字典

返回值:None(原字典会被修改)

  • 示例:
d = {‘name‘:‘william‘,‘age‘:30,‘sex‘:‘male‘}
d2 = {‘hobby‘:‘football‘}
d.update(d2)  # 扩展字典
print(type(d),d)

  输出结果:

技术分享
<class dict> {name: william, age: 30, sex: male, hobby: football}
View Code

 

7、fromkeys

  • 官方说明:
技术分享
    def fromkeys(*args, **kwargs): # real signature unknown
        """ Returns a new dict with keys from iterable and values equal to value. """
        pass
View Code

描述:从序列键和值设置为value来创建一个新的字典。

参数:seq  用于字典的键准备的列表

   value  若提供值将被设置为这个值,否则默认为None

返回值:得到一个新的列表

  • 示例:
seq = (‘name‘, ‘age‘, ‘sex‘)
dict = dict.fromkeys(seq)  
print(dict)

dict = dict.fromkeys(seq, 10)
print(dict)

  输出结果:

技术分享
{name: None, age: None, sex: None}
{name: 10, age: 10, sex: 10}
View Code

 

8、get()

描述:返回指定键的值

参数:key  指定查找的键

   defaullt  如果要查找的键不存在时,则返回自定的值

返回值:返回指定键的值,如果要查找的键不存在时,则返回自定的值(不写默认为None)

  • 示例1:
d = {‘name‘:‘william‘,‘age‘:30,‘sex‘:‘male‘}
d2 = d.get(‘hobby‘)  # 查找的键不在字典中且后面不加值,则输出为None
print(type(d2),d2)

  输出结果:

技术分享
<class NoneType> None
View Code
  • 示例2:
d = {‘name‘:‘william‘,‘age‘:30,‘sex‘:‘male‘}
d2 = d.get(‘hobby‘,‘football‘) # 查找的键不在字典中且后面有值,则输出这个值
print(type(d2),d2)

  输出结果:

技术分享
<class str> football
View Code
  • 示例3:
d = {‘name‘:‘william‘,‘age‘:30,‘sex‘:‘male‘}
d2 = d.get(‘age‘)  # 查找的键存在字典中,则输出这个键对应的值
print(type(d2),d2)

  输出结果:

技术分享
<class int> 30
View Code

 

 

9、items

  • 官方说明:
技术分享
   def items(self): # real signature unknown; restored from __doc__
        """ D.items() -> a set-like object providing a view on D‘s items """
        pass
View Code

描述:返回可遍历的(键、值)元组数组

参数:

返回值:返回可遍历的(键、值)元组数组

  • 示例:
d = {‘name‘:‘william‘,‘age‘:30,‘sex‘:‘male‘}
d2 = d.items()
print(type(d2),d2)

  输出结果:

技术分享
<class dict_items> dict_items([(name, william), (age, 30), (sex, male)])
View Code

 

10、popitem()

  • 官方说明:
技术分享
    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
View Code

描述:随机返回并删除字典中的一对键和值。

参数:无

返回值:返回一对键和值(原字典会被修改)

  • 示例:
d = {‘name‘:‘william‘,‘age‘:30,‘sex‘:‘male‘}
d2 = d.popitem()
print(type(d2),d2)  # 随机返回一对键和值
print(type(d),d)   # 原有的字典将会被更改

  输出结果:

技术分享
<class tuple> (sex, male)
<class dict> {name: william, age: 30}
View Code

 

11、setdefault()

  • 官方说明:
技术分享
    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
View Code

描述:和get方法类似,如果键不存在,则将这个键加入到字典里,对应一个空值

参数:key  查找的键

   default  键不存在时,设置的默认键值

返回值:返回指定键的值(原字典不会被修改),如果键不存在,则返回None(原字典会被修改)

  • 示例1:
d = {‘name‘:‘william‘,‘age‘:30,‘sex‘:‘male‘}
d2 = d.setdefault(‘name‘)  # 返回指定键的值
print(type(d2),d2)

  输出结果:

技术分享
<class str> william
View Code
  • 示例2:
d = {‘name‘:‘william‘,‘age‘:30,‘sex‘:‘male‘}
d2 = d.setdefault(‘xxxxx‘)
print(type(d2),d2)
print(type(d),d)

  输出结果:

技术分享
<class NoneType> None
<class dict> {name: william, age: 30, sex: male, xxxxx: None}
View Code

 

python3字典方法统计