Python linear data structure

Time:2020-6-3

Python linear data structure

catalog
  • 1 linear data structure
  • 2. Built in common data types
    • 2.1 numerical type
    • 2.2 sequence
      • 2.2.1 list
      • 2.2.2 tuple
      • 2.2.3 string string
      • 2.2.4 bytes
      • 2.2.5 byte array

Code every article of Python

1 linear data structure

The linear structure to be introduced in this chapter: list, tuple, string, bytes, byte array.

  • Linear table: it is an abstract mathematical concept. It is an abstract sequence of a group of elements. It consists of finite elements (0 or any).

    Linear table can be divided into sequence table and link table.

  • Sequence table: an orderly storage of a set of elements in memory. A list is a typical sequence table.

  • Linked table: a group of elements are stored and linked in memory, and they know who is connected to each other.

    For these two types of tables, find, add, delete and modify the elements in the array to see the impact:

  • Find element

    For the sequential table, it is orderly to store data in memory, and it can quickly obtain elements through index number, which is highly efficient..

    For linked tables, which are stored in a decentralized way, elements can only be obtained by iterating one by one, with poor efficiency.

  • Add elements

    For a sequential table, if you add elements at the end, it has no effect on the whole data table. But if you insert elements at the beginning or in the middle, all the subsequent elements will be reordered, which has a great impact (think of millions or more of data).

    For linked tables, no matter where the element is added, it will not affect other elements, with little impact.

  • Delete element
    For sequential tables, removing elements has the same problem as adding elements.
    For linked tables, no matter where the element is deleted, it will not affect other elements, with little impact.

  • Modify element
    For the sequential table, it is efficient to get the elements quickly through index and then modify them.

    For linked tables, it is inefficient to get elements and then modify them.

Summary: the sequence table is the most efficient for finding and modifying, and the efficiency for adding and deleting is low. Linked tables are the opposite.

2. Built in common data types

2.1 numerical type

  • Int integer type

    explain: integers include negative integers, 0, positive integers (… – 2, – 1,0,1,2,…).

    x1 = 1
    x2 = 0
    x3 = -1
    print(type(x1), x1)
    print(type(x2), x2)
    print(type(x3), x3)
    
    #The output results are as follows:
     1
     0
     -1

    Int() method:You can convert a number or string to an integer. The default base = 10, which means decimal. If no parameter is passed in, 0 will be returned.

    x1 = int()
    x2 = int('1')
    X3 = int ('0b10 ', base = 2) × base = 2, table binary, consistent with the type of the passed in parameter.
    x4 = int(3.14)
    print(type(x1), x1)
    print(type(x2), x2)
    print(type(x3), x3)
    print(type(x4), x4)
    
    #The output results are as follows:
     0
     1
     2
     3
  • Float floating point type
    explain: it is composed of integer and decimal parts. The parameters passed in can beintstrbytesbytearray

    x1 = float(1)
    x2 = float('2')
    x3 = float(b'3')
    print(type(x1), x1)
    print(type(x2), x2)
    print(type(x3), x3)
    
    #The output results are as follows:
     1.0
     2.0
     3.0
  • Complex (complex type)

    explain: it is composed of real number and imaginary number, both of which are floating-point numbers.

    The incoming parameter can beintstrIf two parameters are passed in, the former is the real part and the latter is the virtual part.

    x1 = complex(1)
    x2 = complex(2,2)
    x3 = complex('3')
    print(type(x1), x1)
    print(type(x2), x2)
    print(type(x3), x3)
    
    #The output results are as follows:
     (1+0j)
     (2+2j)
     (3+0j)
  • Bool (boolean type)

    explain: a subclass of int, which returns true and false, corresponding to 1 and 0.

    x1 = bool(0)
    x2 = bool(1)
    x3 = bool()
    x4 = 2 > 1
    print(type(x1), x1)
    print(type(x2), x2)
    print(type(x3), x3)
    print(type(x4), x4)
    
    #The output results are as follows:
     False
     True
     False
     True

2.2 sequence

2.2.1 list

explain:A list is made up of several element objects and isOrderly variableLinear data structure of, using brackets[ ]express.

  • initialization

    Lst = [] empty list mode 1
    #Or
    Lst = list() (empty list mode 2)
    print(type(lst),lst)
    
    #The input results are as follows:
     []
  • Indexes

    explain:Use positive index (from left to right) and negative index (from right to left) to access elements with time complexity ofO(1), highly efficient use.

    Data obtained according to a given interval is called slice.

    Positive index:

    Index from left to right, starting from 0, interval is [0, length – 1], left package is not right package.

    lst = ['a','b','c','d']
    Print (LST [0]) gets the first element
    Print (LST [1:2]) gets the second element, left package and right package, slice
    Print (LST [2:]) ාget the third element to the last element, slice
    Print (LST [:]) (get all elements, slice)
    
    #The output results are as follows:
    a
    ['c']
    ['c', 'd']
    ['a', 'b', 'c', 'd']

    Negative index:

    Index from right to left, starting from – 1, interval is [- length, – 1]

    lst = ['a','b','c','d']
    print(lst[-1])
    print(lst[-2:])
    
    #The output results are as follows:
    d
    ['c', 'd']
  • query

    Index () method:L.index(value, [start, [stop]]) -> integer

    The index ID is returned. To iterate the list, the time complexity is O (n).

    lst = ['a','b','c','d']
    print( lst.index ('a ', 0,4)) ා get the index ID of element' a 'of interval [0,4]
    
    #The output results are as follows:
    0

    Note: if the element cannot be queried, theValueError

    Count() method:L.count(value) -> integer

    It returns the number of times the element appears. To iterate the list, the time complexity is O (n).

    lst = ['a','b','a','b']
    print(lst.count('a'))
    
    #The output results are as follows:
    2

    Len() method:It returns the number of list elements with a time complexity of O (1).

    lst = ['a','b','c','d']
    print(len(lst))
    
    #The output results are as follows:
    4

    remarks:The so-called o (n) refers to the decrease of efficiency with the increasing scale of data, while the O (1) does not affect efficiency with the large scale of data.

  • modify

    The list is ordered and variable, so the elements in the list can be modified.

    lst = ['a','b','c','d']
    lst[0] = 'A'
    print(lst)
    
    #The output results are as follows:
    ['A', 'b', 'c', 'd']
  • increase

    Append() method:L.append(object) -> None

    Append element at the end, modify in place, and return none.

    lst = ['a','b','c','d']
    lst.append('e')
    print(lst)
    
    #The output results are as follows:
    ['a', 'b', 'c', 'd', 'e']

    Insert() method:L.insert(index, object) -> None ,

    Inserts the element object at the specified index location and returns none.

    lst = ['a','b','c','d']
    lst.insert (0, 'a') insert 'a' at index 0, and all the original elements move backward, increasing the complexity
    print(lst)
    
    #The output results are as follows:
    ['A', 'a', 'b', 'c', 'd']

    Extend() method: L.extend(iterable) -> None

    You can add multiple elements, append the elements of the iteratable object, and return none.

    lst = ['a','b','c','d']
    lst.extend([1,2,3])
    print(lst)
    
    #The output results are as follows:
    ['a', 'b', 'c', 'd', 1, 2, 3]

    You can also pass the list through the+and*, spliced into a new list.

    lst1 = ['a','b','c','d']
    lst2 = ['e','f','g']
    print(lst1 + lst2)
    Print (lst1 * 2) ා񖓿make two copies of each element in the list
    
    #The output results are as follows:
    ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']

    Here is another special note:

    Lst1 = [[1]] * 3 × result: [[1], [1], [1]]
    print(lst1)
    Lst1 [0] [0] = 10. Result: [[10], [1], [1]], is that right??
    print(lst1)
    
    #The output results are as follows:
    [[1], [1], [1]]
    [[10], [10], [10]] (why is the result this? Please look down at the copy chapter of the list for the answer!
  • delete

    Remove() method:L.remove(value) -> None

    Traverse the search from left to right, delete the element when it is found, return none, and throw when it is not foundValueError

    lst = ['a','b','c','d']
    lst.remove('d')
    print(lst)
    
    #The output results are as follows:
    ['a ',' B ',' C '] (element' d 'has been deleted

    Pop () method:L.pop([index]) -> item

    The tail element is deleted by default. You can specify the index to delete the element. The index is thrown out of boundsIndexError

    lst = ['a','b','c','d']
    lst.pop()
    print(lst)
    
    #The output results are as follows:
    ['a', 'b', 'c']

    Clear() method:L.clear() -> None

    Clear all elements of the list, use with caution.

    lst = ['a','b','c','d']
    lst.clear()
    print(lst)
    
    #The output results are as follows:
    [] empty list
  • reversal

    Reverse() method:L.reverse()

    Reverses the elements in the list to return none.

    lst = ['a','b','c','d']
    lst.reverse()
    print(lst)
    
    #The output results are as follows:
    ['d', 'c', 'b', 'a']
  • sort

    Sort() method:L.sort(key=None, reverse=False) -> None

    Sort the list elements. The default is ascending, and reverse = true is descending.

    lst = ['a','b','c','d']
    lst.sort(reverse=True)
    print(lst)
    
    #The output results are as follows:
    ['d', 'c', 'b', 'a']
  • In member operation

    Judge whether the member is in the list. If there is one, return true; if there is none, return false.

    lst = ['a','b','c','d']
    print('a' in lst)
    print('e' in lst)
    
    #The output results are as follows:
    True
    False
  • List copy

    explain:List copy refers to the copy of list elements, which can be divided into shallow copy and deep copy. List element objects such as lists, tuples, dictionaries, classes, and instances are classified as reference types (pointing to memory addresses), while numbers and strings are classified as simple types so that you can understand them.

    Example 1: is this a copy?

    lst1 = [1,[2,3],4]
    lst2 = lst1
    Print (ID (lst1), ID (lst2), lst1 = = lst2, lst2) ා id() view memory address
    
    #The output results are as follows:
    1593751168840 1593751168840 True [1, [2, 3], 4]

    Obviously, it does not belong to any copy. To put it bluntly, it points to the same memory address.

    Example 2: shallow copy

    explain:Shallow copy forreference typeObject will not be copied, and the address points are still the same.

    lst1 = [1,[2,3],4]
    lst2 = lst1.copy()
    print(id(lst1),id(lst2),lst1 == lst2, lst2)
    print('=' * 30)
    Lst1 [1] [0] = 200 ා modify the reference type of the list, and all lists will change
    print(lst1, lst2)
    
    #The output results are as follows:
    1922175854408 1922175854344 True [1, [2, 3], 4]
    ==============================
    [1, [200, 3], 4] [1, [200, 3], 4]

    Example 3: deep copy

    explain:Deep copy forreference typeThe object will also be copied to another copy, with different address points.

    import copy
    
    lst1 = [1,[2,3],4]
    lst2 = copy.deepcopy(lst1)
    print(id(lst1),id(lst2),lst1 == lst2, lst2)
    print('=' * 30)  
    Lst1 [1] [0] = 200 ා modify the reference type of the list without affecting other lists
    print(lst1, lst2)
    
    #The output results are as follows:
    2378580158344 2378580158280 True [1, [2, 3], 4]
    ==============================
    [1, [200, 3], 4] [1, [2, 3], 4]

2.2.2 tuple

explain:Tuples are composed of several element objects, and areOrder immutableData structure of, using parentheses( )express.

  • initialization

    T1 = () empty element mode 1, once created, cannot be changed
    T2 = tuple() ා empty element mode 2, once created, will not be changed
    T3 = ('a ',) ා there is only one element in the tuple, make sure to add a comma', '
    T4 = (['a ',' B ',' C ']) (empty list method 2)

    remarks:Tuple if there is only one element object,certainComma after,Otherwise, it becomes another data type.

  • Indexes
    As with lists, there are no more examples.

    t = ('a','b','c','d')
    print(t[0])
    print(t[-1])
    #The output results are as follows:
    a
    d
  • query

    As with lists, there are no more examples.

    t = ('a','b','c','d')
    print(t.index('a'))
    print(t.count('a'))
    print(len(t))
    
    #The output results are as follows:
    0
    1
    4
  • Addition, deletion and modification

    Tuple isImmutableType, element object cannot be added, deleted or modified.

    But pay attention to the following scenarios:

    The element object (memory address) in the tuple is immutable, and the reference type is variable. —-Here’s the reference type again.

    #Tuple of tuple cannot be modified (i.e. memory address)
    t = ([1],)
    t[0]= 100
    print(t)
    #It turned out to be a mistake
    TypeError: 'tuple' object does not support item assignment
        
    ############################################
    
    #Reference type objects in tuples can be modified (such as nested lists)
    t = ([1],2,3)
    T [0] [0] = 100 ා modify element of tuple reference type object
    print(t)
    
    #The output results are as follows:
    ([100], 2, 3)

2.2.3 string string

explain:A string is composed of several characters and isOrder immutableThe data structure of, expressed in quotation marks.

  • initialization
    Various patterns, using single quotation mark, double quotation mark, triple quotation mark, etc.

    name = 'tom'
    age = 18
    STR1 ='abc 'ා single quote string
    STR2 = "ABC" ා double quotation mark string
    Str3 = "" I'm Python "" "ා three quotation mark string
    Str4 = R "C: \ windows \ note" ා r prefix, no escape (escape character is not valid)
    Str5 = f '{name} is {age} age.' ා f prefix, string format, V3.6 support
    print(type(str1), str1)
    print(type(str2), str2)
    print(type(str3), str3)
    print(type(str4), str4)
    print(type(str5), str5)
    
    #The output results are as follows:
     abc
     abc
     I'm python
     c:\windows\note
     tom is 18 age.
  • Indexes

    As with lists, there are no more examples.

    str = "abcdefg"
    print(str[0])
    print(str[-1])
    
    #The output results are as follows:
    a
    g
  • connect

    By plus+Connect multiple strings and return a new string.

    str1 = "abcd"
    str2 = "efg"
    print(str1 + str2)
    
    #The output results are as follows:
    abcdefg

    Join() method:S.join(iterable) -> str

    S represents the separator string, and Iterable is an iterative objectcharacter string, the result returns a string.

    str = "abcdefg"
    print('->'.join(str))
    
    #The output results are as follows:
    a->b->c->d->e->f->g
  • characters finding

    Find() method:S.find(sub[, start[, end]]) -> int

    Find the substring sub from left to right, or specify the interval. If it is found, it will return a positive index. If it is not found, it will return a positive index-1

    str = "abcdefg"
    print(str.find('a',0,7))
    print(str.find('A'))
    
    #The output results are as follows:
    0
    -1

    Rfind() method:S.rfind(sub[, start[, end]]) -> int

    Find the substring sub from the right to the left, or specify an interval, find and return a positive index, or return if not found-1

    str = "abcdefg"
    print(str.rfind('a'))
    print(str.rfind('A'))
    
    #The output results are as follows:
    0
    -1

    alsoindex()andfind()Similar, but can not find throw exception, not recommended.

    images.count()You can also count the number of character occurrences.

    imagelen(s)You can also count the length of a string.

  • division

    Split() method:S.split(sep=None, maxsplit=-1) -> list of strings

    SEP is the separator, the default is a blank string, maxplit = – 1 is to traverse the whole string, and finally return the list.

    str = "a,b,c,d,e,f,g"
    print(str.split(sep=','))
    
    #The output results are as follows:
    ['a', 'b', 'c', 'd', 'e', 'f', 'g']

    The rsplit () method differs from the above in that it traverses from right to left.

    Splitlines() method: S.splitlines([keepends]) -> list of strings

    The string is cut by line, and keeps indicates whether the line separator is reserved, and finally returns the list.

    str = "a\nb\nc\r\nd"
    print(str.splitlines())
    print(str.splitlines(keepends=True))
    
    #The output results are as follows:
    ['a', 'b', 'c', 'd']
    ['a\n', 'b\n', 'c\r\n', 'd']

    Partition () methodS.partition(sep) -> (head, sep, tail)

    When querying the separator from left to right, a tuple is returned, which is divided into three tuples: header, separator and tail.

    str = "a*b*c*d"
    print(str.partition('*'))
    #The output results are as follows:
    ('a', '*', 'b*c*d')

    Rpartition () methodS.rpartition(sep) -> (head, sep, tail)

    Different from the above method, i.e. from right to left, but this method is more commonly used. You can get suffix information.

    str1 = "http://www.python.org:8843"
    str2 = str1.rpartition(':')
    port = str2[-1]
    print(port)
  • replace

    Replace() method:S.replace(old, new[, count]) -> str

    Traverse the entire string to find all replacements. Count indicates the number of times to replace. The default replacement is all, and the last one is returnedNew string

    str = "www.python.org"
    print( str.replace ('w ','m')) × returns a new string
    Print (STR) ා the string is immutable and remains the same
    
    #The output results are as follows:
    mmm.python.org
    www.python.org
  • remove

    Strip() method:S.strip([chars]) -> str

    Removes the specifiedCharacter set chars, the blank character is removed by default.

    str = " * www.python.org  *"
    print( str.strip ("*") ා remove the asterisk '*' and blank '' at the beginning and end of the string
    
    #The output results are as follows:
    www.python.org

    alsolstrip()andrstripRemove the character set to the left and right of the string, respectively.

  • Head and tail judgment

    Startswitch() method:S.startswith(prefix[, start[, end]]) -> bool

    By default, it determines whether there is a specified character prefix at the beginning of the string, and it can also specify an interval.

    str = "www.python.org"
    print(str.startswith('www',0,14))
    print(str.startswith('p',0,14))
    #The output results are as follows:
    True
    False

    Endswitch() method:S.endswith(suffix[, start[, end]]) -> bool

    By default, it determines whether there is a specified character suffix at the end of the string, and it can also specify an interval.

    str = "www.python.org"
    print(str.startswith('www',0,14))
    print(str.startswith('p',0,14))
    #The output results are as follows:
    True
    False
    str = "www.python.org"
    print(str.endswith('g',11,14))
    #The output results are as follows:
    True
  • format

    C style format:

    Format string: use% s (the corresponding value is string),% d (the corresponding value is number), etc., and you can also insert the modifier% 03D in the middle.

    Formatted value: can only be an object, can be a tuple or a dictionary.

    name = "Tom"
    age = 18
    print("%s is %d age." % (name,age))
    #The output results are as follows:
    Tom is 18 age.

    Format format:

    Format string: use curly braces {}, and modifiers can be used inside curly braces.

    Formatted value: * args is the variable position parameter, * * kwargs is the variable key parameter.

    #Positional reference
    print("IP={} PORT={}".format('8.8.8.8',53))  #Positional reference
    Print ("{server}: IP = {1} port = {0}". Format (53, '8.8.8.8', server = DNS server ')) (location and keyword parameters
    
    #The output results are as follows:
    IP=8.8.8.8 PORT=53
    DNS Server: IP=8.8.8.8 PORT=53
    #Floating point
    print("{}".format(0.123456789))
    Print ("{: F}". Format (0.123456789)) 񖓿 the default decimal point is 6 digits
    Print ("{:. 2F}". Format (0.123456789)) ාtwo decimal places
    Print ("{: 15}". Format (0.123456789)) 񖓿 width is 15, right justified
    
    #The output results are as follows:
    0.123456789
    0.123457 why is this value? Greater than 5 to carry
    0.12
        0.123456789 ා 4 spaces to the left
  • Other common functions

    str = "DianDiJiShu"
    print( str.upper ()) ාාall converted to uppercase
    print( str.lower ()) ාall letters are converted to lowercase
    
    #The output results are as follows:
    DIANDIJISHU
    diandijishu

2.2.4 bytes

bytes andbytearrayTwo data types introduced from Python 3.

In the world of computers, machines0and1Composed, also called binary (byte) to communicate. This code is calledASCIIcode.

So the language of machine communication is called machine language. However, if we want to communicate with machines, what should we do?

  • Encoding human language into machine recognizable language is usually called encoding (string to ASCII).
  • To decode the language of a machine into a language that can be recognized by an adult class is usually called decoding (ASCII code to string).

So far, the history of modern coding is roughly as follows: ASCII (1 byte) – > Unicode (2-4 bytes) – > UTF-8 (1)6 bytes), utf8 is multi byte encoding, generally using 13 bytes, special use 4 bytes (general Chinese use 3 bytes), downward compatible with ASCII encoding.

China also has its own code: GBK

Common ASCII code table must bear in mind (sorting part):

Detailed ASCII download link:

Link: https://pan.baidu.com/s/1fWVl57Kqmv-tkjrDKwPvSw  Extraction code: tuyz

Therefore, the system on the machine is byte, 1 byte is equal to 8 bits, for example, decimal 2, which is represented by binary and hexadecimal:

#Binary
0000 0010 - one byte

#Hexadecimal, the machine is basically display hexadecimal
0x2

Bytes is immutable

Bytes() ාාempty bytes, once created, cannot be changed
Bytes (int) × specifies the size of bytes, filled with 0
bytes(iterable_ Of_ Int) iteratable object of [0.255] integer
Bytes (string, encoding [, errors]) is equivalent to string.encoding (), string encoded in bytes
bytes(bytes_ Or_ Buffer) copy a new byte object
  • initialization

    b1 = bytes()
    b2 = bytes(range(97,100))
    b3 = bytes(b2)
    b4 = bytes('123',encoding='utf-8')
    b5 = b'ABC'
    b6 = b'\xe4\xbd\xa0\xe5\xa5\xbd'.decode('utf-8')
    print(b1, b2, b3, b4, b5, b6, sep='\n')
    
    #The output results are as follows:
    b''
    b'abc'
    b'abc'
    b'123'
    b'ABC'
    Hello

2.2.5 byte array

bytearrayIt is a variable array, which can be added, deleted or modified, similar to a list.

Bytearray() ා empty bytearray, changeable
bytearray(iterable_ Of_ Int) iteratable object of [0.255] integer
Bytearray (string, encoding [, errors]) is equivalent to string.encoding (), string encoded in bytes
bytearray(bytes_ Or_ Buffer) copy a new byte array object
Bytearray (int) ා specify the size of bytes, and fill with 0
  • Addition, deletion and modification

    #Initialization
    b = bytearray()
    print(b)
    #The output results are as follows:
    bytearray(b'')
    #--------------------------
    #Add element object
    b.append(97)
    print(b)
    b.extend([98,99])
    print(b)
    #The output results are as follows:
    bytearray(b'a')
    bytearray(b'abc')
    #--------------------------
    #Insert element object
    b.insert(0,65)
    print(b)
    #The output results are as follows:
    bytearray(b'Aabc')
    #--------------------------
    #Delete element object
    b.pop()
    print(b)
    #The output results are as follows:
    bytearray(b'Aab')

That’s it today. Let’s talk about it next roundSet (set)andDict (Dictionary), please wait patiently.


If you love my articles, please pay attention to my official account: drip technology, scan code attention, and share it regularly.

公众号:点滴技术