首页 > 代码库 > python手记(7)------字典(操作方法)

python手记(7)------字典(操作方法)

1.dict方法概述

In[70]: dir(dict)
Out[69]: 

[__class__,
 __contains__,
 __delattr__,
 __delitem__,
 __dir__,
 __doc__,
 __eq__,
 __format__,
 __ge__,
 __getattribute__,
 __getitem__,
 __gt__,
 __hash__,
 __init__,
 __init_subclass__,
 __iter__,
 __le__,
 __len__,
 __lt__,
 __ne__,
 __new__,
 __reduce__,
 __reduce_ex__,
 __repr__,
 __setattr__,
 __setitem__,
 __sizeof__,
 __str__,
 __subclasshook__,
 clear,
 copy,
 fromkeys,
 get,
 items,
 keys,
 pop,
 popitem,
 setdefault,
 update,
 values]

 

2.copy():

  dict.copy()——返回一个新的(浅拷贝)——列表这类复杂数据需要深拷贝。。

3.clear()

   清空字典中所有元素 同 b={}

In[86]: b
Out[85]: 
{age: 26, name: ddd, university: shanghaicaijing}
In[87]: b.clear()
In[88]: b
Out[87]: 
{}
In[89]: b={}

 

4.items,keys,values

  分别返回字典中的类似集合类对象。有迭代行,用来遍历方便。

In[91]: city2
Out[90]: 

{fifth: [zhengzhou, hefei, wuhan],
 first: beijing,
 forth: shenzhen,
 second: shanghai}
In[92]: city2.items()
Out[91]: 
dict_items([(first, beijing), (second, shanghai), (forth, shenzhen), (fifth, [zhengzhou, hefei, wuhan])])
In[93]: city2.keys()
Out[92]: 
dict_keys([first, second, forth, fifth])
In[94]: city2.values()
Out[93]: 
dict_values([beijing, shanghai, shenzhen, [zhengzhou, hefei, wuhan]])
In[95]: for i in city2.keys():
...     print(i)
...     
first
second
forth
fifth

 

5.删除方法——pop() & popitem()

  pop()——删除该键值对,并返回值。如果没有该键,可以返回信息或报错。

In[96]: city
Out[95]: 
{first: beijing, second: shanghai}
In[97]: city.pop(third,未有{0}项.format(third))
Out[96]: 
未有third项
In[98]: city.pop(first)
Out[97]: 
beijing

 

 popitem()————随机删除,无需参数,返回类型为数组,空数组报错。

In[99]: help(dict.popitem)
Help on method_descriptor:

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

 

In[102]: city3
Out[101]: 

{fifth: [zhengzhou, hefei, wuhan],
 first: beijing,
 forth: shenzhen,
 second: shanghai}
In[103]: city3.popitem()
Out[102]: 
(fifth, [zhengzhou, hefei, wuhan])
In[104]: city3.clear()
In[105]: city3.popitem()
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-104-c47179c00790>", line 1, in <module>
    city3.popitem()
KeyError: popitem(): dictionary is empty

 

6. 更新字典——update()

  

In[107]: help(dict.update)
Help on method_descriptor:

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]

    dict.update(可迭代对象/字典2)————无返回值,增加字典2中的内容到字典中

 

In[108]: a
Out[107]: 
{age: 26, name: eric, number: 201521234, university: CUP}
In[109]: city2
Out[108]: 

{fifth: [zhengzhou, hefei, wuhan],
 first: beijing,
 forth: shenzhen,
 second: shanghai}
In[110]: a.update(city2)
In[111]: a
Out[110]: 

{age: 26,
 fifth: [zhengzhou, hefei, wuhan],
 first: beijing,
 forth: shenzhen,
 name: eric,
 number: 201521234,
 second: shanghai,
 university: CUP}

 

python手记(7)------字典(操作方法)