python字典使用

>>> dic = dict((("A",[1,2,3]),("F",56)))
>>> dic
{'A': [1, 2, 3], 'F': 56}
>>> dic = {"R":"RRRR","T":"TTTT"}
>>> dic
{'R': 'RRRR', 'T': 'TTTT'}
>>> dic["R"]
'RRRR'
>>> 
>>> dic = dict((("A",[1,2,3]),("F",56)))
>>> dic["A"]
[1, 2, 3]
>>> dic["A"][0]
1
>>> dic["E"] = "RYFHF"
>>> dic
{'A': [1, 2, 3], 'F': 56, 'E': 'RYFHF'}
>>> dict1 = {}
>>> dict1.formkeys((1,2,3),"number")
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    dict1.formkeys((1,2,3),"number")
AttributeError: 'dict' object has no attribute 'formkeys'
>>> dict1 = dict1.formkeys((1,2,3),"number")
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    dict1 = dict1.formkeys((1,2,3),"number")
AttributeError: 'dict' object has no attribute 'formkeys'
>>> dict1 = {"A":"a","B":"b"}
>>> d = dict1.keys
>>> d
<built-in method keys of dict object at 0x1057d1cf0>
>>> for i in d :
    print(i)

    
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    for i in d :
TypeError: 'builtin_function_or_method' object is not iterable
>>> dict1.get("A")
'a'
>>> dict1.set("C","c")
Traceback (most recent call last):
  File "<pyshell#22>", line 1, in <module>
    dict1.set("C","c")
AttributeError: 'dict' object has no attribute 'set'
>>> dict1.get("C","c")
'c'
>>> dict1
{'A': 'a', 'B': 'b'}
>>> "A" in dict1
True
>>> dict1
{'A': 'a', 'B': 'b'}
>>> dict1.clear()
>>> dict1
{}
>>> a = {"x":"XXXX","e":"EEEEEE"}
>>> b= a
>>> a = {}
>>> a
{}
>>> b
{'x': 'XXXX', 'e': 'EEEEEE'}
>>> b["t"] = "TTTT"
>>> b
{'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT'}
>>> for i in b.keys
SyntaxError: invalid syntax
>>> or i in b.keys:
    
SyntaxError: invalid syntax
>>> for i in b.keys:
    print(i)

    
Traceback (most recent call last):
  File "<pyshell#41>", line 1, in <module>
    for i in b.keys:
TypeError: 'builtin_function_or_method' object is not iterable
>>> for i in b.items():
    print(i)

    
('x', 'XXXX')
('e', 'EEEEEE')
('t', 'TTTT')
>>> for i in b.keys():
    print(i)

    
x
e
t
>>> for i in b.values():
    print(i)

    
XXXX
EEEEEE
TTTT
>>> for i in b.items():
    if i["x"] == "XXXX":
        print(i)

        
Traceback (most recent call last):
  File "<pyshell#54>", line 2, in <module>
    if i["x"] == "XXXX":
TypeError: tuple indices must be integers or slices, not str
>>> for i in b.items():
    if i["x"] == "XXXX":
        print("i")

        
Traceback (most recent call last):
  File "<pyshell#56>", line 2, in <module>
    if i["x"] == "XXXX":
TypeError: tuple indices must be integers or slices, not str
>>> b
{'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT'}
>>> b.get("x")
'XXXX'
>>> b.get("d","DDDD")
'DDDD'
>>> b
{'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT'}
>>> b.set("d","DDDD")
Traceback (most recent call last):
  File "<pyshell#61>", line 1, in <module>
    b.set("d","DDDD")
AttributeError: 'dict' object has no attribute 'set'
>>> help(b)
Help on dict object:

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)
 |  
 |  Methods defined here:
 |  
 |  __contains__(self, key, /)
 |      True if D has a key k, else False.
 |  
 |  __delitem__(self, key, /)
 |      Delete self[key].
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __len__(self, /)
 |      Return len(self).
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __setitem__(self, key, value, /)
 |      Set self[key] to value.
 |  
 |  __sizeof__(...)
 |      D.__sizeof__() -> size of D in memory, in bytes
 |  
 |  clear(...)
 |      D.clear() -> None.  Remove all items from D.
 |  
 |  copy(...)
 |      D.copy() -> a shallow copy of D
 |  
 |  fromkeys(iterable, value=None, /) from builtins.type
 |      Returns a new dict with keys from iterable and values equal to value.
 |  
 |  get(...)
 |      D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.
 |  
 |  items(...)
 |      D.items() -> a set-like object providing a view on D's items
 |  
 |  keys(...)
 |      D.keys() -> a set-like object providing a view on D's keys
 |  
 |  pop(...)
 |      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
 |  
 |  popitem(...)
 |      D.popitem() -> (k, v), remove and return some (key, value) pair as a
 |      2-tuple; but raise KeyError if D is empty.
 |  
 |  setdefault(...)
 |      D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
 |  
 |  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]
 |  
 |  values(...)
 |      D.values() -> an object providing a view on D's values
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __hash__ = None

>>> b
{'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT'}
>>> setitem(b,"d","DDD")
Traceback (most recent call last):
  File "<pyshell#64>", line 1, in <module>
    setitem(b,"d","DDD")
NameError: name 'setitem' is not defined
>>> for i in b.keys():
    if i in b:
        print(i)
        break

    
x
>>> for i in b.keys():
    if i in b:
        print(i)

        
x
e
t
>>> b["y"] = "RRRRR"
>>> b
{'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT', 'y': 'RRRRR'}
>>> a = b.copy()
>>> a
{'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT', 'y': 'RRRRR'}
>>> b.clear()
>>> b
{}
>>> a
{'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT', 'y': 'RRRRR'}
>>> id(a)
4387105744
>>> id(b)
4386400656
>>> a.popitem()
('y', 'RRRRR')
>>> a
{'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT'}
>>> a.setdifault("b","BBBB")
Traceback (most recent call last):
  File "<pyshell#83>", line 1, in <module>
    a.setdifault("b","BBBB")
AttributeError: 'dict' object has no attribute 'setdifault'
>>> a
{'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT'}
>>> a.setdefault("v","VVVV")
'VVVV'
>>> a
{'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT', 'v': 'VVVV'}
>>> a.get("e")
'EEEEEE'
>>> a
{'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT', 'v': 'VVVV'}
>>> a.setdefault("n","NNNNN")
'NNNNN'
>>> a
{'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT', 'v': 'VVVV', 'n': 'NNNNN'}
>>> a.updata("g","GGGG")
Traceback (most recent call last):
  File "<pyshell#91>", line 1, in <module>
    a.updata("g","GGGG")
AttributeError: 'dict' object has no attribute 'updata'
>>> a
{'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT', 'v': 'VVVV', 'n': 'NNNNN'}
>>> a.update("e","update")
Traceback (most recent call last):
  File "<pyshell#93>", line 1, in <module>
    a.update("e","update")
TypeError: update expected at most 1 arguments, got 2
>>> a
{'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT', 'v': 'VVVV', 'n': 'NNNNN'}
a
>>> a.update({"e","update"})
Traceback (most recent call last):
  File "<pyshell#95>", line 1, in <module>
    a.update({"e","update"})
ValueError: dictionary update sequence element #0 has length 1; 2 is required
>>> a.update({"e":"update"})
>>> a
{'x': 'XXXX', 'e': 'update', 't': 'TTTT', 'v': 'VVVV', 'n': 'NNNNN'}
>>> a.update("e":"update2")
SyntaxError: invalid syntax
>>> a.update({"e":"update2"})
>>> a
{'x': 'XXXX', 'e': 'update2', 't': 'TTTT', 'v': 'VVVV', 'n': 'NNNNN'}
>>> a.setdefault("e","rrrr")
'update2'
>>> a
{'x': 'XXXX', 'e': 'update2', 't': 'TTTT', 'v': 'VVVV', 'n': 'NNNNN'}
>>> a.setdefault("e1","XXXX")
'XXXX'
>>> a
{'x': 'XXXX', 'e': 'update2', 't': 'TTTT', 'v': 'VVVV', 'n': 'NNNNN', 'e1': 'XXXX'}
>>> 

 

posted @ 2017-03-18 20:23  张凯泽  阅读(869)  评论(0编辑  收藏  举报