Python (nine) dictionaries and sets

Dictionary DICT

dict1 = {"key1":"value1", "key2":"value2" }

Constructor:

 |  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)

for example:

-------------------------------------------------------------------

Dictionary = assignment is assignment by reference, for example:

 

>>> dict1={}

>>> dict1

{}

>>> dict2

{0: 'hello world', 1: 'hello world', 2: 'hello world', 3: 'hello world', 4: 'hello world', 5: 'hello world', 6: 'hello world', 7: 'hello world', 8: 'hello world', 9: 'hello world'}

>>> dict1.clear()

>>> dict2

{0: 'hello world', 1: 'hello world', 2: 'hello world', 3: 'hello world', 4: 'hello world', 5: 'hello world', 6: 'hello world', 7: 'hello world', 8: 'hello world', 9: 'hello world'}

>>> dict2.clear()

>>> dict2

{}

 

dict2 = dict1.copy()//Full copy, the value of dict2 is in the new memory area

-------------------------------------------------------------------

 

gather

    Mutable collection set features: can not be repeated, the order is rearranged

    Immutable collection frozenset features: can be repeated, the order is not rearranged

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326336669&siteId=291194637