[Python tutorial] Data structure

Introduction

This is an attempt to read the 3rd edition of the Python tutorial and make a note of what you have learned.

Python Tutorial 3rd Edition

And when I finish reading, I would like to take this exam By the end, the test has started ...!

** Let's get started! ** **

Python 3 Engineer Certification Basic Exam

I hope it continues, I hope it continues

data structure

Supplement about the list

All methods of list object

list.append(x) --Add an item to the end of the list --Equivalent to ʻa [len (a):] = [x] , ʻa + = [x]

>>> a = [66.25, 333, 333, 1, 1234.5]
>>> a.append(333)
>>> a
[66.25, 333, 333, 1, 1234.5, 333]

list.extend(L) --Expand the list by adding all the items in the given list L to the end of the list --Equivalent to ʻa [len (a):] = L, ʻa + = L.

>>> a = [66.25, 333, 333, 1, 1234.5]
>>> b = [1, 2, 3]
>>> a.extend(b)
>>> a
[66.25, 333, 333, 1, 1234.5, 1, 2, 3]

list.insert(i, x) --Insert an item at the specified position --The first argument is the index of the element --That is, the insertion is done before this element --ʻA.insert (0, x) will be inserted at the beginning of the list --ʻA.insert (len (a), x) is equivalent to ʻa.append (x)`

>>> a = [66.25, 333, 333, 1, 1234.5]
>>> a.insert(2, -1)
>>> a
[66.25, 333, -1, 333, 1, 1234.5]

list.remove(x) --Delete the first item with the value x --If such an item does not exist, an error will occur.

>>> a = [66.25, 333, 333, 1, 1234.5]
>>> a.remove(333)
>>> a
[66.25, 333, 1, 1234.5]

list.pop([i]) --Removes the item at the specified position from the list and returns this item --If no index is specified, a.pop () returns the last item in the list and removes it from the list.
* The square brackets used to enclose the index i in this method signature (definition notation) are , Just indicates that the argument is optional **, and a corner at this position It doesn't mean that you should enter parentheses. These notations are often seen in library references.

>>> a = [66.25, 333, 333, 1, 1234.5]
>>> a.pop()
1234.5
>>> a
[66.25, 333, 333, 1]
>>> a.pop(2)
333
>>> a
[66.25, 333, 1]

list.clear() --Remove all items from the list --Equivalent to del a [:]

>>> a = [66.25, 333, 333, 1, 1234.5]
>>> a.clear()
>>> a
[]

list.index(x) --Returns the index of the first item with a value of x --If such an item does not exist, an error will occur.

>>> a = [66.25, 333, 333, 1, 1234.5]
>>> a.index(1)
3

list.count(x) --Returns the number of x in the list

>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print(a.count(333), a.count(66.25), a.count('x'))
2 1 0

list.sort(key=None, reverse=False) --Sort the list in-place (= directly on the list object without making a copy) --Sort can be customized with arguments

>>> a = [333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]
>>> a.sort(reverse=True)
>>> a
[1234.5, 333, 333, 66.25, 1, -1]

list.reverse() --Reverse list elements in place

>>> a = [333, 1234.5, 1, 333, -1, 66.25]
>>> a.reverse()
>>> a
[66.25, -1, 333, 1, 1234.5, 333]

list.copy() --Returns a shallow copy of the list --Equivalent to ʻa [:] `

>>> a = [333, 1234.5, 1, 333, -1, 66.25]
>>> a.copy()
[333, 1234.5, 1, 333, -1, 66.25]

Use the list as a stack

--The last added element is fetched first on the stack --Use ʻappend ()to push items to the top of the stack --Use unindexedpop ()` to get an item from the top of the stack

>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack
[3, 4, 5]
>>> stack.pop()
5
>>> stack
[3, 4]

Use the list as a queue

--In the queue, get the elements in the order in which they were put. --While ʻappend and pop at the end of the list are fast, ʻinsert and pop at the beginning of the list are slow
(this requires all other elements to be shifted by 1). For) -** Use collections.deque to implement queues ** --deque is designed to speed up element additions and pops at both the beginning and end

>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry")
>>> queue.append("Graham")
>>> queue.popleft()
'Eric'
>>> queue.popleft()
'John'
>>> queue
deque(['Michael', 'Terry', 'Graham'])

List comprehension

--List comprehensions provide a complete way to generate lists --A common usage is to add some processing to each member of a sequence or iterable body to generate a new list, or to extract only the elements that meet certain conditions and generate a subsequence.

A program that generates a list of squares


>>> squares = []
>>> for x in range(10):
...     squares.append(x**2)
...
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

A program that generates a list of squares (when using the lamdba expression)


>>> squares = list(map(lambda x: x**2, range(10)))
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

A program that generates a list of squares (when using list comprehensions)


>>> squares = [x**2 for x in range(10)]
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

A list comprehension consists of an expression followed by a for clause, followed by zero or more for clauses or a ʻif` clause, all enclosed in parentheses ([]).

What you get is a new list of values that evaluate the first expression in the context of subsequent for and ʻif` clauses.

For example, the following list comprehension takes elements from two lists and tuples them if they are not the same.

>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x !=y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

This is equivalent to the following program

>>> combs = []
>>> for x in [1,2,4]:
...     for y in [3,1,4]:
...             if x != y:
...                     combs.append((x, y))
...
>>> combs
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (4, 3), (4, 1)]

Note that the for and if statements of both programs in the program below are arranged in the same order. Parentheses are required when the expression is a tuple

>>> #Generate a new list with doubled values
>>> vec = [-4, -2, 0, 2, 4]
>>> [x*2 for x in vec]
[-8, -4, 0, 4, 8]
>>> #Filter to remove negative numbers
>>> [x for x in vec if x >= 0]
[0, 2, 4]
>>> #Apply function to all elements
>>> [abs(x) for x in vec]
[4, 2, 0, 2, 4]
>>> #Call a method for each element
>>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
>>> [weapon.strip() for weapon in freshfruit]
['banana', 'loganberry', 'passion fruit']
>>> #Binary tuple(Numerical value,Squared value)Generate a list of
>>> [(x, x**2) for x in range(6)]
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
>>> #An error will occur if the tuple is not enclosed in parentheses.
>>> [x, x**2 for x in range(6)]
  File "<stdin>", line 1
    [x, x**2 for x in range(6)]
               ^
SyntaxError: invalid syntax
>>> #Smooth the list (one-dimensional) using two fors
>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

List comprehensions can contain complex expressions and nested functions

>>> from math import pi
>>> [str(round(pi, i)) for i in range(1, 6)]
['3.1', '3.14', '3.142', '3.1416', '3.14159']

Nested list comprehension

--You can use any expression at the beginning of the list comprehension, and you can put other list comprehensions here.

A 3x4 matrix implemented with 3 lists of length 4


>>> matrix = [
...     [1, 2, 3, 4],
...     [5, 6, 7, 8],
...     [9, 10, 11, 12],
... ]

Swap rows and columns


>>> [[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

The above program is equivalent to

>>> transposed = []
>>> for i in range(4):
...     transposed.append([row[i] for row in matrix])
...
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

That is, equivalent to

>>> transposed = []
>>> for i in range(4):
...     transposed_row = []
...     for row in matrix:
...             transposed_row.append(row[i])
...     transposed.append(transposed_row)
...
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

Realistically, use built-in functions for complex flows The zip function can be used in these situations

>>> list(zip(*matrix))
[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]

del statement

--The del statement specifies an index instead of a value when deleting an item in the list --Unlike the pop () method in that it does not return a value --The del statement can be used to slice out a list or clear an entire list

>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a
[]

del can also be used to delete an entire variable

>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

Tuples and sequences

--Tuples consist of comma-separated values

>>> t = 12345, 54321, 'hello!'
>>> t[0]
12345
>>> t
(12345, 54321, 'hello!')
>>> #Tuples can be nested
>>> u = t, (1, 2, 3, 4, 5)
>>> u
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
>>> #Tuples cannot be changed
>>> t[0] = 88888
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

Can store mutable objects

>>> v = ([1, 2, 3], [3, 2, 1])
>>> v
([1, 2, 3], [3, 2, 1])

The output tuples are always enclosed in parentheses, so nested tuples are also interpreted correctly. It is not possible to assign to tuple items, but it is possible to generate tuples that contain mutable objects such as lists.

Tuples look like lists, but they are used in different situations and have different uses. Tuples are ** immutable **, and it is customary to create a sequence of dissimilar elements and access each element unpacking or by index (or even an attribute in the case of a name month tuple).

Lists are ** mutable **, usually made up of similar elements, and accessed by iterating over the list.

Creating tuples with 0 or 1 items has special problems (indistinguishable from other types because there is no delimiter comma), so the syntax has an escape to deal with these. First, make an empty tuple with an empty pair of parentheses. And one item tuple is made by adding a comma after one value

>>> empty = ()
>>> singleton = 'hello',
>>> len(empty)
0
>>> singleton
('hello',)

The following sentence is an example of tuple packing (tuple packing) 12345, 54321, hello! are in one tuple

>>> t = 12345, 54321, 'hello!'
>>> t
(12345, 54321, 'hello!')

The following processing is called ** sequence unpacking **, and any sequence may come universally. When unpacking a sequence, you need a list of variables on the left side that is equal to the length of the sequence. Note that multiple assignment is just a combination of tuple packing and sequence unpacking.

>>> t = 12345, 54321, 'hello!'
>>> x, y, z = t
>>> x
12345
>>> y
54321
>>> z
'hello!'

Set

--A collection of elements that do not overlap with a set in no particular order --Basic uses include membership testing and elimination of duplicate entries. --Aggregate objects also support mathematical operations such as sums, intersections, differences, and symmetric differences. --You need to use curly braces {} or set () to generate the set (the whole company will generate an empty dictionary)

>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket)
{'orange', 'pear', 'banana', 'apple'}
>>> 'orange' in basket
True
>>> 'crabgrass' in basket
False

Demonstrate set operations by taking non-overlapping (unique) characters from two words

>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> #Unique character of a
>>> a
{'b', 'r', 'a', 'd', 'c'}
>>> #Characters that exist in a but not in b
>>> a - b
{'r', 'b', 'd'}
>>> #Characters present in a or b
>>> a | b
{'l', 'm', 'b', 'z', 'r', 'a', 'd', 'c'}
>>> #Characters that exist in both a and b
>>> a & b
{'a', 'c'}
>>> #Uncommon characters in a or b
>>> a ^ b
{'m', 'b', 'z', 'r', 'd', 'l'}

Set comprehensions that are very similar to list comprehensions are also supported

>>> a = { x for x in 'abracadabra' if x not in 'abc'}
>>> a
{'r', 'd'}

Dictionary (dictionary)

-See Python Library Reference 4.10. Mapping Types — dict --Dictionary may exist as "associative memory", "associative array", or "hash" in other languages. --Sequences are indexed by consecutive numbers, while dictionaries are indexed by keys. --Any immutable type can be used for the key --Strings and numbers can always be used as keys --Tuples can also be used as keys (provided that the tuple contains only strings, numbers, and tuples) --Tuples that contain mutable objects directly or indirectly cannot be used as keys --Lists cannot be used as keys.
* This is because it can be modified in-place by index assignment, slice assignment, and methods such as ʻappend () and ʻextend (). --Since the dictionary is conditional on the uniqueness of the keys (no duplication in one dictionary), it is appropriate to think of it as an unsorted set of "key: value" pairs. --If you write curly braces vs. "{}", it becomes an empty dictionary. --If you put a series of comma-separated "key: value" pairs in these curly braces, you will be given this "key: value: pair group" as the initial value of the dictionary. --The main function of the dictionary is to store the value with some key and retrieve the value by specifying the key. --You can also delete "key: value" for each pair with del. --If you store using a key that is already in use, the previous value will be lost. --If you try to retrieve a value with a key that does not exist, an error will occur. --Multiplying a dictionary with list (d.keys ()) returns an unsorted list of all the keys in that dictionary (if you want to sort, insteadsorted (d.keys ()) To --If you want to check if a key exists in the dictionary, use the keyword ʻin`.

>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'jack': 4098, 'sape': 4139, 'guido': 4127}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'jack': 4098, 'guido': 4127, 'irv': 4127}
>>> list(tel.keys())
['jack', 'guido', 'irv']
>>> sorted(tel.keys())
['guido', 'irv', 'jack']
>>> 'guido' in tel
True
>>> 'jack' not in tel
False

The dict constructor builds a dictionary from a sequence of tuples of" key: value "pairs.

>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'guido': 4127, 'jack': 4098}

Dictionary comprehensions can be used to generate dictionaries from arbitrary expressions that give keys and values

>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}

If the key is a simple string, it may be easier to specify the pair with the keyword argument.

>>> dict(sape=4139, guido=4127, jack=4098)
{'sape': 4139, 'guido': 4127, 'jack': 4098}

Loop technique

When looping through a dictionary, you can use the ʻitems ()` method to get the key and its corresponding value at the same time.

>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
...     print(k, v)
...
gallahad the pure
robin the brave

When looping through a sequence, you can use the ʻenumerate ()` function to get the position index and its corresponding value at the same time.

>>> for i, v in enumerate(['tic', 'tac', 'toe']):
...     print(i, v)
...
0 tic
1 tac
2 toe

If you want to loop over two or more sequences at the same time, you can use the zip () function to pair the entries from both companies.

>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
...     print('What is your {0}? It is {1}.'.format(q, a))
...
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue.

To loop the sequence in reverse order, first specify the sequence in forward order and then call the reversed () function.

>>> for i in reversed(range(1, 10, 2)):
...     print(i)
...
9
7
5
3
1

Use the sorted () function to loop the sequence in sorted order This function does not touch the original sequence and returns a newly sorted list

>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
...     print(f)
...
apple
banana
orange
pear

If you want to modify a list in a loop, it's easier and safer to create a new list

>>> import math
>>> raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]
>>> filtered_data = []
>>> for value in raw_data:
...     if not math.isnan(value):
...             filtered_data.append(value)
...
>>> filtered_data
[56.2, 51.7, 55.3, 52.5, 47.8]

Supplementary information about conditions

--You can use any operator, not just comparison, for the conditions used in while and ʻif statements. --Comparison operators ʻin and not in check if a value exists in the sequence --The operators ʻis and ʻis not compare two objects to see if they are exactly the same.
* Identity matters only for variable objects such as lists. --All comparison operators have equal precedence, all lower than all numeric operators --For example, ʻa <b == c allows you to chain comparisons so that you can determine if a is less than b and b is equal to c. --Comparisons can be combined by the Boolean operators ʻand and ʻor, and the conclusion of the comparison (and all other Boolean expressions) can be negated by not. --In this operator, not has the highest rank and ʻor has the lowest rank, so ʻA and not B or C is equivalent to (A and (not B)) or C. --The Boolean operators ʻand and ʻor are often called ** short-circuit operators **. <br> * The evaluation of the argument (calculation target) is performed from left to right, and the evaluation is stopped when the conclusion is decided. For --If A and C are true, but B is false, ʻA and B and C` does not evaluate expression C. --If a general value is used instead of a Boolean value, the return value of the short circuit operator will be the last evaluated argument.

The results of comparisons and other Boolean expressions can be assigned to variables


>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
>>> non_null = string1 or string2 or string3
>>> non_null
'Trondheim'

Comparison of sequences, comparison of other types

--Sequence objects can be compared to objects with the same sequence type --Use lexicographical order for comparison --Compare the first item verbs, and if the two companies are different, the size is used as a conclusion, and if they are the same, go to the comparison of the second item verb. --If the two item verbs being compared are also of the same sequence type, a lexicographical comparison is made recursively. --Use Unicode code point numbers of individual characters for the dictionary order of strings --Comparing objects of different types with < or > is also a legitimate operation as long as those objects have the appropriate comparison method. --For example, different numeric types are compared by the value of that number, so 0 and 0.0 are equal. --Other than this, the interpreter does not provide friendly ordering and creates a TypeError exception.

the term

In-place

--How to replace the original data --Manipulate the list object directly without making a copy

Shallow copy

--Shallow copy creates a new compound object and then (as much as possible) inserts a reference to the object found in the original object. --Reference: Python standard library 8.10. copy — Shallow copy and deep copy operations

Recommended Posts

[Python tutorial] Data structure
data structure python push pop
[Python tutorial] Control structure tool
Python tutorial
Picture book data structure algorithm Python
Python Django Tutorial (5)
Data analysis python
Python Django Tutorial (8)
Python Django Tutorial (6)
Python internal structure
Python Django Tutorial (7)
Python Django Tutorial (1)
Python Django tutorial tutorial
Python Django Tutorial (3)
[python] Read data
Python Django Tutorial (4)
Python data structure and internal implementation ~ List ~
Python data structure and operation (Python learning memo ③)
Data analysis with python 2
[Docker] Tutorial (Python + php)
Python Django tutorial summary
Python Data Visualization Libraries
Data analysis overview python
Data cleaning using Python
C-like structure in Python
Python data analysis template
Python OpenCV tutorial memo
[Python] Sorting Numpy data
Cloud Run tutorial (python)
Data analysis with Python
Sample data created with python
My python data analysis container
Handle Ambient data in Python
Python for Data Analysis Chapter 4
Display UTM-30LX data in Python
Get Youtube data with python
My python data analytics environment
Python application: data visualization # 2: matplotlib
[Python] Decision Tree Personal Tutorial
Python data analysis learning notes
Python for Data Analysis Chapter 2
Python Django Tutorial Cheat Sheet
Image data type conversion [Python]
Data analysis using python pandas
python> tuple> data, address = s.recvfrom (10000)
Python for Data Analysis Chapter 3
Read json data with python
Get Leap Motion data in Python.
[Translation] scikit-learn 0.18 Tutorial Text data manipulation
EEG analysis in Python: Python MNE tutorial
[Python] How to FFT mp3 data
Development memorandum ~ pandas, forecast, data structure ~
Python Application: Data Cleansing Part 1: Python Notation
Data acquisition using python googlemap api
3D skeleton structure analysis with Python
Python
Read Protocol Buffers data in Python3
Python: Time Series Analysis: Preprocessing Time Series Data
Get data from Quandl in Python
Python Application: Data Handling Part 3: Data Format
Experiment data fitting (Python) being added ...