+ 1

Data Science - Reshape

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. Input Format First line: an integer (r) indicating the number of rows of the 2-dimensional array Next line: numbers separated by the space Output Format An numpy 2d array of values rounded to the second decimal. Sample Input 2 1.2 0 0.5 -1 Sample Output [[ 1.2 0. ] [ 0.5 -1. ]] import numpy as np r = int(input()) lst = [float(x) for x in input().split()] arr = np.array(lst) print(np.round(arr.reshape(r, 2), decimals=2))

7th Oct 2021, 12:56 PM
Ana Djurica
Ana Djurica - avatar
14 Answers
+ 14
try r = int(input()) lst = [float(x) for x in input().split()] arr = np.array(lst) arr_new=arr.reshape(r,-1) #-1 stands for any value that numpy founds suitable. print(arr_new)
19th Oct 2021, 4:15 AM
Musa Khan
Musa Khan - avatar
+ 3
#library import import numpy as np #input and create list row = int(input()) lst = [round(float(x), 2) for x in input().split()] #number of columns column = len(lst)//row #reshape numpy array arr = np.array(lst) arr_2d = arr.reshape(row, column) print(arr_2d)
10th Jul 2022, 11:37 AM
Kerem DÜZENLİ
+ 1
This is 15 code project (in data science course) I Think that I don't understand this: "Note that r divides the length of the list evenly." of the task. The 2 first tests are o.k. but the next 3 tests are wrong.
7th Oct 2021, 1:18 PM
Ana Djurica
Ana Djurica - avatar
+ 1
Please help. Thank you.
7th Oct 2021, 1:19 PM
Ana Djurica
Ana Djurica - avatar
+ 1
Josiah Mathieu and what am I missing?
7th Oct 2021, 4:09 PM
Ana Djurica
Ana Djurica - avatar
+ 1
I solved it. Thank you.
7th Oct 2021, 4:40 PM
Ana Djurica
Ana Djurica - avatar
+ 1
The inputs of Next line(lst) are not divisible by 2 in all the tests
6th Apr 2022, 3:27 PM
Ana Djurica
Ana Djurica - avatar
+ 1
hack with `.reshape(r,-1)` works but it didn't explained in the course :)
4th Jun 2022, 4:28 AM
Eugene Lagoda
Eugene Lagoda - avatar
+ 1
import numpy as np r = int(input()) lst = [float(x) for x in input().split()] arr = np.array(lst) arr=arr.reshape(r,-1) print(arr)
20th Jan 2023, 11:29 AM
Mustapha ouaniza
0
The note that r DIVIDES THE LENGTH of the list evenly tells you that the 2D array will always have enough elements to fill r rows evenly (no empty spots). Therefore you have to have the number of columns in which r rows creates an acceptable 2D array.
7th Oct 2021, 2:17 PM
Josiah Mathieu
Josiah Mathieu - avatar
0
Can somebody explain why reshape(r,2) doesn`t work?
6th Apr 2022, 10:24 AM
Andrei Prystupchyk
Andrei Prystupchyk - avatar
0
import numpy as np r = int(input()) lst = [float(x) for x in input().split()] arr = np.array(lst) p=np.array(lst)+np.array(arr) k=p.size//r print((p/2).reshape(r,k))
8th Nov 2022, 6:04 AM
Пётр Попов
Пётр Попов - avatar
0
import numpy as np r = int(input()) lst = [float(x) for x in input().split()] arr = np.array(lst) print(np.reshape(arr, (r, int(len(lst) / r))))
14th Feb 2023, 7:36 PM
Plamen Petkov
Plamen Petkov - avatar
0
Your code works for the given sample, but it has one issue — you’ve hardcoded 2 as the number of columns: arr.reshape(r, 2) That will only work if the number of columns is always 2, which is not guaranteed by the problem statement. Instead, you should calculate the number of columns from the total length of the list and r. Here’s the corrected version: python Copy Edit import numpy as np r = int(input()) lst = [float(x) for x in input().split()] arr = np.array(lst) # Calculate columns automatically c = len(lst) // r print(np.round(arr.reshape(r, c), decimals=2)) Why this works: len(lst) // r ensures the columns are computed based on input size. .reshape(r, c) creates the correct 2D shape. np.round(..., 2) rounds to 2 decimal places as required.
11th Aug 2025, 1:52 PM
Mayank kumar Verma
Mayank kumar Verma - avatar