Tambahkan lebih banyak kolom ke dalam array 2D
# To create 2 or more dimensional Arrays in python
# A two-dimensional array(list) is like a table with rows and columns.
# Assume there are 1, 2, 3 to n rows and 1, 2, 3 to n colums of data
# Let data at row 1,column 1; row 2, column 2, correspond to d11, d22
# col1 col2 ... coln
# row1 d11 d12 ... d1n
# row2 d21 d22 ... d2n
# . . . ... .
# . . . ... .
# . . . ... .
# rown dn1 dn2 ... dnn
# Syntax of 2d array in python:
# [[d11,d12,d13,..,d1n],[d21,d22,d23,.......,d2n]]
# 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)
# Accessing the values using index position
# Syntax:
# 1) Get row value using [] operator
# i.e array[row index]
# 2) Get column value using [][]
# i.e array[row index][column index]
# where,
# row index is the row position starts from 0
# column index is the column position starts from 0 in a row.
# For instance:
# get the first row
print(array[0])
# get the third row
print(array[2])
#get the element at the first row and the third column
print(array[0][2])
# get the element (value) at the third row and forth column
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
# Inserting values into two-dimensional array using the insert() function
# Syntax:
# array.insert(index,[values])
# where,
# the index is the row position to insert a particular row
# [values] are the values to be inserted into the array.
# It must a list of values. It could be same length (5) as columns above
# Example:
# insert 5 new data at the third row
array.insert(2, [1,2,3,4,5])
#insert another column of data at the 6th row
array.insert(5, [8,9,10,11,12])
#display
print(array)
Output:
[[23, 45, 43, 23, 45], [45, 67, 54, 32, 45], [1, 2, 3, 4, 5], [89, 90, 87, 65, 44], [23, 45, 67, 32, 10], [8,9,10,11,12]]
Mckynde