python中定义数据结构_Python中的数据结构。

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中可用的不同类型的数据结构。

  1. List

    清单
  2. Tuple

    元组
  3. Set

  4. 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.

我们可以简单地使用以下语法创建数字列表。

Image for post

Not only numbers we can store string, decimals in the list. we can modify the list also.

不仅数字,我们还可以在列表中存储字符串和小数。 我们也可以修改列表。

Image for post

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.

我们可以通过它们的索引号访问这些元素。 要获取数据的第一个元素,我们需要使用以下语法。

Image for post

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方法来标识列表的长度。

Image for post

Using length we can find the last index number by subtracting 1 from the length.

使用长度,我们可以通过从长度中减去1来找到最后一个索引号。

Image for post

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来访问最后一个元素。

Image for post

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”。

Image for post

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方法。

Image for post

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”元素。

Image for post

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方法。 默认情况下,它将删除列表中的最后一个元素。

Image for post

Now we will see the extend method in List

现在我们将在List中看到extend方法

Image for post

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,我们可以根据需要进行更改。 我们将看到一些使用此运算符的示例。

Image for post

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.

我们可以以相反的顺序访问列表,并使用以下代码可以在一行代码中反转列表。

Image for post

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.

我们可以使用反向方法来反向列表。 我们将列出一个列表,并尝试使用相反的方法并再次打印该列表。 现在我们可以看到列表中的元素是相反的顺序。

Image for post

清单复制 (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.

在此,我们将创建另一个列表,但问题是两个列表都指向相同的内存位置。 一个列表中的任何更改都将反映在两个列表中。 因此,我们没有维护列表的副本,但是拥有一个带有两个不同名称的列表。 请参见下面的代码。

Image for post

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.

在这种情况下,我们有一个列表的副本,它们没有彼此链接,并且指向不同的存储位置。 因此,一个列表中的任何更改都不会影响其他列表,因此我们确保不会丢失任何数据。

Image for post

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中定义数据结构

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值