Type python - collection

A collection of objects is a set of hash values ​​disordered array, a set of key members do dictionary. Collection of two types: a variable set, can add and delete elements, can not set the variable hash, the dictionary can not be used as an element of the other keys are not set; immutable set contrast, the hash value, or it may be used as a dictionary key member of the collection.

1. Create a collection of collections and to assign

The only way to create a collection - a collection of plants with the method set () and frozenset ()

>>> s = set('cheeseshop')

>>> s

set(['c', 'e', 'h', 'o', 'p', 's'])

>>> t = frozenset('bookshop')

>>> t

frozenset(['b', 'h', 'k', 'o', 'p', 's'])

>>> type(s)

<type 'set'>

>>> len(s)

6

>>> s (t) == len (s)

True

>>> s == t

False

2. The value in the collection

You can view the collection traverse checking whether a member or element is a collection of members.

>>> s

set(['c', 'e', 'h', 'o', 'p', 's'])

>>> 'k' in s

False

>>> for i in s:

... print i,

...

c e h o p s

3. Update Collection

Add and remove members of the collection with a variety of built-in set of methods and operators.

>>> s

set(['c', 'e', 'h', 'o', 'p', 's'])

>>> s.add('z')

>>> s

set(['c', 'e', 'h', 'o', 'p', 's', 'z'])

>>> s.update('pypi')

>>> s

set(['c', 'e', 'i', 'h', 'o', 'p', 's', 'y', 'z'])

>>> s.remove('z')

>>> s

set(['c', 'e', 'i', 'h', 'o', 'p', 's', 'y'])

>>> s -= set('pypi')

>>> s

set(['c', 'e', 'h', 'o', 's'])

4. Delete collection

Make the collection beyond its scope, or call them directly del clears the current namespace.

>>> del s

The collection type operator

5.1. Standard type operators (set of all types)

Membership (in, not in): whether an element is a set of member;

Set equivalent / not equivalent: refers to two sets are equal for each set, if and only if the members of each member of a set is also another set wherein each set must also be said to be a subset of another set, i.e. s <= t and t <= s, are true value (true), the set of equivalents / or order-independent member of the set type and the set of non-equivalent, only with the elements of the set .

>>> s

set(['c', 'e', 'h', 'o', 'p', 's'])

>>> t

frozenset(['b', 'h', 'k', 'o', 'p', 's'])

>>> s == t

False

>>> set('posh') == set('shop')

True

Subset / superset: Set python inspection of whether a set of comparison operators other superset or subset of the set. "Less than" sign (<, <=) is used to determine a subset of "greater than" sign (>,> =) is used to determine a superset.

>>> set('shop') < set('cheeseshop')

True

>>> set('bookshop') >= set('shop')

True

5.2. Collection type operator (the set of all types)

Joint (|): Combined (Union) and the OR operation is equivalent to a collection, is a union of two sets of new collection that in each element is at least one member of the set. The method has a combined equivalent, union ()

>>> s

set(['c', 'e', 'h', 'o', 'p', 's'])

>>> t

frozenset(['b', 'h', 'k', 'o', 'p', 's'])

>>> s | t

set(['c', 'b', 'e', 'h', 'k', 'o', 'p', 's'])

>>>

Intersection (&): AND than as set intersection operation (or AND) operation, the intersection of two sets is a new collection that each element is simultaneously set two members, i.e., two sets belonging to a member of. Intersection symbol has an equivalent method, intersection ().

>>> s

set(['c', 'e', 'h', 'o', 'p', 's'])

>>> t

frozenset(['b', 'h', 'k', 'o', 'p', 's'])

>>> s & t

set(['h', 's', 'o', 'p'])

Interpolating / relative complement (-): two sets (s and t) is the relative difference complement or complement refers to a collection of C, the elements of the collection, the collection belongs s, not belonging to the set t. There is a difference between the symbols equivalent method, difference ()

set(['c', 'e', 'h', 'o', 'p', 's'])

>>> t

frozenset(['b', 'h', 'k', 'o', 'p', 's'])

>>> s - t

set(['c', 'e'])

Symmetric difference (^): The symmetric difference is set to XOR (also known as "XOR"), two sets (s and t) refers to the differential symmetry C of another set, the elements of the set, only belonging to the set s or member of the set of t, can not belong to a set of two, symmetric difference has an equivalent method, the symmetric_difference ()

>>> s

set(['c', 'e', 'h', 'o', 'p', 's'])

>>> t

frozenset(['b', 'h', 'k', 'o', 'p', 's'])

>>> s ^ t

set(['b', 'e', 'k', 'c'])

Mixing collection type: If the two operands of the same type, i.e., are variable or invariable collection set, the type of the result produced is the same, if the type of the operands are not the same as the left and right (left operand is SET, the right operand is frozenset, or vice versa), the type of the result produced by the same type of the left operand.

>>> s

set(['c', 'e', 'h', 'o', 'p', 's'])

>>> t

frozenset(['b', 'h', 'k', 'o', 'p', 's'])

>>> s | t

set(['c', 'b', 'e', 'h', 'k', 'o', 'p', 's'])

>>> t | s

frozenset(['c', 'b', 'e', 'h', 'k', 'o', 'p', 's'])

5.3. Collection type operator (for only the variable set)

(Union) Update (| =): Add (possibly multiple) members, and update equivalent from an existing collection of

>>> s = set('cheeseshop')

>>> u = frozenset(s)

>>> s |= set('pypi')

>>> s

set(['c', 'e', 'i', 'h', 'o', 'p', 's', 'y'])

Retention / Intersection Update (& =): reserved (or intersection update) operations to retain a membership of other collections. The method and intersection_update () are equivalent.

>>> s

set(['p', 'c', 'e', 'h', 's', 'o'])

>>> s &= set('shop')

>>> s

set(['h', 's', 'o', 'p'])

>>>

Difference Update (- =): of s and t be the set of difference update s - = t, the difference update returns a collection that is a member of the set of elements of the set of s t to remove the remaining elements. The method and difference_update () are equivalent.

>>> s

set(['p', 'c', 'e', 'h', 's', 'o'])

>>> s -= set('shop')

>>> s

set(['c', 'e'])

Symmetric Difference Update (^ =): of s and t are set symmetrical differential update operation (s ^ = t) returns a collection that only the members or other members of the original set of set s in t. The method and symmetric_difference_update () are equivalent.

>>> s

set(['p', 'c', 'e', 'h', 's', 'o'])

>>> t = frozenset('bookshop')

>>> s ^= t

>>> s

set(['c', 'b', 'e', 'k'])

6. Built-in Functions

() Len: (or number of elements) Returns a collection cardinality

6.1. Collection type factory function

set () and frozenset (): set () to generate a variable set, frozenset () to generate a set of immutable, the parameter is not available, generating empty set; parameters must be iterative, i.e., a sequence or iterator, or support iteration object, such as a file or a dictionary.

6.2. Collection type built-in methods

6.2.1. Method (the set of all methods)

Method name

operating

s.issubset(t)

If s is a subset of t Returns True, False otherwise

s.issuperset(t)

If t s is a superset of returns True, otherwise False

s.union(t)

Returns a new collection, which is set and s and t

s.intersection(t)

It returns a new collection, which is the intersection of s and t

s.difference(t)

返回一个新集合,该集合是s的成员,但不是t的成员

s.symmetric_difference

返回一个新集合,该集合是s或t的成员,但不是s和t共有的成员

s.copy()

返回一个新集合,该集合是s的浅复制

6.2.2.方法(仅适用于可变集合)

方法名称

操作

s.update(t)

用t中的元素修改s,即s现在包含

s.intersection_update()

s中的成员是共同属于s和t的元素

s.symmetric_difference_update(t)

s中的成员更新为那些包含在s或t中,但不是s和t共有的元素

s.add(obj)

在集合s中添加对象obj

s.remove(obj)

从集合s中删除对象obj,如果obj不是集合s中的元素(obj not in s),将引发KeyError错误。

s.discard(obj)

如果obj是集合s中的元素,从集合s中删除对象obj

s.pop()

删除集合s中的任意一个对象,并返回它

s.clear()

删除集合s中的所有元素

附录:集合类型总结表

函数/方法名

等价操作符

说明

所有集合类型

len(s)

 

集合基数:集合s中元素的个数

set([obj])

 

可变集合工厂函数:obj必须是支持迭代的,由obj中的元素创建集合,否则创建一个空集合

frozenset([obj])

 

不可变集合工厂函数:执行方式和set()相同,但它返回的是不可变集合

 

obj in s

成员测试:obj是s中的一个元素吗?

 

obj not in s

非成员测试:obj不是s中的一个元素吗?

 

s == t

等价测试,测试s和t是否具有相同的元素?

 

s != t

不等价测试,与==相反

 

s < t

(严格意义上)子集测试:s!=t且s中所有的元素都是t的成员

s.issubset(t)

s <= t

子集测试(允许不严格意义上的子集):s中所有的元素都是t的成员

 

s > t

(严格意义上)超集测试:s!=t且t中所有的元素都是s的成员

s.issuperset(t)

s >= t

超集测试(允许不严格意义上的超集):t中所有的元素都是s的成员

s.union(t)

s | t

合并集合:s或t中的元素

s.intersection(t)

s & t

交集操作:s和t中的元素

s.difference(t)

s – t

差分操作:s中的元素,而不是t中的元素

s.symmetric_difference

s ^ t

对称差分操作:s或t中的元素,但不是s和t共有的元素

s.copy()

 

复制操作:返回s的浅复制副本

仅用于可变集合

s.update(t)

s |= t

(Union)修改操作:将t中的成员添加到s

s.intersection_update(t)

s &= t

交集修改操作:s中仅包括s和t中共有的成员

s.difference_update(t)

s -= t

差修改操作:s中包括仅属于s但不属于t中的成员

s.symmetric_difference_update(t)

s ^= t

对称差分修改操作:s中包括仅属于s或仅属于t中的成员

s.add(obj)

 

加操作:将obj添加到s

s.remove(obj)

 

删除操作:将obj从s中删除,如果s中不存在obj,将引发KeyError

s.discard(obj)

 

丢弃操作:remove()的友好版本,如果s中存在obj,从s中删除它

s.pop()

 

pop操作:移除并返回s中的任意一个元素

s.clear()

 

消除操作:移除s中的所有元素

Guess you like

Origin www.cnblogs.com/mrlayfolk/p/11980278.html