home PYTHONJAVA
 

Python3 collection

Set is an unordered sequence of non-repeating elements.

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

Create format:

parame = {value01,value02< /span>,...}
or 
set(value)

Instance(Python 3.0+)

>>>basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>> print(basket) # Demonstrated here is the deduplication function {'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 # All elements contained in the set a or b {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'} >>> a & b # Elements that are included in both sets 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'}

Like a list comprehension, the same set supports set comprehensions(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", "Runoob", "Taobao")) >>> thisset.add("Facebook") >>> print(thisset) {'Taobao', 'Facebook', 'Google', 'Runoob'}

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", "Runoob", "Taobao")) >>> thisset.update({1,3}) >>> print(thisset) {1, 3, 'Google', 'Taobao', 'Runoob'} >>> thisset.update([1,4],[5,6]) >>> print(thisset) {1, 3, 4, 5, 6, 'Google', 'Taobao', 'Runoob'} >>>

Instance(Python 3.0+)

>>>thisset = set(("Google", "Runoob", "Taobao")) >>> thisset.remove("Taobao") >>> print(thisset) {'Google', 'Runoob'} >>> thisset.remove("Facebook") # 不存在会发生错误 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" , "Runoob", " Taobao")) >>> thisset. Discard("Facebook ") # does not exist and no error will occur >>> print( Thisset) {'Taobao' , 'Google', ' Runoob'}

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", "Runoob", "Taobao", "Facebook")) x = thisset.pop() print(x)

Output result:

$ python3 test.py
Runoob

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", "Runoob", "Taobao", "Facebook")) >>> thisset.pop() 'Facebook' >>> print(thisset) {'Google', 'Taobao', 'Runoob'} >>>

3、Calculate the number of collection elements

The syntax is as follows:

len(s)

Calculation set s Number of elements。

Instance(Python 3.0+)

>>>thisset = set(("Google", "Runoob", "Taobao")) >>> len(thisset) 3

4、Empty collection

The syntax is as follows:

s.clear()

Empty collection s。

Instance(Python 3.0+)

>>>thisset = set(("Google", "Runoob", "Taobao")) >>> thisset.clear() >>> print(thisset) set()

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" , "Runoob", " Taobao")) >>> "Runoob " in thisset True >>> "Facebook " in thisset False >>>





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