Wait for bloom
时光不语,静待花开

字典详解                                                    

1.clear

def clear(self): # real signature unknown; restored from __doc__
""" D.clear() -> None. Remove all items from D. """
pass
翻译:删除字典元素
1 #!/usr/bin/python
2 test={'name':'test','age':22}
3 test.clear()
4 print(test)
View Code

2.copy

def copy(self): # real signature unknown; restored from __doc__
""" D.copy() -> a shallow copy of D """
pass
翻译:复制一份影子副本(就是第一层复制,第二层只是复制一个对象地址)
 1 #!/usr/bin/python
 2 test={'name':'test','age':22}
 3 x=test.copy()
 4 test['name']='小李'
 5 print(test)
 6 print(x)
 7 test['name']={'sales':['小张','小李']}
 8 print(test)
 9 x=test.copy()
10 test['name']['sales']=['小张','小李','小王']
11 print(test)
12 print(x)
View Code

3.fromkeys

def fromkeys(*args, **kwargs): # real signature unknown
""" Create a new dictionary with keys from iterable and values set to value. """
pass
翻译:用可迭代对象创建一个新的字典
1 #!/usr/bin/python
2 test={'name':'test','age':22}
3 x=test.fromkeys([1,2,3],['a','b','c'])
4 print(x)
View Code

4.get

def get(self, *args, **kwargs): # real signature unknown
""" Return the value for key if key is in the dictionary, else default. """
pass
翻译:如果key在字典里面则返回key的值,否则默认
1 #!/usr/bin/python
2 test={'name':'test','age':22}
3 x=test.get('name')
4 print(x)
View Code

5.items

def items(self): # real signature unknown; restored from __doc__
""" D.items() -> a set-like object providing a view on D's items """
pass翻译:提供一个类似集合的对象,提供关于字典的项的视图
1 #!/usr/bin/python
2 test={'name':'test','age':22}
3 print(test.items())
View Code

6.key

def keys(self): # real signature unknown; restored from __doc__
""" D.keys() -> a set-like object providing a view on D's keys """
pass
翻译:提供一个类似集合的对象,提供关于字典的KEY的视图
1 #!/usr/bin/python
2 test={'name':'test','age':22}
3 print(test.keys())
View Code

7.values

def values(self): # real signature unknown; restored from __doc__
""" D.values() -> an object providing a view on D's values """
pass
翻译:提供一个类似集合的对象,提供关于字典的值的视图
1 #!/usr/bin/python
2 test={'name':'test','age':22}
3 print(test.values())
View Code

8.pop

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, default is returned if given, otherwise KeyError is raised
"""
pass
翻译:删除key及对应的值,如果key没有找到,则报KeyError
1 #!/usr/bin/python
2 test={'name':'test','age':22}
3 test.pop('name')
4 print(test)
View Code

9.popitem

def popitem(self, *args, **kwargs): # real signature unknown
"""
Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order.
Raises KeyError if the dict is empty.
"""
pass
翻译:删除并返回一个键值对作为二元组
对按后进先出的顺序,如果字典为空则返回KeyError
1 #!/usr/bin/python
2 test={'name':'test','age':22}
3 test.popitem()
4 print(test)
View Code

10.setdefault

def setdefault(self, *args, **kwargs): # real signature unknown
"""
Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.
"""
pass
翻译:如果key不在字典里面,则插入一个key并且带默认值,如果key在字典里面则返回原来值,其他默认
1 #!/usr/bin/python
2 test={'name':'test','age':22}
3 test.setdefault(1)
4 print(test)
5 test.setdefault(1,'default')
6 print(test)
View Code

11.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
翻译:执行更新操作,从字典对象E和F更新D
如果E存在,并且是.key()方法,则循环更新
如果E存在,并且缺少.key()方法,则插入
其他情况,直接替换
1 #!/usr/bin/python
2 test={'name':'test','age':22}
3 test.update({'name':'xiaozhang'})
4 print(test)
5 test.update({'name':'xiaozhang','age':32,'sex':''})
6 print(test)
View Code

 

posted on 2024-01-19 19:19  Little-Girl  阅读(25)  评论(0)    收藏  举报