Cara menggunakan python dynamic multidimensional array

Array is a data structure used to store elements. An array can only store similar types of elements. A Two Dimensional is defined as an Array inside the Array. The index of the array starts with 0 and ends with a size of array minus 1. We can create ‘n’ number of arrays in an array.

In the above image, we can see that an index uniquely identifies each array element.

In this Python List tutorial, you will learn:

How to Create Array in Python?

We can create a two-dimensional array(list) with rows and columns.

Syntax:

[[r1,r2,r3,..,rn],[c1,c2,c3,.......,cn]]

Where,

r stands for rows and c stands for columns

Example: Following is the example for creating

2D array with 4 rows and 5 columns

array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]] #display print(array)

Output:

[[23, 45, 43, 23, 45], [45, 67, 54, 32, 45], [89, 90, 87, 65, 44], [23, 45, 67, 32, 10]]

Accessing the values

We can access the values using index position

Syntax:

We can get row value using Array[row index][column index]3 operator

array[row index]

We can get column value using Array[row index][column index]4

Array[row index][column index]

where,

  • array is an input array
  •  row index is the row index position starts from 0
  •  column index is the column index position starts from 0 in a row.

Example:

In this example we are going to access the values using index positions

#creare 2D array with 4 rows and 5 columns array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]] #display print(array) #get the first row print(array[0]) #get the third row print(array[2]) #get the first row third element print(array[0][2]) #get the third row forth element print(array[2][3])

Output:

[[23, 45, 43, 23, 45], [45, 67, 54, 32, 45], [89, 90, 87, 65, 44], [23, 45, 67, 32, 10]] [23, 45, 43, 23, 45] [89, 90, 87, 65, 44] 43 65

We can also access elements using Array[row index][column index]5

Syntax:

for rows in the array: for columns in rows: print(columns)

where,

  • rows are used to iterate the row by row
  • columns is used to iterate the values present in each row.

Example:

Creare 2D array with 4 rows and 5 columns array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]] #use for loop to iterate the array for rows in array: for columns in rows: print(columns,end=" ") print()

Output:

23 45 43 23 45 45 67 54 32 45 89 90 87 65 44 23 45 67 32 10

Inserting the values into the two-dimensional array

Here we are going to insert values into two dimensional array using insert() function

Syntax:

array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]] #display print(array) 0

where,

  • the array is the input array
  • the index is the row position to insert a particular row
  • value are the values to be inserted into the array

Example: Insert to values in the array

array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]] #display print(array) 1

Output:

array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]] #display print(array) 2

Updating the values into the two-dimensional array

Here are two methods for updating values in the 2-D array(list).

You can update rows by using the following syntax

array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]] #display print(array) 3

You can update column values inside rows by using the following syntax

array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]] #display print(array) 4

Example:

array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]] #display print(array) 5

Output:

array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]] #display print(array) 6

Deleting the values from two-dimensional array

You can delete rows using the Array[row index][column index]6 function

Syntax:

array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]] #display print(array) 7

where,

  • the array is the input array
  • index refers to the row index

Example:

array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]] #display print(array) 8

Output:

array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]] #display print(array) 9

Get the size of two-dimensional array

You can get the size of the two-dimensional array using the line() function. It will return the number of rows in the array

Postingan terbaru

LIHAT SEMUA