Python3 collection

collection is an unordered sequence of non-repeating elements.

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

Create format:

parame = {value01,value02< /span>,...}

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 = collection('abracadabra') >>> b = collection('alacazam') >>> a {'a', 'r', 'b', 'c', 'd'} >>> a - b # All elements contained in the collection a or b {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'} >>> a & b # Elements that are included 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'}

Like a list comprehension, the same collection supports collection comprehensions(collection 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+)

>>>thiscollection = collection(("Google", "Welookups", "Javacodegeeks")) >>> thiscollection.add("Facebook") >>> print(thiscollection) {'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+)

>>>thiscollection = collection(("Google", "Welookups", "Javacodegeeks")) >>> thiscollection.update({1,3}) >>> print(thiscollection) {1, 3, 'Google', 'Javacodegeeks', 'Welookups'} >>> thiscollection.update([1,4],[5,6]) >>> print(thiscollection) {1, 3, 4, 5, 6, 'Google', 'Javacodegeeks', 'Welookups'} >>>

Instance(Python 3.0+)

>>>thiscollection = collection(("Google", "Welookups", "Javacodegeeks")) >>> thiscollection.remove("Javacodegeeks") >>> print(thiscollection) {'Google', 'Welookups'} >>> thiscollection.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+)

>>>thiscollection = collection( ("Google" , "Welookups", " Javacodegeeks")) >>> thiscollection. Discard("Facebook ") # does not exist and no error will occur >>> print( Thiscollection) {'Javacodegeeks' , 'Google', ' Welookups'}

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


Script mode instance(Python 3.0+)

thiscollection = collection(("Google", "Welookups", "Javacodegeeks", "Facebook")) x = thiscollection.pop() print(x)

Output result:

$ python3 test.py

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+)

>>>thiscollection = collection(("Google", "Welookups", "Javacodegeeks", "Facebook")) >>> thiscollection.pop() 'Facebook' >>> print(thiscollection) {'Google', 'Javacodegeeks', 'Welookups'} >>>

3、Calculate the number of collection elements

The syntax is as follows:


Calculation collection s Number of elements。

Instance(Python 3.0+)

>>>thiscollection = collection(("Google", "Welookups", "Javacodegeeks")) >>> len(thiscollection) 3

4、Empty collection

The syntax is as follows:


Empty collection s。

Instance(Python 3.0+)

>>>thiscollection = collection(("Google", "Welookups", "Javacodegeeks")) >>> thiscollection.clear() >>> print(thiscollection) collection()

4, determine if the element exists in the collection

The syntax is as follows:

x in s

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

Instance (Python 3.0+)

>>>thiscollection = collection( ("Google" , "Welookups", " Javacodegeeks")) >>> "Welookups " in thiscollection True >>> "Facebook " in thiscollection False >>>

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