YouTip LogoYouTip

Python3 Set

# Python3.x Python3 Sets A set is an unordered collection of unique elements. Elements in a set are not repeated, and common set operations such as intersection, union, and difference can be performed. You can create a set using curly braces `{ }`, with elements separated by commas `,`, or you can use the `set()` function to create a set. **Creation Format:** parame = {value01,value02,...} or set(value) Here is a simple example: set1 = {1, 2, 3, 4} # Create a set directly using curly braces set2 = set([4, 5, 6, 7]) # Create a set from a list using the set() function **Note:** To create an empty set, you must use `set()` instead of `{ }`, because `{ }` is used to create an empty dictionary. More examples: ## Example (Python 3.0+) >>> basket ={'apple','orange','apple','pear','orange','banana'} >>>print(basket)# This demonstrates the deduplication function {'orange','banana','pear','apple'} >>>'orange'in basket # Quickly check if an element is in the set True >>>'crabgrass'in basket False >>># The following demonstrates operations between two sets. ... >>> a =set('abracadabra') >>> b =set('alacazam') >>> a {'a','r','b','c','d'} >>> a - b # Elements in set a but not in set b {'r','d','b'} >>> a | b # All elements in set a or b {'a','c','r','d','b','m','z','l'} >>> a & b # Elements common to both set a and b {'a','c'} >>> a ^ b # Elements not in the intersection of a and b {'r','d','b','m','z','l'} Similar to list comprehensions, sets also support set comprehensions: ## Example (Python 3.0+) >>> a ={x for x in'abracadabra'if x not in'abc'} >>> a {'r','d'} * * * ## Basic Set Operations ### 1. Adding Elements **Syntax:** s.add( x ) Adds element x to set s. If the element already exists, no action is taken. ## Example (Python 3.0+) >>> thisset =set(("Google","","Taobao")) >>> thisset.add("Facebook") >>>print(thisset) {'Taobao','Facebook','Google',''} There is another method that can also add elements, and the parameter can be a list, tuple, dictionary, etc. The syntax is as follows: s.update( x ) x can have multiple values, separated by commas. ## Example (Python 3.0+) >>> thisset =set(("Google","","Taobao")) >>> thisset.update({1,3}) >>>print(thisset) {1,3,'Google','Taobao',''} >>> thisset.update([1,4],[5,6]) >>>print(thisset) {1,3,4,5,6,'Google','Taobao',''} >>> ### 2. Removing Elements **Syntax:** s.remove( x ) Removes element x from set s. If the element does not exist, an error will occur. ## Example (Python 3.0+) >>> thisset =set(("Google","","Taobao")) >>> thisset.remove("Taobao") >>>print(thisset) {'Google',''} >>> thisset.remove("Facebook")# An error will occur if it doesn't exist Traceback (most recent call last): File "", line 1,in KeyError: 'Facebook' >>> There is another method to remove elements from a set, which does not cause an error if the element does not exist. The format is as follows: s.discard( x ) ## Example (Python 3.0+) >>> thisset =set(("Google","","Taobao")) >>> thisset.discard("Facebook")# No error occurs if it doesn't exist >>>print(thisset) {'Taobao','Google',''} We can also randomly remove an element from the set. The syntax is as follows: s.pop() ## Script Mode Example (Python 3.0+)
← Met Win AtobPython Mongodb Delete Document β†’