python算法和数据结构_Python中的数据结构和算法
python算法和數(shù)據(jù)結(jié)構(gòu)
To
至
Leonardo da Vinci
達(dá)芬奇(Leonardo da Vinci)
介紹 (Introduction)
The purpose of this article is to give you a panorama of data structures and algorithms in Python. This topic is very important for a Data Scientist in order to help him or her to design and solve machine learning models in a more effective way.
本文的目的是為您提供Python數(shù)據(jù)結(jié)構(gòu)和算法的全景圖。 為了幫助數(shù)據(jù)科學(xué)家以更有效的方式設(shè)計(jì)和求解機(jī)器學(xué)習(xí)模型,該主題對(duì)于數(shù)據(jù)科學(xué)家而言非常重要。
We will see together with practical examples the built-in data structures, the user-defined data structures, and last but not least I will introduce you to some algorithms like traversal algorithms, sorting algorithms, and searching algorithms.
我們將與實(shí)際示例一起查看內(nèi)置數(shù)據(jù)結(jié)構(gòu),用戶定義的數(shù)據(jù)結(jié)構(gòu),最后但并非最不重要的一點(diǎn)是,我將向您介紹一些算法,例如遍歷算法,排序算法和搜索算法。
So, let’s get started!
所以,讓我們開始吧!
第一部分:導(dǎo)入數(shù)據(jù)結(jié)構(gòu) (Part I: Bult-in Data Structures)
As the name suggests, data structures allow us to organize, store, and manage data for efficient access and modification.
顧名思義,數(shù)據(jù)結(jié)構(gòu)使我們能夠組織,存儲(chǔ)和管理數(shù)據(jù),以進(jìn)行有效的訪問和修改。
In this part, we are going to take a look at built-in data structures. There are four types of built-in data structures in Python: list, tuple, set, and dictionary.
在這一部分中,我們將研究?jī)?nèi)置數(shù)據(jù)結(jié)構(gòu)。 Python中有四種類型的內(nèi)置數(shù)據(jù)結(jié)構(gòu):列表,元組,集合和字典。
List
清單
A list is defined using square brackets and holds data that is separated by commas. The list is mutable and ordered. It can contain a mix of different data types.
列表使用方括號(hào)定義,并包含用逗號(hào)分隔的數(shù)據(jù)。 該列表是可變的和有序的。 它可以包含不同數(shù)據(jù)類型的混合。
out:
出:
january['january', 'february', 'march', 'april', 'may', 'june', 'july']['birthday', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']Below there are some useful functions for the list.
下面是該列表的一些有用功能。
out:
出:
Whatis
your
favourite
painting
?Who-is-your-favourite-artist-?
out:
出:
['Chagall', 'Kandinskij', 'Dalí', 'da Vinci', 'Picasso', 'Warhol', 'Basquiat']Tuple
元組
A tuple is another container. It is a data type for immutable ordered sequences of elements. Immutable because you can’t add and remove elements from tuples, or sort them in place.
元組是另一個(gè)容器。 它是元素的不可變有序序列的數(shù)據(jù)類型。 不可變,因?yàn)槟鸁o法在元組中添加和刪除元素,也無法對(duì)其進(jìn)行排序。
out:
出:
The dimensions are 7 x 3 x 1Set
組
Set is a mutable and unordered collection of unique elements. It can permit us to remove duplicate quickly from a list.
Set是可變且無序的唯一元素集合。 它可以允許我們從列表中快速刪除重復(fù)項(xiàng)。
out:
出:
{1, 2, 3, 5, 6}False
Basquiat
Dictionary
字典
Dictionary is a mutable and unordered data structure. It permits storing a pair of items (i.e. keys and values).
字典是一種可變且無序的數(shù)據(jù)結(jié)構(gòu)。 它允許存儲(chǔ)一對(duì)項(xiàng)目(即鍵和值)。
As the example below shows, in the dictionary, it is possible to include containers into other containers to create compound data structures.
如下例所示,在字典中,可以將容器包含在其他容器中以創(chuàng)建復(fù)合數(shù)據(jù)結(jié)構(gòu)。
out:
出:
In a Sentimental MoodLacrimosa
第二部分:用戶定義的數(shù)據(jù)結(jié)構(gòu) (Part II: User-Defined Data Structures)
Now I will introduce you three user-defined data structures: ques, stack, and tree. I assume that you have a basic knowledge of classes and functions.
現(xiàn)在,我將向您介紹三種用戶定義的數(shù)據(jù)結(jié)構(gòu):ques,stack和tree。 我假設(shè)您具有有關(guān)類和函數(shù)的基本知識(shí)。
Stack using arrays
使用數(shù)組堆疊
The stack is a linear data structure where elements are arranged sequentially. It follows the mechanism L.I.F.O which means last in first out. So, the last element inserted will be removed as the first. The operations are:
堆棧是線性數(shù)據(jù)結(jié)構(gòu),其中元素按順序排列。 它遵循LIFO機(jī)制,即先進(jìn)先出。 因此,插入的最后一個(gè)元素將被刪除為第一個(gè)元素。 操作是:
- Push → inserting an element into the stack 按下→將元素插入堆棧
- Pop → deleting an element from the stack 彈出→從堆棧中刪除元素
The conditions to check:
檢查條件:
- overflow condition → this condition occurs when we try to put one more element into a stack that is already having maximum elements. 溢出條件→當(dāng)我們嘗試將一個(gè)以上的元素放入已經(jīng)具有最大元素的堆棧中時(shí),就會(huì)發(fā)生這種情況。
- underflow condition →this condition occurs when we try to delete an element from an empty stack. 下溢條件→當(dāng)我們嘗試從空堆棧中刪除元素時(shí),將發(fā)生這種情況。
out:
出:
5True
[10, 23, 25, 27, 11]
overflow
11
27
25
23
10
underflow
Queue using arrays
使用數(shù)組排隊(duì)
The queue is a linear data structure where elements are in a sequential manner. It follows the F.I.F.O mechanism that means first in first out. Think when you go to the cinema with your friends, as you can imagine the first of you that give the ticket is also the first that step out of the line. The mechanism of the queue is the same.
隊(duì)列是線性數(shù)據(jù)結(jié)構(gòu),其中元素按順序排列。 它遵循先進(jìn)先出的先進(jìn)先出機(jī)制。 想想當(dāng)您和朋友一起去電影院時(shí),您可以想象到,第一個(gè)出票的人也是第一個(gè)跳出界限的人。 隊(duì)列的機(jī)制是相同的。
Below the aspects that characterize a queue.
在表征隊(duì)列的方面之下。
Two ends:
兩端:
- front → points to starting element 前面→指向起始元素
- rear → points to the last element 后→指向最后一個(gè)元素
There are two operations:
有兩個(gè)操作:
- enqueue → inserting an element into the queue. It will be done at the rear. 入隊(duì)→將元素插入隊(duì)列。 它將在后部完成。
- dequeue → deleting an element from the queue. It will be done at the front. 出隊(duì)→從隊(duì)列中刪除元素。 它將在前面完成。
There are two conditions:
有兩個(gè)條件:
- overflow → insertion into a queue that is full 溢出→插入已滿的隊(duì)列
- underflow → deletion from the empty queue 下溢→從空隊(duì)列中刪除
out:
出:
[2, 3, 4, 5][3, 4, 5]
Tree (general tree)
樹(普通樹)
Trees are used to define hierarchy. It starts with the root node and goes further down, the last nodes are called child nodes.
樹用于定義層次結(jié)構(gòu)。 它從根節(jié)點(diǎn)開始,然后向下延伸,最后一個(gè)節(jié)點(diǎn)稱為子節(jié)點(diǎn)。
In this article, I focus on the binary tree. The binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child. Below you can see a representation and an example of the binary tree with python where I constructed a class called Node and the objects that represent the different nodes( A, B, C, D, and E).
在本文中,我重點(diǎn)介紹二叉樹。 二叉樹是一種樹數(shù)據(jù)結(jié)構(gòu),其中每個(gè)節(jié)點(diǎn)最多具有兩個(gè)子節(jié)點(diǎn),稱為左子節(jié)點(diǎn)和右子節(jié)點(diǎn)。 在下面,您可以看到python二進(jìn)制樹的表示形式和示例,在其中構(gòu)造了一個(gè)名為Node的類,并表示了代表不同節(jié)點(diǎn)(A,B,C,D和E)的對(duì)象。
image by author圖片作者Anyway, there are other user-defined data structures like linked lists and graphs.
無論如何,還有其他用戶定義的數(shù)據(jù)結(jié)構(gòu),例如鏈表和圖形。
第三部分:算法 (Part III: Algorithms)
The concept of the algorithm has existed since antiquity. In fact, the ancient Egyptians used algorithms to solve their problems. Then they taught this approach to the Greeks.
自上古以來,算法的概念就存在了。 實(shí)際上,古埃及人使用算法來解決他們的問題。 然后他們向希臘人教授了這種方法。
The word algorithm derives itself from the 9th-century Persian mathematician Mu?ammad ibn Mūsā al-Khwārizmī, whose name was Latinized as Algorithmi. Al-Khwārizmī was also an astronomer, geographer, and a scholar in the House of Wisdom in Baghdad.
算法一詞源于9世紀(jì)的波斯數(shù)學(xué)家Mu?ammadibnMūsāal-Khwārizmī ,其名稱被拉丁化為Algorithmi。 Al-Khwārizmī還是天文學(xué)家,地理學(xué)家,也是巴格達(dá)智慧之家的學(xué)者。
As you already know algorithms are instructions that are formulated in a finite and sequential order to solve problems.
如您所知,算法是按有限順序排列的指令來解決問題。
When we write an algorithm, we have to know what is the exact problem, determine where we need to start and stop and formulate the intermediate steps.
在編寫算法時(shí),我們必須知道確切的問題是什么,確定需要在哪里開始和停止以及制定中間步驟。
There are three main approaches to solve algorithms:
有三種主要的算法求解方法:
- Divide et Impera (also known as divide and conquer) → it divides the problem into sub-parts and solves each one separately Divide et Impera(也稱為“分而治之”)→將問題分為幾個(gè)部分,分別解決每個(gè)問題
- Dynamic programming → it divides the problem into sub-parts remembers the results of the sub-parts and applies it to similar ones 動(dòng)態(tài)編程→將問題劃分為多個(gè)子部分,記住子部分的結(jié)果并將其應(yīng)用于相似的部分
- Greedy algorithms → involve taking the easiest step while solving a problem without worrying about the complexity of the future steps 貪婪算法→包括在解決問題的同時(shí)采取最簡(jiǎn)單的步驟,而無需擔(dān)心未來步驟的復(fù)雜性
Tree Traversal Algorithm
樹遍歷算法
Trees in python are non-linear data structures. They are characterized by roots and nodes. I take the class I constructed before for the binary tree.
python中的樹是非線性數(shù)據(jù)結(jié)構(gòu)。 它們的特征是根和節(jié)點(diǎn)。 我采用之前為二叉樹構(gòu)造的類。
Tree Traversal refers to visiting each node present in the tree exactly once, in order to update or check them.
樹遍歷是指只訪問樹中存在的每個(gè)節(jié)點(diǎn)一次,以更新或檢查它們。
image by author圖片作者There are three types of tree traversals:
有三種類型的樹遍歷:
- In-order traversal → refers to visiting the left node, followed by the root and then the right nodes. 有序遍歷→指先訪問左節(jié)點(diǎn),然后依次訪問根節(jié)點(diǎn)和右節(jié)點(diǎn)。
Here D is the leftmost node where the nearest root is B. The right of root B is E. Now the left sub-tree is completed, so I move towards the root node A and then to node C.
這里D是最左邊的節(jié)點(diǎn),其中最近的根是B。根B的右邊是E。現(xiàn)在左側(cè)的子樹已完成,因此我朝根節(jié)點(diǎn)A移動(dòng),然后向節(jié)點(diǎn)C移動(dòng)。
out:
出:
DB
E
A
C
- Pre-order traversal → refers to visiting the root node followed by the left nodes and then the right nodes. 順序遍歷→是指先訪問根節(jié)點(diǎn),然后再訪問左節(jié)點(diǎn),再訪問右節(jié)點(diǎn)。
In this case, I move to the root node A and then to the left child node B and to the sub child node D. After that I can go to the nodes E and then C.
在這種情況下,我先移至根節(jié)點(diǎn)A,然后移至左子節(jié)點(diǎn)B,再移至子子節(jié)點(diǎn)D。之后,我可以先移至節(jié)點(diǎn)E,然后移至C。
out:
出:
AB
D
E
C
- Post-order traversal → refers to visiting the left nodes followed by the right nodes and then the root node 后順序遍歷→是指先訪問左側(cè)節(jié)點(diǎn),然后再訪問右側(cè)節(jié)點(diǎn),然后再訪問根節(jié)點(diǎn)
I go to the most left node which is D and then to the right node E. Then, I can go from the left node B to the right node C. Finally, I move towards the root node A.
我先去最左邊的節(jié)點(diǎn)D,再去右邊的節(jié)點(diǎn)E。然后,我可以從左邊的節(jié)點(diǎn)B到右邊的節(jié)點(diǎn)C。最后,我向根節(jié)點(diǎn)A移動(dòng)。
out:
出:
DE
B
C
A
Sorting Algorithm
排序算法
The sorting algorithm is used to sort data in some given order. It can be classified in Merge Sort and Bubble Sort.
排序算法用于按給定順序?qū)?shù)據(jù)進(jìn)行排序。 可以分為合并排序和氣泡排序。
Merge Sort → it follows the divide et Impera rule. The given list is first divided into smaller lists and compares adjacent lists and then, reorders them in the desired sequence. So, in summary from unordered elements as input, we need to have ordered elements as output. Below, the code with each step described.
合并排序→遵循除法和Impera規(guī)則 。 給定的列表首先被分成較小的列表,并比較相鄰列表,然后按所需順序?qū)ζ溥M(jìn)行重新排序。 因此,總之,無序元素作為輸入,我們需要有序元素作為輸出。 下面,用每個(gè)步驟描述代碼。
out:
出:
input - unordered elements: 15 1 19 93output - ordered elements:
[1, 15, 19, 93]
- Bubble Sort → it first compares and then sorts adjacent elements if they are not in the specified order. 冒泡排序→如果不按指定順序?qū)ο噜徳剡M(jìn)行排序,則首先進(jìn)行比較,然后對(duì)它們進(jìn)行排序。
out:
出:
[1, 3, 9, 15]- Insertion Sort → it picks one item of a given list at the time and places it at the exact spot where it is to be placed. 插入排序→它會(huì)同時(shí)選擇給定列表中的一項(xiàng)并將其放置在要放置的確切位置。
out:
出:
[1, 3, 9, 15]There are other Sorting Algorithms like Selection Sort and Shell Sort.
還有其他排序算法,例如選擇排序和外殼排序 。
Searching Algorithms
搜索算法
Searching algorithms are used to seek for some elements present in a given dataset. There are many types of search algorithms such as Linear Search, Binary Search, Exponential Search, Interpolation Search, and so on. In this section, we will see the Linear Search and Binary Search.
搜索算法用于尋找給定數(shù)據(jù)集中存在的某些元素。 有很多類型的搜索算法,例如線性搜索,二進(jìn)制搜索,指數(shù)搜索,插值搜索等。 在本節(jié)中,我們將看到線性搜索和二進(jìn)制搜索。
- Linear Search → in a single-dimensional array we have to search a particular key element. The input is the group of elements and the key element that we want to find. So, we have to compare the key element with each element of the group. In the following code, I try to seek element 27 in our list. 線性搜索→在一維數(shù)組中,我們必須搜索特定的關(guān)鍵元素。 輸入是元素組和我們要查找的關(guān)鍵元素。 因此,我們必須將關(guān)鍵元素與組中的每個(gè)元素進(jìn)行比較。 在下面的代碼中,我嘗試在列表中查找元素27。
out:
出:
'not fund'- Binary Search → in this algorithm, we assume that the list is in ascending order. So, if the value of the search key is less than the element in the middle of the list, we narrow the interval to the lower half. Otherwise, we narrow to the upper half. We continue our check until the value is found or the list is empty. 二進(jìn)制搜索→在此算法中,我們假定列表按升序排列。 因此,如果搜索關(guān)鍵字的值小于列表中間的元素,則將間隔縮小到下半部分。 否則,我們縮小到上半部分。 我們繼續(xù)檢查,直到找到該值或列表為空。
out:
出:
FalseTrue
結(jié)論 (Conclusion)
Now you have an overview of data structures and algorithms. So, you can start going to a deeper understanding of algorithms.
現(xiàn)在,您將概述數(shù)據(jù)結(jié)構(gòu)和算法。 因此,您可以開始對(duì)算法進(jìn)行更深入的了解。
The beautiful image of the Vitruvian Man I have chosen for this article is not casual. The drawing is based on the correlation of the ideal human body in relation to geometry. In fact, for this representation, Leonardo da Vinci was inspired by Vitruvius who described the man’s body as the ideal body to determine the correct proportion in architecture.
我為本文選擇的維特魯威人的美麗形象并非隨隨便便。 該圖基于理想人體與幾何體的相關(guān)性。 實(shí)際上,對(duì)于這種表示形式,達(dá)芬奇(Leonardo da Vinci)的靈感來自維特魯威(Vitruvius) ,他將男人的身體描述為理想的身體,可以確定建筑中正確的比例。
For what concerns algorithms, the Vitruvian Man hides a secret algorithm used by the artists for centuries to certify that their works were inspired by the divine proportion.
關(guān)于算法,《維特魯威人》(Vitruvian Man)隱藏了藝術(shù)家?guī)讉€(gè)世紀(jì)以來一直在使用的秘密算法,以證明他們的作品是受神圣比例啟發(fā)的。
Sometimes I like to think that maybe Leonardo da Vinci, through his wonderful works, wanted to define the most important algorithm which is the algorithm of life.
有時(shí)我想認(rèn)為達(dá)芬奇(Leonardo da Vinci)通過他的出色著作想要定義最重要的算法,即生命算法。
Thanks for reading this. There are some other ways you can keep in touch with me and follow my work:
感謝您閱讀本文。 您可以通過其他方法與我保持聯(lián)系并關(guān)注我的工作:
Subscribe to my newsletter.
訂閱我的時(shí)事通訊。
You can also get in touch via my Telegram group, Data Science for Beginners.
您也可以通過我的電報(bào)小組“ 面向初學(xué)者的數(shù)據(jù)科學(xué)”進(jìn)行聯(lián)系 。
翻譯自: https://towardsdatascience.com/data-structures-algorithms-in-python-68c8dbb19c90
python算法和數(shù)據(jù)結(jié)構(gòu)
總結(jié)
以上是生活随笔為你收集整理的python算法和数据结构_Python中的数据结构和算法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 孕妇梦到吃粽子预示着男还是女
- 下一篇: python dash_Dash是Dat