# Python Linear Data Structure

python linear data structure

```<center>Code every article in python. </center>
``` # 1 Linear data structure

This chapter describes the linear structure: list, tuple, string, bytes, bytearray.

• Linear table: An abstract mathematical concept that is the abstraction of a sequence of elements consisting of an infinite number of elements (0 or any).

Linear tables can also be divided into sequential and linked tables.

• Sequential table: An ordered storage of a set of elements in memory.A list of lists is a typical sequential table.

• Linked table: A group of elements that are linked in separate storage in memory and know each other who is connected.

For both tables, elements in the array are searched, added, deleted, and modified to see what happens: • Find Elements

For sequential tables, the data is stored in memory in an orderly manner, which can quickly retrieve elements by index number and is efficient.

Linked tables are stored separately, and elements can only be retrieved iteratively in one iteration, which is inefficient.

For sequential tables, adding elements at the end will have no impact on the entire data table, but inserting elements at the beginning or in the middle will reorder all subsequent elements, which has a significant impact (think of millions or more of data).

For linked tables, no matter where an element is added, it will not affect other elements, but will have a small impact.

• Delete element
For sequential tables, deleting elements has the same problem as adding elements.
For linked tables, deleting elements anywhere does not affect other elements, but has little effect.

• Modify Element
For sequential tables, elements can be quickly retrieved and then modified through an index, which is efficient.

For linked tables, elements can only be retrieved and modified iteratively, which is inefficient.

Summary: Sequential tables are the most efficient for finding and modifying, and the most inefficient for increasing and deleting.Linked tables are the opposite.

# 2. Built-in common data types

## 2.1 numerical model

• int integer type

Description: Integers include negative integers, 0, and 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 is as follows:
<class 'int'> 1
<class 'int'> 0
<class 'int'> -1
```

int() method: A number or string can be converted to an integer, defaulting to base=10, meaning 10, and returning 0 if no parameter is passed in.

```x1 = int()
x2 = int('1')
x3 = int('0b10',base=2)  #base=2, table binary, consistent with the type of incoming parameter.
x4 = int(3.14)
print(type(x1), x1)
print(type(x2), x2)
print(type(x3), x3)
print(type(x4), x4)

# The output is as follows:
<class 'int'> 0
<class 'int'> 1
<class 'int'> 2
<class 'int'> 3
```
• float floating point type
Description: Composed of integers and decimals, the parameters passed in can be int, str, bytes, bytearray.

```x1 = float(1)
x2 = float('2')
x3 = float(b'3')
print(type(x1), x1)
print(type(x2), x2)
print(type(x3), x3)

# The output is as follows:
<class 'float'> 1.0
<class 'float'> 2.0
<class 'float'> 3.0
```
• Complex (complex type)

Description: Composed of real and imaginary parts, are floating point numbers.

The incoming parameter can be int or str. If two parameters are passed in, the first parameter is the real part and the second parameter is the imaginary 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 is as follows:
<class 'complex'> (1+0j)
<class 'complex'> (2+2j)
<class 'complex'> (3+0j)
```
• bool (Boolean type)

Description: As a subclass of int, 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 is as follows:
<class 'bool'> False
<class 'bool'> True
<class 'bool'> False
<class 'bool'> True
```

## 2.2 Sequence

### 2.2.1 list list list list

Description: A list is an orderly, variable, linear data structure composed of several element objects, represented by square brackets [].

• Initialization

```lst = []  # Empty List Mode 1
#perhaps
lst = list()  # Empty List Mode 2
print(type(lst),lst)

# The input results are as follows:
<class 'list'> []
```
• Indexes

Description: Use positive index (left-to-right), negative index (right-to-left) to access elements with O(1) time complexity and efficient usage.

Getting data by a given interval is called a slice.

Positive index:

Left to right, index from 0, interval is [0, length-1], left package is not right package.

```lst = ['a','b','c','d']
print(lst)  # Get the first element
print(lst[1:2])  # Get the second element, left pack, right pack, slice
print(lst[2:])  # Get the third to the last element, slice
print(lst[:])  # Get all elements, slice

# The output is as follows:
a
['c']
['c', 'd']
['a', 'b', 'c', 'd']
```

Negative index:

Right to left, index from -1, interval [-length, -1]

```lst = ['a','b','c','d']
print(lst[-1])
print(lst[-2:])

# The output is as follows:
d
['c', 'd']
```
• query

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

Returns the index id with an O(n) time complexity to iterate over the list.

```lst = ['a','b','c','d']
print(lst.index('a',0,4))  # Gets the index id of the element'a'of the interval [0,4]

# The output is as follows:
0
```

Note: If the element cannot be queried, a ValueError is thrown.

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

Returns the number of occurrences of the element, iterating through the list with a time complexity of O(n).

```lst = ['a','b','a','b']
print(lst.count('a'))

# The output is as follows:
2
```

Le () method: Returns the number of elements in the list with a time complexity of O(1).

```lst = ['a','b','c','d']
print(len(lst))

# The output is as follows:
4
```

Note: The so-called O(n) means that efficiency decreases with the size of the data, whereas O(1) does not affect efficiency with the size of the data.

• modify

Lists are ordered and variable, so you can modify the elements in the list.

```lst = ['a','b','c','d']
lst = 'A'
print(lst)

# The output is as follows:
['A', 'b', 'c', 'd']
```
• increase

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

Append element to tail, modify in place, return None.

```lst = ['a','b','c','d']
lst.append('e')
print(lst)

# The output is as follows:
['a', 'b', 'c', 'd', 'e']
```

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

Inserts an element object at the specified index position and returns None.

```lst = ['a','b','c','d']
lst.insert(0,'A')  # Inserting'A'at index 0 adds complexity by moving all elements backwards
print(lst)

# The output is as follows:
['A', 'a', 'b', 'c', 'd']
```

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

You can add more than one element, append elements of an iterative object, and return None.

```lst = ['a','b','c','d']
lst.extend([1,2,3])
print(lst)

# The output is as follows:
['a', 'b', 'c', 'd', 1, 2, 3]
```

You can also stitch lists into new lists using + and *.

```lst1 = ['a','b','c','d']
lst2 = ['e','f','g']
print(lst1 + lst2)
print(lst1 * 2)  # Copy 2 copies of each element in the list

# The output is as follows:
['a', 'b', 'c', 'd', 'e', 'f', 'g']
['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']
```

Here is another special note:

```lst1 = [] * 3  # Results: [, , ]
print(lst1)
lst1 = 10  # Result: [, , , is that correct?
print(lst1)

# The output is as follows:
[, , ]
[, , ]  # Why is this the result?Look down at the list copy chapter to find the answer!
```
• delete

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

Walk through the search from left to right, delete the element if found, return None, and throw ValueError if not found.

```lst = ['a','b','c','d']
lst.remove('d')
print(lst)

# The output is as follows:
['a', 'b', 'c']  # Element'd'has been deleted
```

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

Default delete tail element, specify index delete element, index throw IndexError out of bounds.

```lst = ['a','b','c','d']
lst.pop()
print(lst)

# The output is as follows:
['a', 'b', 'c']
```

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

Empty all elements of the list and use with caution.

```lst = ['a','b','c','d']
lst.clear()
print(lst)

# The output is as follows:
[]  # Empty list
```
• Reversal

Reverse() method: L.reverse()

Reverse the elements in the list to return None.

```lst = ['a','b','c','d']
lst.reverse()
print(lst)

# The output is as follows:
['d', 'c', 'b', 'a']
```
• sort

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

Sort list elements by default in ascending order and reverse=True in descending order.

```lst = ['a','b','c','d']
lst.sort(reverse=True)
print(lst)

# The output is as follows:
['d', 'c', 'b', 'a']
```
• in member action

Determines whether a member is in the list, returns True if there is one, and returns False if there is none.

```lst = ['a','b','c','d']
print('a' in lst)
print('e' in lst)

# The output is as follows:
True
False
```
• List Copy

Description: List copying refers to the copying of list elements, which can be divided into shallow copy and deep copy.List element objects such as lists, tuples, dictionaries, classes, instances are categorized as reference types (pointing to memory addresses), while numbers and strings are categorized as simple types first so that they can be understood.

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 is as follows:
1593751168840 1593751168840 True [1, [2, 3], 4]
``` Obviously it's not any copy, it's all pointing to the same memory address.

Example 2: Shallow copy

Note: Shallow copies are not copy able for reference type objects, and the address points to the same. ```lst1 = [1,[2,3],4]
lst2 = lst1.copy()
print(id(lst1),id(lst2),lst1 == lst2, lst2)
print('=' * 30)
lst1 = 200  # Modify the reference type of the list, all lists will change
print(lst1, lst2)

# The output is as follows:
1922175854408 1922175854344 True [1, [2, 3], 4]
==============================
[1, [200, 3], 4] [1, [200, 3], 4]
```

Example 3: Deep copy

Note: A deep copy will also be copied to another reference type object 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 = 200  # Modify the reference type of the list without affecting other lists
print(lst1, lst2)

# The output is as follows:
2378580158344 2378580158280 True [1, [2, 3], 4]
==============================
[1, [200, 3], 4] [1, [2, 3], 4]
```

### 2.2.2 tuple tuple

Description: A tuple is an ordered, immutable data structure composed of several element objects, represented by parentheses ().

• Initialization

```t1 = ()  # Empty Element Mode 1, once created, will not change
t2 = tuple()  # Empty Element Mode 2, once created, will not change
t3 = ('a',)  # A tuple has only one element. Make sure it has a comma', '
t4 = (['a','b','c'])  # Empty List Mode 2
```

Note: If there is only one element object in a tuple, make sure to add a comma after it, otherwise it will become a different data type.

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

```t = ('a','b','c','d')
print(t)
print(t[-1])
# The output is 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 is as follows:
0
1
4
```

Tuples are immutable types and cannot add or delete element objects.

But be aware of the following scenarios:

Element objects (memory addresses) in tuples are immutable and reference types are variable.--There's a reference type again.

```# Tuples of tuples are not modifiable (that is, memory addresses)
t = (,)
t= 100
print(t)
# The result was misreported
TypeError: 'tuple' object does not support item assignment

############################################

# Reference type objects inside tuples can be modified (such as nested lists)
t = (,2,3)
t = 100  # Modify elements of tuple reference type objects
print(t)

# The output is as follows:
(, 2, 3)
```

### 2.2.3 string

Description: A string consists of several characters and is an ordered, immutable data structure, expressed in quotation marks.

• Initialization
Various patterns, using single quotation marks, double quotation marks, three quotation marks, and so on.

```name = 'tom'
age = 18
str1 = 'abc'  # Single quote string
str2 = "abc"  # Double Quoted String
str3 = """I'm python"""  # Triple Quoted String
str4 = r"c:\windows\note"  # r prefix, no escape (escape characters do not work)
str5 = f'{name} is {age} age.'  # f prefix, string formatting, 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 is as follows:
<class 'str'> abc
<class 'str'> abc
<class 'str'> I'm python
<class 'str'> c:\windows\note
<class 'str'> tom is 18 age.
```
• Indexes

As with lists, there are no more examples.

```str = "abcdefg"
print(str)
print(str[-1])

# The output is as follows:
a
g
```
• Connect

Returns a new string by concatenating multiple strings with a plus sign.

```str1 = "abcd"
str2 = "efg"
print(str1 + str2)

# The output is as follows:
abcdefg
```

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

s is a delimiter string, iterable is an iteratable object string, and the result returns the string.

```str = "abcdefg"
print('->'.join(str))

# The output is 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 an interval, find the return positive index, and return -1 if not found.

```str = "abcdefg"
print(str.find('a',0,7))
print(str.find('A'))

# The output is as follows:
0
-1
```

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

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

```str = "abcdefg"
print(str.rfind('a'))
print(str.rfind('A'))

# The output is as follows:
0
-1
```

There are also index() and find() similar, but if not found, an exception will be thrown and is not recommended.

Like s.count(), you can also count the number of occurrences of a character.

Like len(s), you can also count the length of a string.

• Division

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

sep denotes a delimiter, defaults to a blank string, maxsplit=-1 denotes traversing the entire string and returning the list.

```str = "a,b,c,d,e,f,g"
print(str.split(sep=','))

# The output is as follows:
['a', 'b', 'c', 'd', 'e', 'f', 'g']
```

The rsplit() method differs from the above by traversing from right to left.

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

Cut the string by line, keepends indicating whether to keep the line separator, and returns to the list.

```str = "a\nb\nc\r\nd"
print(str.splitlines())
print(str.splitlines(keepends=True))

# The output is as follows:
['a', 'b', 'c', 'd']
['a\n', 'b\n', 'c\r\n', 'd']
```

partition() method: S.partition (sep) -> (head, sep, tail)

Query the separator from left to right, and when you encounter a tuple that is split into a header, separator, and tail, return a tuple.

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

rpartition() method: S.rpartition (sep) -> (head, sep, tail)

Unlike the above method, which is right-to-left, this is a common one where you can get some information about the suffix.

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

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

Walk through the entire string, find all replacements, count indicates the number of replacements, default replaces all, and finally return a new string.

```str = "www.python.org"
print(str.replace('w','m'))  # A new string is returned
print(str)  # String is immutable and remains as it is

# The output is as follows:
mmm.python.org
www.python.org
```
• remove

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

Removes the specified character set chars at both ends of the string, removing blank characters by default.

```str = " * www.python.org  *"
print(str.strip("* "))  # String with asterisk'*'and blank'' removed

# The output is as follows:
www.python.org
```

lstrip() and rstrip remove the left and right character sets of strings, respectively.

• Judgment from beginning to end

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

The default is to determine whether a specified character prefix exists at the beginning of a string, or to specify an interval.

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

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

The default is to determine whether a specified character suffix exists at the end of a string, or to specify an interval.

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

c Style Formatting: Format string: You can also insert the modifier%03d in the middle using%s (corresponding value is string),%d (corresponding value is number), etc.

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 is as follows:
Tom is 18 age.
```

format formatting: Format string: Use curly brackets {} where modifiers can be used.

Values formatted: *args are variable positional parameters, **kwargs are variable keyword parameters.

```# Position parameter
print("IP={} PORT={}".format('8.8.8.8',53))  # Position parameter
print("{Server}: IP={1} PORT={0}".format(53, '8.8.8.8', Server='DNS Server'))  # Position and keyword referral parameters

# The output is as follows:
IP=8.8.8.8 PORT=53
DNS Server: IP=8.8.8.8 PORT=53
```
```# Floating point number
print("{}".format(0.123456789))
print("{:f}".format(0.123456789))    #  Decimal point defaults to 6 digits
print("{:.2f}".format(0.123456789))  # Two decimal places
print("{:15}".format(0.123456789))   # Width 15, right aligned

# The output is as follows:
0.123456789
0.123457     # Why is this value?Greater than 5 to carry
0.12
0.123456789  # There are 4 spaces on the left
```
• Other Common Functions

```str = "DianDiJiShu"
print(str.upper())  # Convert all letters to uppercase
print(str.lower())  # Convert all letters to lowercase

# The output is as follows:
DIANDIJISHU
diandijishu
```

### 2.2.4 bytes

Two data types introduced by bytes and bytearray from python3.

In the world of computers, machines are composed of 0 and 1, also known as binary (byte) to communicate. This set of codes is called ASCII encoding.

So the language of machine communication is called machine language.But what do we humans need to do if we want to communicate with machines?

• Encoding a human language into a machine-recognized language is often called encoding (string to ASCII code).
• Decoding a machine's language into a human-recognized language is often called decoding (ASCII code to string).

The history of modern encoding so far is roughly the following: ASCII(1 byte) -> Unicode (2-4 bytes) -> UTF-8 (16 bytes), utf8 is a multibyte encoding, usually 13 bytes, especially 4 bytes (3 bytes in general Chinese), which is downward compatible with ASCII encoding.

China also has its own code: gbk

Common ASCII code tables must be kept in mind (collation section): ```Link: https://pan.baidu.com/s/1fWVl57Kqmv-tkjrDKwPvSw Extraction Code: tuyz
```

So the binary on the machine is the byte, and one byte equals eight bits, for example, decimal 2, represented in binary and 16:

```# Binary
0000 0010  # One byte bytes

0x2
```

bytes is an immutable type

```bytes()     # Empty bytes, once created unchangeable
bytes(int)  # Specifies the size of the byte, filled with 0
bytes(iterable_of_ints)  # Iterable object of [0.255] integer
bytes(string, encoding[, errors])  # Equivalent to string.encoding(), string encoded as 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 is as follows:
b''
b'abc'
b'abc'
b'123'
b'ABC'
//Hello
```

### 2.2.5 bytearray byte array

bytearray is a variable array that can be added or deleted, like a list.

```bytearray()  # Empty bytearray, can change
bytearray(iterable_of_ints)  # Iterable object of [0.255] integer
bytearray(string, encoding[, errors])  # Equivalent to string.encoding(), string encoded as bytes
bytearray(bytes_or_buffer)   # Copy a new byte array object
bytearray(int)  # Specifies the size of the byte, filled with 0
```

```# Initialization
b = bytearray()
print(b)
# The output is as follows:
bytearray(b'')
#--------------------------
b.append(97)
print(b)
b.extend([98,99])
print(b)
# The output is as follows:
bytearray(b'a')
bytearray(b'abc')
#--------------------------
# Insert Element Object
b.insert(0,65)
print(b)
# The output is as follows:
bytearray(b'Aabc')
#--------------------------
# Delete Element Object
b.pop()
print(b)
# The output is as follows:
bytearray(b'Aab')
```

That's it for today. Please be patient with us next round, followed by set and dict.

If you like my article, please pay attention to my public number: drip technology, scanner attention, share it regularly Posted on Thu, 26 Mar 2020 14:50:29 -0400 by CraigRoberts