python數組排序,python數組_Python數組

 2023-11-19 阅读 25 评论 0

摘要: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

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數組排序?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])

python二維數組?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。

python數組 條件,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 ))

python數組長度、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)

python 字符串。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

lambda python。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)

python沒有數組?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)

python的數組怎么用?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])

python中數組,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']]

python定義空數組。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 slicearr1 = 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://hbdhgg.com/1/183275.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息