SoFunction
Updated on 2024-11-13

Example of 2D array copying using python slicing

.csv data format 10*3, dataSet

1.1,1.5,2.5 
1.3,1.9,3.2 
1.5,2.3,3.9 
1.7,2.7,4.6 
1.9,3.1,5.3 
2.1,3.5,6 
2.3,3.9,6.7 
2.5,4.3,7.4 
2.7,4.7,8.1 
2.9,5.1,8.8 

Copy the first two columns of the first 8 rows of this data into a new array with the following core code (trainData is the new array):

m, n = (dataSet)
trainData = ((m, n))
trainData[:8,:-1] = dataSet[:8,:-1]

symbol, before indicates the range of rows to be copied;, after indicates the range of columns to be copied, if you are not familiar with this you can search for python slicing.

The results of the run are as follows:

[[ 1.1 1.5 1. ] 
[ 1.3 1.9 1. ] 
[ 1.5 2.3 1. ] 
[ 1.7 2.7 1. ] 
[ 1.9 3.1 1. ] 
[ 2.1 3.5 1. ] 
[ 2.3 3.9 1. ] 
[ 2.5 4.3 1. ] 
[ 1. 1. 1. ] 
[ 1. 1. 1. ]]

The above example of using python slicing to realize two-dimensional array copying is all that I have shared with you, I hope it can give you a reference, and I hope you will support me more.