python中定義數據結構
I remembered the day when I made up my mind to learn python then the very first things I learned about data types and data structures. So in this article, I would like to discuss different data structures in python.
我記得當初下定決心學習python的那一天,然后我才開始學習有關數據類型和數據結構的第一件事。 因此,在本文中,我想討論python中的不同數據結構。
So initially, what is data structures? in a simple world, it is a structure that can hold data, where data refers to a different type of data and related data. overall it is the concept of organizing and storing data so it can be accessed easily and work efficiently.
那么最初,什么是數據結構? 在簡單的世界中,它是一種可以保存數據的結構,其中數據是指不同類型的數據和相關數據。 總的來說,這是組織和存儲數據的概念,因此可以輕松訪問它并有效地工作。
The different types of data structures available in Python are listed below.
下面列出了Python中可用的不同類型的數據結構。
- List 清單
- Tuple 元組
- Set 組
- Dictionary 字典
We will start with List and see the different operations we can do with List. The List is a Mutable data structure in Python that means after the list is created we can perform data manipulations on it like the update, delete, insert operations.
我們將從List開始,然后看看我們可以對List執行的不同操作。 List是Python中的Mutable數據結構,這意味著創建列表后,我們可以對其執行數據操作,如更新,刪除,插入操作。
We can simply create a list of numbers with below syntax.
我們可以簡單地使用以下語法創建數字列表。

Not only numbers we can store string, decimals in the list. we can modify the list also.
不僅數字,我們還可以在列表中存儲字符串和小數。 我們也可以修改列表。

So, we are able to store data in a list, now the question comes how to access those elements of data?.
因此,我們能夠將數據存儲在列表中,現在的問題是如何訪問那些數據元素?
We can access those elements by their index number. To fetch the first element of data we need to use the below syntax.
我們可以通過它們的索引號訪問這些元素。 要獲取數據的第一個元素,我們需要使用以下語法。

Similarly, we can get the second, the third element as required. Now we want to see the last element in a list. We can do this in two methods.
同樣,我們可以根據需要獲取第二個,第三個元素。 現在,我們要查看列表中的最后一個元素。 我們可以用兩種方法做到這一點。
The first method is using the length method to identify the length of the list.
第一種方法是使用length方法來標識列表的長度。

Using length we can find the last index number by subtracting 1 from the length.
使用長度,我們可以通過從長度中減去1來找到最后一個索引號。

See the last element in our myList is extracted. Now we will try with the second method. we need to add “-” (Minus) symbol before index number so we are reading list from the reverse, So we can access the last element by using index number as -1.
看到我們的myList中的最后一個元素被提取。 現在,我們將嘗試第二種方法。 我們需要在索引號之前添加“-”(減號)符號,以便從背面讀取列表,因此我們可以使用索引號為-1來訪問最后一個元素。

Similarly, we can extract the last second or last third number accordingly by changing the index numbers with -2 and -3 respectively.
同樣,我們可以分別通過將索引號更改為-2和-3來提取倒數第??二個或倒數第三個數字。
What if we need to update the list, Here we go. We are going to change the value of 3rd element from “ramu” to “Krishna”.
如果我們需要更新列表,該怎么辦? 我們將把第三個元素的值從“ ramu”更改為“ Krishna”。

see that is simple we can change the list accordingly. Now we add new values to list. To achieve this we will use the append method.
看到很簡單,我們可以相應地更改列表。 現在,我們將新值添加到列表中。 為此,我們將使用append方法。

See we have added new element “Vinod” to list. Now we will see how to delete elements from the list. Again we can do this in 2 ways. The first way is to use the remove method. we need to give the value of elements to be removed. For example, we need to remove the “Vinod” element we just added.
看到我們在列表中添加了新元素“ Vinod”。 現在,我們將看到如何從列表中刪除元素。 同樣,我們可以通過2種方式做到這一點。 第一種方法是使用remove方法。 我們需要給出要刪除的元素的值。 例如,我們需要刪除剛剛添加的“ Vinod”元素。

Note: Make sure you enter the data value correctly. It is case sensitive and keeps an eye on lowercase and uppercase letters.
注意:確保正確輸入數據值。 它區分大小寫,并且注意小寫和大寫字母。
The second way of removing elements is by using the pop method. by default, it will remove the last element in the list.
刪除元素的第二種方法是使用pop方法。 默認情況下,它將刪除列表中的最后一個元素。

Now we will see the extend method in List
現在我們將在List中看到extend方法

we can see a new list named myList2 is added to myList at the end. so using this we can add two lists.
我們可以看到在末尾將一個名為myList2的新列表添加到myList中。 因此,我們可以添加兩個列表。
切片列表 (Slicing in List)
We can slice the list and able to take the required part of the list for our operations. This will come in handy when we prepare our data for machine learning algorithms.
我們可以對列表進行切片,并能夠將列表的必需部分用于我們的操作。 當我們為機器學習算法準備數據時,這將派上用場。
Here is the syntax of using slicing operator.
這是使用切片運算符的語法。
ListName[startindex:endindex:step]
ListName [startindex:endindex:step]
By default step size is 1 and we can change it as per our requirements. we will see a few examples of using this operator.
默認情況下,步長為1,我們可以根據需要進行更改。 我們將看到一些使用此運算符的示例。

Note: The upper boundary is excluded so please keep in mind before giving value to the end index.
注意:上限不包括在內,因此在給最終索引賦值之前請記住。
We can access the list in reverse order and using the below code we can reverse the list in one line of code.
我們可以以相反的順序訪問列表,并使用以下代碼可以在一行代碼中反轉列表。

if we skip the values in start index and end index default values are taken like 0 for start index and length of the list in end index. we gave -1 in step so list starts reading from right to left.
如果我們跳過起始索引和終止索引中的值,則起始索引和終止索引中列表的長度的默認值將像0一樣。 我們給-??1的步數,所以列表從右到左開始讀取。
Reverse Method
反轉法
We can reverse the list using the reverse method. we will make a list and try to use the reverse method and print the list again. now we can see our elements in list are in reverse order.
我們可以使用反向方法來反向列表。 我們將列出一個列表,并嘗試使用相反的方法并再次打印該列表。 現在我們可以看到列表中的元素是相反的順序。

清單復制 (List Copying)
We can copy the list in 2 different ways. Technically speaking they are shallow copy and deep copy. We will see the difference between them.
我們可以通過2種不同的方式復制列表。 從技術上講,它們是淺復制和深復制。 我們將看到它們之間的區別。
淺拷貝。 (Shallow Copy.)
In this, we will create another list but the problem is both lists are pointing to the same memory location. any change in the one list will reflect in both the lists. so we are not maintaining a copy of the list but we are having one list with two different names. Please see the below code.
在此,我們將創建另一個列表,但問題是兩個列表都指向相同的內存位置。 一個列表中的任何更改都將反映在兩個列表中。 因此,我們沒有維護列表的副本,但是擁有一個帶有兩個不同名稱的列表。 請參見下面的代碼。

hereafter creating otherList, we tried to change the 2nd index element to “Cat”. we saw this by printing otherList, all good till here. Now we are printing the actual List myList, even here we can see the “Cat”, we lost “Apple”. so this is not a copy but 2 different names for one list, both the lists are pointing to the same memory location. To overcome this problem and able to store the previous list we have to go for Deep Copy.
在創建otherList之后,我們嘗試將第二個索引元素更改為“ Cat”。 我們通過打印otherList看到了這一點,直到這里一切都很好。 現在我們正在打印實際的List myList,即使在這里我們可以看到“ Cat”,也丟失了“ Apple”。 因此,這不是副本,而是一個列表的2個不同名稱,兩個列表都指向相同的內存位置。 為了克服此問題并能夠存儲先前的列表,我們必須使用Deep Copy。
深拷貝。 (Deep Copy.)
In this case, we are having a copy of list, they are not linked to each other and they are pointing to different memory locations. so any change in one list will not affect other lists, doing this we ensure no data loss.
在這種情況下,我們有一個列表的副本,它們沒有彼此鏈接,并且指向不同的存儲位置。 因此,一個列表中的任何更改都不會影響其他列表,因此我們確保不會丟失任何數據。

Now we tried to change the element in otherList, but it doesn’t affect myList. Doing this we can have a copy of List.
現在,我們嘗試更改otherList中的元素,但它不會影響myList。 這樣做,我們可以獲得List的副本。
By this, we came to the end of the List data structure and methods we can use on it. In my next article, we will discuss other data structures in python like Tuple, Set, Dictionaries, Strings.
至此,我們來到了List數據結構和可以在其上使用的方法的結尾。 在我的下一篇文章中,我們將討論python中的其他數據結構,例如元組,集合,字典,字符串。
Please feel to share or comment if there are any mistakes/queries. I hope you all enjoyed reading this article. Please do mention if went wrong somewhere, I am still learning and suggestions will improve my articles.
如果有任何錯誤/查詢,請分享或發表評論。 我希望大家都喜歡閱讀本文。 請提一下如果某個地方出了問題,我仍在學習,建議會改善我的文章。
Thank you.
謝謝。
Stay Safe.
注意安全。
翻譯自: https://medium.com/analytics-vidhya/data-structures-in-python-d33e6d82d740
python中定義數據結構
本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。 如若轉載,請注明出處:http://www.pswp.cn/news/389117.shtml 繁體地址,請注明出處:http://hk.pswp.cn/news/389117.shtml 英文地址,請注明出處:http://en.pswp.cn/news/389117.shtml
如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!