[Python study notes] collections

definition

Collection (set) is disordered, unique data type, the list can be removed or repeated elements tuple

Collection (set) which can put a variable data type

Collection (set) inside remove (), and discard () are deleted, but if there is no element which would remove an error, but did not discard the elements without error

Braces {} may be used or a set () function creates a set of note: Create an empty set must be set () instead of {}, {} as it is used to create an empty dictionary

In [13]: s={'ecs','evs','vbs','evs','ces'}
In [14]: print s
set(['vbs', 'ecs', 'evs', 'ces'])

In [19]: s = set(('google','taobao','google'))
In [20]: print s
set(['google', 'taobao'])

In [15]: s = set('google')
In [16]: print s
set(['e', 'o', 'g', 'l'])

 

The relationship between the set of test operation

Intersection: list_1.intersection (list_2)
and set: list_1.union (list_2)
difference sets: list_1.difference (list_2)
         list_2.difference (list_1)
peer differential list_1.symmetric_difference (list_2)
subset list_1.issubset (list_2)
Parent set list_1.issuperset (list_2)
whether the intersection list_1.isdisjoint (list_2)

 

Intersection: list_1 & list_2
union: list_1 | list_2
set difference: list_1 - list_2
         list_2 - list_1
peer difference: list_1 ^ list_2

Add collection

s.add (x)
will be added to the collection element x s, if existing elements, nothing is done

In [23]: s={'google','taobao','tencent'}

In [24]: s.add('facebook')

In [25]: print s
set(['facebook', 'taobao', 'google', 'tencent'])

 


s.update ([1,3,4])
to add a number in the collection, with the argument should be an iterative type can be lists, tuples, dictionaries, etc.

Delete collection

s.remove (1)
Delete the collection specified element
s.pop ()
random delete an element in the collection, and returns the removed element

Other operations set and

len (s)
display set of set length
"1" in s
detected whether an element is a member of the set of s, returns a Boolean value

s.copy ()
shallow copy of a collection, not in-depth study here, goes on to say
s.clear ()
to clear all the elements of the collection

 

Guess you like

Origin www.cnblogs.com/vaon/p/10972780.html