Home > Blockchain >  Resizing list from a 2D array
Resizing list from a 2D array

Time:03-14

I came across a problem when trying to do the resize as follow:

I am having a 2D array after processing data and now I want to resize the data, with each row ignoring the first 5 items.

What I am doing right now is to:

edit: this apporach works fine as long as you make sure you are working with a list, but not string. it failed to work on my side because I haven't done the convertion from string to list properly. so it ends up eliminating the first five characters in the entire string.

2dArray=[[array1],[array2]]
new_Resize_2dArray= [array[5:] for array in 2dArray]

However, it does not seems to work as it just recopy all the element over to the new_Resize_2dArray.

I would like to ask for help to see what did I do wrong or if there is any scientific calculation library I could use to acheive this.

CodePudding user response:

First, because python list indexing is zero-based, your code should read new_Resize_2dArray= [array[5:] for array in 2dArray] if you want to not include the first 5 columns. Otherwise, I see no issue with your single line of code.

As for scientific computing libraries, numpy is a highly prevalent 3rd party package with a high-performance multidimensional array type ndarray. Using ndarrays, your code could be shortened to new_Resize_2dArray = 2dArray[:,5:]

Aside: It would help to include a bit more of your code or a minimum example where you are getting the unexpected result (e.g., use a fake/stand-in 2d array to see if it works as expected or still fails.

  • Related