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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/389117.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python实训英文_GitHub - MiracleYoung/You-are-Pythonista: 汇聚【Python应用】【Python实训】【Python技术分享】等等...

You-are-Pythonista汇聚【从零单排】【实战项目】【数据科学】【自然语言处理】【计算机视觉】【面试题系列】【大航海】【Python应用】【错题集】【技术沙龙】【内推渠道】等等【人人都是Pythonista】由公众号【Python专栏】推出,请认准唯一标识:请仔细…

java电子商务系统源码 Spring MVC+mybatis+spring cloud+spring boot+spring security

鸿鹄云商大型企业分布式互联网电子商务平台,推出PC微信APP云服务的云商平台系统,其中包括B2B、B2C、C2C、O2O、新零售、直播电商等子平台。 分布式、微服务、云架构电子商务平台 java b2b2c o2o 技术解决方案 开发语言: java、j2ee 数据库&am…

Go语言实现FastDFS分布式存储系统WebAPI网关

前言 工作需要,第一次使用 Go 来实战项目。 需求:采用 golang 实现一个 webapi 的中转网关,将一些资源文件通过 http 协议上传至 FastDFS 分布式文件存储系统。 一、FastDFS 与 golang 对接的代码 github:https://github.com/weil…

builder 模式

首先提出几个问题: 什么是Builder模式?为什么要使用Builder模式?它的优点是什么,那缺点呢?什么情况下使用Builder模式? 关于Builder模式在代码中用的很多,比如AlertDialog, OkHttpClient等。一…

工作失职的处理决定_工作失职的处理决定

精品文档2016全新精品资料-全新公文范文-全程指导写作–独家原创1/3工作失职的处理决定失职是指工作人员对本职工作不认真负责,未依照规定履行自己的职务,致使单位或服务对象造成损失的行为。关于工作失职的处理决定该怎么写呢?下面学习啦小编给大家带来…

venn diagram_Venn Diagram Python软件包:Vennfig

venn diagram目录 (Table of Contents) Introduction 介绍 Installation 安装 Default Functions 默认功能 Parameters 参量 Examples 例子 Conclusion 结论 介绍 (Introduction) In the last article, I showed how to draw basic Venn diagrams using matplotlib_venn.在上一…

应用程序的主入口点应用程序的主入口点应用程序的主入口点

/// <summary>/// 应用程序的主入口点。/// </summary>[STAThread]static void Main(string[] args){Stream stream Assembly.GetExecutingAssembly().GetManifestResourceStream("CapApp.TestApp.exe");byte[] bs new byte[stream.Length];stream.Rea…

创梦天地通过聆讯:上半年经营利润1.3亿 腾讯持股超20%

雷帝网 雷建平 11月23日报道时隔半年后&#xff0c;乐逗游戏母公司创梦天地终于通过上市聆讯&#xff0c;这意味着创梦天地很快将在港交所上市。创梦天地联合保荐人包括瑞信、招商证券国际、中金公司。当前&#xff0c;创梦天地运营的游戏包括《梦幻花园》、《快乐点点消》、《…

PyCharm之python书写规范--消去提示波浪线

强迫症患者面对PyCharm的波浪线是很难受的&#xff0c;针对如下代码去除PyCharm中的波浪线&#xff1a; # _*_coding:utf-8_*_ # /usr/bin/env python3 A_user "lin" A_password "lin123"for i in range(3): # 循环次数为3name input("请输入你的…

关于java static 关键字

当我们创建类时会指出哪个类的对象的外观与行为。 一般的流程是用new 创建这个类的对象&#xff0c;然后生成数据的存储空间&#xff0c;并使用相应的方法。 但以下两种情况不太适合这个流程&#xff1a; 只想用一个存储区域来保存一个特定的数据—–无论要创建多少个对象&a…

plotly django_使用Plotly为Django HTML页面进行漂亮的可视化

plotly djangoHello everyone! Recently I had to do some visualizations for my university project, I’ve done some googling and haven’t found any simple guides on how to put Plotly plots on an HTML page.大家好&#xff01; 最近&#xff0c;我不得不为我的大学项…

roce和iwarp_VIA、IB、RDMA、RoCE、iWARP、DPDK的发展与纠缠?

VIA(Virtual Interface Architecture): 这个只是一个标准&#xff0c;基本上不要了解太多。楼主的问题可以细分成2个层次考虑。一个是网络环境&#xff0c;二是具体的协议和实现。一、网络环境IB(InfiniBand): 是一种网络环境&#xff0c;做对比的是以太网, IB往往用于高性能集…

remoting

原文地址&#xff1a;http://blog.csdn.net/chengking/archive/2005/10/26/517349.aspx (一).说明 一个远程调用示例. 此示例实现功能: 客房端调用远程方法&#xff08;远程方法可以弹 出自定义信息&#xff09;&#xff0c;实现发送信息功能. 实现原理概是这样的…

handler 消息处理机制

关于handler消息处理机制&#xff0c;只要一提到&#xff0c;相信作为一个android工程师&#xff0c;脑海就会有这么一个流程 大家都滚瓜烂熟了&#xff0c;但别人问到几个问题&#xff0c;很多人还是栽到这个“烂”上面&#xff0c;比如&#xff1a; 一个线程是如何对应一个L…

es6简单介绍

let和const 原先声明变量的形式 var test 5; //全局变量 function a() {var cc3; //局部变量alert(test); } function b(){alert(test);}test 5;//全局变量 function a() {aa3; //全局变量alert(test); } 在es6之前&#xff0c;作用域只有全局作用域和函数作用域&#xff0…

软件工程方法学要素含义_日期时间数据的要素工程

软件工程方法学要素含义According to Wikipedia, feature engineering refers to the process of using domain knowledge to extract features from raw data via data mining techniques. These features can then be used to improve the performance of machine learning a…

洛谷P1605:迷宫(DFS)

题目背景 迷宫 【问题描述】 给定一个N*M方格的迷宫&#xff0c;迷宫里有T处障碍&#xff0c;障碍处不可通过。给定起点坐标和终点坐标&#xff0c;问: 每个方格最多经过1次&#xff0c;有多少种从起点坐标到终点坐标的方案。在迷宫中移动有上下左右四种方式&#xff0c;每次只…

vue图片压缩不失真_图片压缩会失真?快试试这几个无损压缩神器。

前端通常在做网页的时候 会出现图片加载慢的情况 在这里我通常会将图片进行压缩 但是通常情况下 观众会认为 图片压缩会出现失真的现象 在这里我会向大家推荐几款图片压缩的工具 基本上会实现无损压缩1.TinyPng地址&#xff1a;https://tinypng.comEnglish&#xff1f;不要慌&a…

remoteing2

此示例主要演示了net remoting,其中包含一个服务器程序Server.exe和一个客户端程序CAOClient.exe。客户端程序会通过http channel调用服务器端RemoteType.dll的对象和方法。服务器端的代码文件由下图所述&#xff1a;Server.cs源代码 :using System;using System.Runtime.Remot…

android 线程池

为什么用线程池 创建/销毁线程伴随着系统开销&#xff0c;过于频繁的创建/销毁线程&#xff0c;会很大程度上影响处理效率 例如&#xff1a; 记创建线程消耗时间T1&#xff0c;执行任务消耗时间T2&#xff0c;销毁线程消耗时间T3 如果T1T3>T2&#xff0c;那么是不是说开…