home PYTHONJAVA
 

Python3 set

You can create a collection using braces { } or set() functions. Note: Creating an empty collection must use set() instead of { } because { } is used to create An empty dictionary.

Create format:

parame = {value01,value02,...}
or 
set(value)

Instance (Python 3.0+)

>>>basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>> print( Basket) # The demo here is to go Heavy features {'orange' , 'banana', ' Pear', ' apple'} >>> 'orange ' in basket # Quickly determine if an element is inside a collection True >>> 'crabgrass ' in basket False >>> # The following shows the operations between two collections. ... >>> a = Set('abracadabra ') >>> b = Set('alacazam ') >>> a {'a' , 'r', ' b', ' c', 'd' } >>> a - b # Elements in collection a that are not included in collection b {'r' , 'd', ' b'} >>> a | b # All elements contained in the collection a or b {'a' , 'c', ' r', ' d', 'b' , 'm' , 'z', ' l'} >>> a & b # Elements in both collections a and b {'a' , 'c'} >>> a ^ b # Elements that are not included in a and b at the same time {'r' , 'd', ' b', ' m', 'z' , 'l' }

Similar to list comprehensions, the same set supports set comprehension:

Instance (Python 3.0+)

>>>a = {x For x in 'abracadabra' if x not in 'abc'} >>> a {'r' , 'd'}

Basic operation of the collection

1, add elements

The syntax is as follows:

s.add( x )

Add the element x to the collection s and take no action if the element already exists.

Instance (Python 3.0+)

>>>thisset = set( ("Google" , "welookups", " javacodegeeks")) >>> thisset. Add("Facebook ") >>> print( Thisset) {'javacodegeeks' , 'Facebook', ' Google', ' welookups'}

There is also a method, you can also add elements, and the parameters can be lists, tuples, dictionaries, etc. The syntax is as follows:

s.update( x )

x can have multiple, separated by commas.

Instance (Python 3.0+)

>>>thisset = set( ("Google" , "welookups", " javacodegeeks")) >>> thisset. Update({1 ,3}) >>> print( Thisset) {1, 3 , 'Google', ' javacodegeeks', ' welookups'} >>> thisset. Update([1 ,4],[5 ,6]) >>> print( Thisset) {1, 3 , 4, 5, 6 , 'Google' , 'javacodegeeks', ' welookups'} >>>

2, remove elements

The syntax is as follows:

s.remove( x )

Removes the element x from the collection s and an error occurs if the element does not exist.

Instance (Python 3.0+)

>>>thisset = set( ("Google" , "welookups", " javacodegeeks")) >>> thisset. Remove("javacodegeeks ") >>> print( Thisset) {'Google' , 'welookups'} >>> thisset. Remove("Facebook ") #No error will occur Traceback (most recent call last ): File "<stdin>", line 1 , in <module> KeyError: 'Facebook' >>>

Another method is also to remove the elements in the collection, and if the element does not exist, no error will occur. The format is as follows:

s.discard( x )

Instance (Python 3.0+)

>>>thisset = set( ("Google" , "welookups", " javacodegeeks")) >>> thisset. Discard("Facebook ") # does not exist and no error will occur >>> print( Thisset) {'javacodegeeks' , 'Google', ' welookups'}

We can also set to randomly delete an element in the collection. The syntax is as follows:

s.pop() 

Script mode instance (Python 3.0+)

thisset = Set((" Google", "welookups" , "javacodegeeks" , "Facebook")) x= thisset.pop() print(x)

Output results:

$ python3 test.py
welookups

The results of multiple execution tests are different.

However, in interactive mode, pop is the first element of the collection (the first element of the sorted collection).

Instance (Python 3.0+)

>>>thisset = set( ("Google" , "welookups", " javacodegeeks", " Facebook")) >>> thisset. Pop() 'Facebook' >>> print( Thisset) {'Google' , 'javacodegeeks', ' welookups'} >>>

3, calculate the number of collection elements

The syntax is as follows:

len(s)

Calculating the number of s elements in the collection.

Instance (Python 3.0+)

>>>thisset = set( ("Google" , "welookups", " javacodegeeks")) >>> len( Thisset) 3

4, empty collection

The syntax is as follows:

s.clear()

Clear the collection s.

Instance (Python 3.0+)

>>>thisset = set( ("Google" , "welookups", " javacodegeeks")) >>> thisset. Clear() >>> print( Thisset) set()< /div>

4, determine if the element exists in the collection

The syntax is as follows:

x in s

Determine whether element x is in the set s, there is a return of True, and there is no return of False.

Instance (Python 3.0+)

>>>thisset = set( ("Google" , "welookups", " javacodegeeks")) >>> "welookups " in thisset True >>> "Facebook " in thisset False >>>

Complete list of collection built-in methods

< /tr> symmetric_difference()
method Description
add()Adding elements to the collection
clear()Remove all elements in the collection
copy()Copy a collection
difference()Return the difference set of multiple collections
difference_update()Remove the elements in the collection, which are also in the specified collection presence.
discard()Delete the specified element in the collection
intersection()Return the intersection of the collections
intersection_update() Removes an element from the collection that does not exist in the specified collection.
isdisjoint()Determine whether two collections contain the same element, if not return True , otherwise return False.
issubset() Determines if the specified collection is a subset of the method's parameter set.
issuperset()Determine whether the parameter set of the method is a specified set Subset
pop()Removing elements
remove()Remove the specified element
returns a collection of elements that are not duplicated in the two collections.
symmetric_difference_update() Removes the same element in the current collection from another specified collection, and inserts a different element from the other specified collection into the current collection.
union()Returns the union of two collections
update()Adding elements to the collection





welookups is optimized for learning.© welookups. 2018 - All Right Reserved and you agree to have read and accepted our term and condition.