python中如何清空字典
1、
>>> a = dict(zip(("a","b","c","d"),(111,222,333,444)))
>>> a
{'a': 111, 'b': 222, 'c': 333, 'd': 444}
>>> type(a)
<class 'dict'>
>>> a.clear()
>>> a
{}
2、
>>> a = dict(a = 111, b = 222, c = 333, d = 444)
>>> a
{'a': 111, 'b': 222, 'c': 333, 'd': 444}
>>> type(a)
<class 'dict'>
>>> a = {}
>>> a
{}
3、以上两种方法的区别
>>> a = dict(a = 111, b = 222)
>>> a
{'a': 111, 'b': 222}
>>> b = a
>>> b
{'a': 111, 'b': 222}
>>> a = {}
>>> a
{}
>>> b
{'a': 111, 'b': 222}
>>> a = dict(zip(("a","b"),(111,222)))
>>> a
{'a': 111, 'b': 222}
>>> b = a
>>> b
{'a': 111, 'b': 222}
>>> a.clear()
>>> a
{}
>>> b
{}
4、
>>> a = dict(zip(("a","b","c","d"),(111,222,333,444)))
>>> a
{'a': 111, 'b': 222, 'c': 333, 'd': 444}
>>> for i in range(len(a)):
a.popitem()
('d', 444)
('c', 333)
('b', 222)
('a', 111)
>>> a
{}