0

I'm trying to resolve an exercise (By SoloLearn) that I need to transform 1D array to 2D array. So, the site uses 5 tests to evaluate the code, my code just passes in the first two first tests. Can someone give me a hint?

Task: Given a list of numbers and the number of rows (r), reshape the list into a 2-dimensional array. Note that r divides the length of the list evenly.

Sample Input

2

1.2 0 0.5 -1

Sample Output

[[ 1.2 0. ]

[ 0.5 -1. ]]

Me code:

import numpy as np

r = int(input()) #numbers of row
lst = [float(x) for x in input().split()] #numbers in array
arr = np.array(lst)
arr = np.round(arr, 2) #round to the second decimal

two_dim = np.reshape(arr, (r, 2))
print(two_dim)
3
  • You can pass -1 to numpy.reshape to let it infer that value from the array length and remaining dimensions. Commented Feb 2, 2021 at 16:16
  • Wow It's worked! I thought this but I didn't try because I thought it won't work... Thanks, my friend! Commented Feb 2, 2021 at 16:19
  • len(arr) // r is fine too, if you don't want to use -1 Commented Feb 2, 2021 at 21:46

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.