Python__Combined data types

Combined data: collection type, sequence type (tuple type, list type), dictionary type

gather:

      Definition: A set is an unordered combination of elements. (Unordered, unique, data types that are required to be put into a collection in Python are immutable).

              Sets are represented by curly braces {}, and elements are separated by commas

              Create a collection with {} or set()

      Set operators: S | T (union) S - T (difference) S & T (intersection) S ^ T (complement)  

                           Judging subset relationship: S<=T (or S<T) S>=T (or S >T) returns True / False

                         Augmentation operator: S | = T ==> S = S|T  

      Collection processing methods: For collection S, the methods we can use are:

                               S.add(x) if x is not in S, add x to S

                               S.discard(x) removes the element x in S, if x is not in the set S, no error is reported

                               S.remove(x) removes the element x in S. If x is not in the set S, a KeyError exception is generated (you can use the try-except method to catch the exception)

                               S.clear() removes all elements in S

                               S.pop() randomly returns an element of S, if S is empty, a KeyError exception is generated

                               S.copy() returns a copy of the set S

                               len(S) returns the number of elements in the set S

                                x in S judges whether S contains element x, x is in the set, returns True, otherwise returns False

                               x not in S  

                               set(x) converts a variable x of another type to a set type

      Collection Application Scenario: Inclusion Relationship Comparison Data Deduplication

sequence:

       Sequence Type Definition: A sequence is a set of elements with a precedence relationship.

                                 A sequence is a one-dimensional element vector, and the element types can be different

                                 Similar sequence of mathematical elements: S0,S1,...,Sn-1

                                 Elements are guided by ordinal numbers, and specific elements of the sequence are accessed through subscripts.

                    Sequence is a base class type, we generally do not use sequence directly, but use its derivatives: string type, tuple type, list type. All the operations of the sequence are tried in the string type, tuple type, and list type. At the same time, strings, tuples, and lists have their own unique operation capabilities.

                    Definition of serial number: serial number has positive increment and reverse decrement

       Sequence processing functions and methods:

                6 operators: x in s: Returns True if x is an element of the sequence s, otherwise returns False

                                     x not in s: 

                                     s + t : concatenate two sequences s and t

                                     s * n ( n * s) : the sequence s is replicated n times

                                     s[i] : index returns the ith element in s, where i is the sequence number.

                                     s[i:j:k] slice, return the subsequence of i to j with k steps in the sequence (the s[::-1] sequence is reversed)

             5 functions and methods:

                                     len(s) returns the length of the sequence s

                                      min(s) returns the smallest element of the sequence s, the elements in s need to be comparable, if the elements are not comparable, an error will be reported

                                       max(s)

                                       s.index(x) / s.index(x,i,j) returns the position of the first occurrence of element x in sequence s from i to j

                                       s.count(x) returns the total number of occurrences of x in the sequence s

       Tuple types and operations:

                      Tuple Definition: A tuple is an extension of the sequence type.

                                A tuple is a sequence type that cannot be modified once created.

                               Created using parentheses() or tuple(), separated by commas

                               Parentheses may or may not be used. def func(): return 1,2 #The return value here is a tuple.

                       Operations on tuples: All common operations on tuples inherit from sequence types

                                 Tuples inherit all common operations for sequence types

                                 Tuples cannot be modified after they are created, so there is no special operation

                                 with or without parentheses

       List types and operations:

                    List type definition: List is an extension of sequence and is very commonly used

                                 A list is a sequence type that can be modified at will after creation

                                  Created using square brackets [] or list(), elements are separated by commas

                                  parentheses may or may not be used

                     List type manipulation functions and methods:

                           function:

                                  ls[i] = x replaces the i-th element of the list ls with x

                                 ls[i:j:k] = lt replaces the sublist of elements corresponding to the ls slice with the list lt

                                 del ls[i] delete the i-th element in the list ls

                                 del ls[i:j:k] deletes the elements i to j in the list ls with step k

                                  ls + =lt with the new list ls, the list lt elements are added to the list ls

                                   ls * = n update list ls whose elements are repeated n times

                            method:

                                   ls.append(x) adds an element x to the end of the list

                                   ls.clear() removes all elements in the list ls

                                   ls.copy() generates a new list and assigns all elements in ls

                                   ls.insert(i,x) adds element x at the i-th position of the list ls

                                   ls.pop(i) removes the i-th element in the list ls and deletes the element

                                    ls.remove(x) removes the first element x that appears in the list ls

                                    ls.reverse() reverses the elements in a list

dictionary:

       A map is a correspondence between keys (indexes) and values ​​(data)

       The dictionary type is the abstraction of the mapping. The dictionary is a collection of key-value pairs, and the key-value pairs are unordered

       Use curly braces or dict() to create, key-value pairs are represented by colons

       Create an empty dictionary use curly braces directly

Dictionary type manipulation functions and methods

                   del d[k] delete the data value corresponding to key k in dictionary d

                   k in d judge whether k is in d 

                   d.keys() returns all key information in dictionary d

                  d.values() returns all value information

                  d.items() returns all key-value pair information

                  d.get(k,<default>) If the key value k exists, return the corresponding value, if it does not exist, return the value of <default>

                 d.pop(k,<default>) If the key k exists, take out the corresponding value, if it does not exist, return the value of <default>

                 d.popitem() randomly re-selects a key-value pair from dictionary d and returns it in the form of a tuple

                  d.clear() deletes all key-value pairs

                  len(d) returns the number of elements in dictionary d

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325100824&siteId=291194637