Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

2d array row and column index

# 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]]

Comment

2d array rows and columns

using System;
using System.Linq;
using System.Runtime.InteropServices;

namespace ConsoleApplication4
{
    public static class Program
    {
        private static void Main()
        {
			/*
				C# supports multidimensional arrays up to 32 dimensions.
    			The multidimensional array can be declared by adding commas in the
    			square brackets.
    			For example, [,] declares two-dimensional array,
    			[, ,] declares three-dimensional array,
    			[, , ,] declares four-dimensional array, and so on.
    			So, in a multidimensional array, number of commas = Number of Dimensions.
			*/

			// For instance:
			int[,] arr2d; // two-dimensional array
			int[, ,] arr3d; // three-dimensional array
			int[, , ,] arr4d ; // four-dimensional array

          // declare and instantiate a 2d array with 4 rows and 5 columns
          	var array = new int[4,5](); 
          	/* The [4, 5] defines the number of rows and columns.
          	   The first rank (4) denotes the number of rows, and the second rank (5)
               defines number of columns.
               The code below instantiate and illustrate the 2d array divide into rows and columns.
          	*/
          
          	var array = new [,] // or var array = new int[4,5]
            {
              // col0,col1,col2,col3,col4
                {0.1, 0.2, 0.3, 0.4, 0.5}, // row0
                {1.1, 1.2, 1.3, 1.4, 1.5}, // row1
                {2.1, 2.2, 2.3, 2.4, 2.5}, // row2
                {3.1, 3.2, 3.3, 3.4, 3.5}, // row3
            };
			
          	// Get the element 0.2 (that is row0, col1)
          	array[0, 1]; //returns 0.2
          	// Get the element 2.3 (that is row2, col2)
          	array[2, 2]; //returns 2.3          

			//array[4, 1]; //throws run-time error as there is no 4th row
          	//array[2, 5]; //throws run-time error as there is no 5th column
          // get the third row
            var row = array.GetRow(2);

            // This prints 2.1, 2.2, 2.3, 2.4, 2.5
            Console.WriteLine(string.Join(", ", row.Select(element => element.ToString())));
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Python :: filter dataframe with a list of index 
Python :: create exact window size tkinter 
Python :: deleting an object in python 
Python :: 1d array operations in python 
Python :: NumPy flip Syntax 
Python :: replace in lists python 
Python :: python to exe online 
Python :: how to tell python to go back to a previous line 
Python :: cosine similarity python 
Python :: onedrive python upload 
Python :: get python to run cli commands 
Python :: how to join an array of characters in python 
Python :: django debug toolbar urlpatterns 
Python :: render to response django 
Python :: remove a first array of item in python 
Python :: python easter egg 
Python :: django add to database 
Python :: 1*2*3*4*5*6* - print on console?by python 
Python :: python calculations with variable x (letter) 
Python :: read data from gooogle cloud storage 
Python :: max element in dictionary python 
Python :: python check if attribute exists in dictionary 
Python :: heading none in pandas import 
Python :: recursive python 
Python :: multiple categories on distplot 
Python :: Chef in his Office codechef solution 
Python :: python manual elif 
Python :: scipy.arange is deprecated and will be removed 
Python :: install python modules without pip 
Python :: flask or django 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =