数组 python_python没有数组

数组 python_python没有数组python数组PythonArraycontainsasequenceofdata.Inpythonprogramming,thereisnoexclusivearrayobjectbecausewecanperformallthearrayoperationsusinglist.Todaywewilllearnaboutpython…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

python数组

Python Array contains a sequence of data. In python programming, there is no exclusive array object because we can perform all the array operations using list. Today we will learn about python array and different operations we can perform on an array (list) in python. I will assume that you have the basic idea of python variables and python data types.

Python Array包含一系列数据。 在python编程中,没有排他的数组对象,因为我们可以使用list执行所有数组操作。 今天,我们将学习python数组以及可以在python中的数组(列表)上执行的不同操作。 我将假定您具有python变量和python数据类型的基本概念。

Python数组 (Python Array)

Python supports all the array related operations through its list object. Let’s start with one-dimensional array initialization.

Python通过其list对象支持所有与数组相关的操作。 让我们从一维数组初始化开始。

Python数组示例 (Python array example)

Python array elements are defined within the brace [] and they are comma separated. The following is an example of declaring python one-dimensional array.

Python数组元素在大括号[]中定义,并且用逗号分隔。 以下是声明python一维数组的示例。

arr = [ 1, 2 ,3, 4, 5]
print (arr)
print (arr[2])
print (arr[4])

Output of above one dimensional array example program will be:

上面的一维数组示例程序的输出将是:

[1, 2, 3, 4, 5]
3
5

Array indexing starts from 0. So the value of index 2 of variable arr is 3.

数组索引从0开始。因此变量arr的索引2的值为3。

In some other programming languages such as Java, when we define an array we also need to define element type, so we are limited to store only that type of data in the array. For example, int brr[5]; is able to store integer data only.

在Java等其他编程语言中,当我们定义数组时,我们还需要定义元素类型,因此我们只能在数组中存储该类型的数据。 例如, int brr[5]; 只能存储整数数据。

But python gives us the flexibility to have the different type of data in the same array. It’s cool, right? Let’s see an example.

但是python使我们可以灵活地在同一数组中拥有不同类型的数据。 很酷吧? 让我们来看一个例子。

student_marks = ['Akkas' , 45, 36.5]
marks = student_marks[1]+student_marks[2]
print(student_marks[0] + ' has got in total = %d + %f = %f ' % (student_marks[1], student_marks[2], marks ))

It give the following output:

它给出以下输出:

Akkas has got in total = 45 + 36.500000 = 81.500000 marks

In the above example you can see that, student_marks array have three type of data – string, int and float.

在上面的示例中,您可以看到, student_marks数组具有三种类型的数据-字符串,整数和浮点数。

Python多维数组 (Python multidimensional array)

Two dimensional array in python can be declared as follows.

python中的二维数组可以声明如下。

arr2d = [ [1,3,5] ,[2,4,6] ]
print(arr2d[0]) # prints elements of row 0
print(arr2d[1]) # prints elements of row 1
print(arr2d[1][1]) # prints element of row = 1, column = 1

It will produce the following output:

它将产生以下输出:

[1, 3, 5]                                                                                                                                                                       
[2, 4, 6]                                                                                                                                                                       
4

Similarly, we can define a three-dimensional array or multidimensional array in python.

同样,我们可以在python中定义三维数组或多维数组。

Python阵列范例 (Python array examples)

Now that we know how to define and initialize an array in python. We will look into different operations we can perform on a python array.

现在,我们知道了如何在python中定义和初始化数组。 我们将研究可以在python数组上执行的不同操作。

使用for循环遍历Python数组 (Python array traversing using for loop)

We can use for loop to traverse through elements of an array. Below is a simple example of for loop to traverse through an array.

我们可以使用for循环遍历数组的元素。 以下是for循环遍历数组的简单示例。

arrayElement = ["One", 2, 'Three' ]
for i in range(len(arrayElement)):
   print(arrayElement[i])

Below image shows the output produced by the above array example program.

下图显示了上述数组示例程序产生的输出。

使用for循环遍历2D数组 (Traversing 2D-array using for loop)

The following code print the elements row-wise then the next part prints each element of the given array.

下面的代码按行打印元素,然后下一部分打印给定数组的每个元素。

arrayElement2D = [ ["Four", 5, 'Six' ] , [ 'Good',  'Food' , 'Wood'] ]
for i in range(len(arrayElement2D)):
   print(arrayElement2D[i])

for i in range(len(arrayElement2D)):
   for j in range(len(arrayElement2D[i])):
       print(arrayElement2D[i][j])

This will output:

python array example for loop traversing 2d array

这将输出:

Python数组追加 (Python array append)

arrayElement = ["One", 2, 'Three' ]
arrayElement.append('Four')
arrayElement.append('Five')
for i in range(len(arrayElement)):
   print(arrayElement[i])

The new element Four and Five will be appended at the end of the array.

新元素“四”和“五”将添加到数组的末尾。

One
2
Three
Four
Five

You can also append an array to another array. The following code shows how you can do this.

您也可以将一个数组附加到另​​一个数组。 以下代码显示了如何执行此操作。

arrayElement = ["One", 2, 'Three' ]
newArray = [ 'Four' , 'Five']
arrayElement.append(newArray);
print(arrayElement)
['One', 2, 'Three', ['Four', 'Five']]

Now our one-dimensional array arrayElement turns into a multidimensional array.

现在,我们的一维数组arrayElement变成了多维数组。

Python数组大小 (Python array size)

We can use len function to determine the size of an array. Let’s look at a simple example for python array length.

我们可以使用len函数来确定数组的大小。 让我们看一个简单的python数组长度示例。

arr = ["One", 2, 'Three' ]

arr2d = [[1,2],[1,2,3,4]]

print(len(arr))
print(len(arr2d))
print(len(arr2d[0]))
print(len(arr2d[1]))

Python数组切片 (Python array slice)

Python provides a special way to create an array from another array using slice notation. Let’s look at some python array slice examples.

Python提供了一种特殊的方式来使用切片符号从另一个数组创建一个数组。 让我们看一些python数组切片示例。

arr = [1,2,3,4,5,6,7]

#python array slice

arr1 = arr[0:3] #start to index 2
print(arr1)

arr1 = arr[2:] #index 2 to end of arr
print(arr1)

arr1 = arr[:3] #start to index 2
print(arr1)

arr1 = arr[:] #copy of whole arr
print(arr1)

arr1 = arr[1:6:2] # from index 1 to index 5 with step 2
print(arr1)

Below image shows the python array slice example program output.

下图显示了python array slice示例程序输出。

Python数组插入 (Python array insert)

We can insert an element in the array using insert() function.

我们可以使用insert()函数在数组中插入一个元素。

arr = [1,2,3,4,5,6,7]

arr.insert(3,10)

print(arr)

Python数组弹出 (Python array pop)

We can call the pop function on the array to remove an element from the array at the specified index.

我们可以在数组上调用pop函数,以指定索引从数组中删除元素。

arr = [1,2,3,4,5,6,7]

arr.insert(3,10)
print(arr)

arr.pop(3)
print(arr)

That’s all about python array and different operations we can perform for the arrays in python.

这就是关于python数组以及我们可以在python中为数组执行的不同操作的全部内容。

翻译自: https://www.journaldev.com/14971/python-array

python数组

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/170322.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 对城市公交系统思考

    对城市公交系统思考

    2021年9月9日
    66
  • 删除多选框选中商品的内容_快速选择工具怎么删除选中部分吗

    删除多选框选中商品的内容_快速选择工具怎么删除选中部分吗多选框定义多选框的出现时将商品循环多次出现,这里用到了el表达式和jstl标签库的foreEach标签,注意input框加上class属性,并加上存有id的属性value:forEachitems=”${productList}”var=”product”varStatus=”vs”>type=”checkbox”class=”check_pid”name=”pid”

    2025年5月31日
    2
  • idea git 使用(idea开发工具怎么使用)

    简介以下会介绍Git在IDEA中的使用,包含大多数的开发场景,这里是用Github做远程仓库,假设小组中有两个人,队长A,和队员B场景一:队长A创建项目并提交到远程Git仓库场景二:队员B从远程Git仓库上获取项目源码场景三:队员B修改了部分源码,提交到远程仓库场景四:队长A从远程仓库获取队员B的提交场景五:队员B接受了一个新功能的任务,创建了一个分支并在分支上开发场景六:队员B把…

    2022年4月17日
    53
  • http错误状态码_HTTP常用的14种状态码

    http错误状态码_HTTP常用的14种状态码一些常见的状态码为:200-服务器成功返回网页404-请求的网页不存在503-服务不可用详细分解:1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码。代码说明100(继续)请求者应当继续提出请求。服务器返回此代码表示已收到请求的第一部分,正在等待其余部分。101(切换协议)请求者已要求服务器切换协议,服务器已确认并准备切换。…

    2022年9月28日
    3
  • Okio基本使用以及源码分析

    Okio基本使用以及源码分析Okio是什么在OkHttp的源码中经常能看到Okio的身影,所以单独拿出来学习一下,作为OkHttp的低层IO库,Okio确实比传统的java输入输出流读写更加方便高效。Okio补充了java.io和java.nio的不足,使访问、存储和处理数据更加容易,它起初只是作为OKHttp的一个组件,现在你可以独立的使用它来解决一些IO问题。先看下okio库中类之间的关系:okio中最关键的是对于缓存队列的管理,这些优化操作使得okio在复制数据的时候可以减少拷贝次数,来看下okio中数据保存的数据结构是

    2022年5月27日
    40
  • java jersey使用总结_Java Jersey2使用总结

    java jersey使用总结_Java Jersey2使用总结前言在短信平台一期工作中,为便于移动平台的开发,使用了JavaJersey框架开发RESTFul风格的WebService接口。在使用的过程中发现了一些问题并积累了一些项目经验,做了一下总结,便于个人成长,同时也希望对有需要的同仁有好的借鉴和帮助。简介Jersey是JAX-RS(JSR311)开源参考实现用于构建RESTfulWebservice,它包含三个部分:核心服务器(CoreS…

    2022年7月12日
    14

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号