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.

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

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.taodudu.cc/news/show-995023.html

相关文章:

  • plotly django_使用Plotly为Django HTML页面进行漂亮的可视化
  • 软件工程方法学要素含义_日期时间数据的要素工程
  • 数据湖 data lake_在Data Lake中高效更新TB级数据的模式
  • ai对话机器人实现方案_显然地引入了AI —无代码机器学习解决方案
  • 图片中的暖色或冷色滤色片是否会带来更多点击? —机器学习A / B测试
  • 图卷积 节点分类_在节点分类任务上训练图卷积网络
  • 回归分析预测_使用回归分析预测心脏病。
  • aws spark_使用Spark构建AWS数据湖时的一些问题以及如何处理这些问题
  • 数据科学家编程能力需要多好_我们不需要这么多的数据科学家
  • sql优化技巧_使用这些查询优化技巧成为SQL向导
  • 物种分布模型_减少物种分布建模中的空间自相关
  • 清洁数据ploy n_清洁屋数据
  • 基于边缘计算的实时绩效_基于绩效的营销中的三大错误
  • 上凸包和下凸包_使用凸包聚类
  • 决策树有框架吗_决策框架
  • mysql那本书适合初学者_3本书适合初学者
  • 阎焱多少身价_2020年,数据科学家的身价是多少?
  • 卡尔曼滤波滤波方程_了解卡尔曼滤波器及其方程
  • 朴素贝叶斯分类器 文本分类_构建灾难响应的文本分类器
  • Seaborn:Python
  • 销货清单数据_2020年8月数据科学阅读清单
  • 米其林餐厅 盐之花_在世界范围内探索《米其林指南》
  • spotify 数据分析_我的Spotify流历史分析
  • 纹个鸡儿天才小熊猫_给熊猫用户的5个提示
  • 图像离群值_什么是离群值?
  • 数据预处理工具_数据预处理
  • 自考数据结构和数据结构导论_我跳过大学自学数据科学
  • 在PyTorch中转换数据
  • tidb数据库_异构数据库复制到TiDB
  • 刚认识女孩说不要浪费时间_不要浪费时间寻找学习数据科学的最佳方法

python中定义数据结构_Python中的数据结构。相关推荐

  1. python 类中定义类_Python中的动态类定义

    python 类中定义类 Here's a neat Python trick you might just find useful one day. Let's look at how you ca ...

  2. python中定义字符串_Python中的字符串String

    Python中除了数字(Numbers)(int,float,complex)之外,另外一种重要的类型就是字符串. 字符串是字符序列,可以由任何字符构成. 在Python语言中,字符串可以放在单引号( ...

  3. python如何定义类_python中定义类

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 类的定义python中,定义类是通过class关键字,例如我们定义一个存储学生信 ...

  4. python怎样定义数组_python中定义数组的正确方法

    我的模拟器(c++)使用python作为绑定. 在我的c++中,我定义数组如下 这是我的变量声明(在头中)Ptr m_names [2]; 这是我的函数声明(在头中) ^{pr2}$ 在源文件中voi ...

  5. python中定义字符串_python中的字符串

    python中的字符串一旦定义,则不可以修改 python中的原始字符串 即 在字符串前面加小写字母r 比如:打印输出C:\Program Files\Microsoft Games python中的 ...

  6. python怎么定义全局变量_python中如何定义全局变量

    全局变量的用法有两种: 全局变量是编程语言中常见的一种变量,通过全局定义,可以是某对象函数创建,也可以是本程序任何位置创建,能够被本程序中的所有 对象或函数进行引用,全局变量的定义有利于程序的变量共享 ...

  7. python中定义数据结构_Python中的数据结构—简介

    python中定义数据结构 You have multiples algorithms, the steps of which require fetching the smallest value ...

  8. python算法和数据结构_Python中的数据结构和算法

    python算法和数据结构 To 至 Leonardo da Vinci 达芬奇(Leonardo da Vinci) 介绍 (Introduction) The purpose of this ar ...

  9. python 下标 遍历列表_python中的数据结构与算法(1):列表、元组与字符串

    列表是 python 中最常用.最重要的数据结构之一. 一般来说,我们用列表来管理一组数据对象,这些对象可能是同一类型,也可能是不同类型.列表不仅记录了这些数据对象,而且也记录了它们之间的一种顺序关系 ...

最新文章

  1. 「SAP技术」SE16和SE16N修改后台表数据方法
  2. 恐惧、野心和迷茫,机器人公民背后的未来世界
  3. ApplicationId 与 PackageName 的区别
  4. 线结构光平面标定计算算子
  5. Linux下常用软件
  6. camx模型_【推荐】基于CAMx的空气质量模拟及污染来源解析技术
  7. UML在powerDesigner的使用及其相关概念
  8. android 应用退到后台,类似最小化
  9. netty系列之:基于流的数据传输
  10. deepin系统中.txt文件图标显示内容问题_deepin从兴致勃勃到彻底放弃
  11. 模板:二叉搜索树平衡树
  12. cmd窗口使用python提示“Python not found”,可能是环境变量配置的原因
  13. 应用前台省电秘籍——这些常见功耗雷坑不要再跳了
  14. 《C++ Primer》第五版课后习题解答_第二章(1)(01-08)
  15. 高德地图我的队伍查岗_详细测试高德地图的家人地图后 我学会了画地为牢
  16. (转)postgis常用函数介绍(二)
  17. php判断平年和闰年,平年和闰年的三种判断方法
  18. c语言关于函数的程序源代码,c语言库函数源代码
  19. 强大的发包工具fine packet builder
  20. postSQL安装和GIS数据导入

热门文章

  1. 575 div3RGB Substring (hard version)——思维-
  2. 在linux下利用ls命令进行模糊查找
  3. 23. 合并K个排序链表
  4. java改错题技巧,看这篇文章准没错!
  5. Java高级工程师必看系列,已拿到offer
  6. [BZOJ2125]最短路
  7. javascript 自定义Map
  8. Quartz2D知识点聚合案例
  9. ibatis 中 $与#的区别
  10. soapui自带的webservice实例 MockService