Python(九)字典和集合

字典DICT

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

构造函数:

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

举个例子:

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

字典=赋值为引用赋值,举个例子:

 

>>> 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()//全拷贝,dict2的值在新的内存区域

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

 

集合

    可变集合 set 特点:不可以重复,顺序重排

    不可变集合 frozenset 特点:可以重复 ,顺序不重排

猜你喜欢

转载自awenhaowenchao.iteye.com/blog/2409014