0

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
it371
it371 - avatar
13 odpowiedzi
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
+ 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
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
0
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
it371
it371 - avatar
0
Please help. Thank you.
7th Oct 2021, 1:19 PM
it371
it371 - avatar
0
Josiah Mathieu and what am I missing?
7th Oct 2021, 4:09 PM
it371
it371 - avatar
0
I solved it. Thank you.
7th Oct 2021, 4:40 PM
it371
it371 - avatar
0
Can somebody explain why reshape(r,2) doesn`t work?
6th Apr 2022, 10:24 AM
Andrei Prystupchyk
Andrei Prystupchyk - avatar
0
The inputs of Next line(lst) are not divisible by 2 in all the tests
6th Apr 2022, 3:27 PM
it371
it371 - 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) arr=arr.reshape(r,-1) print(arr)
20th Jan 2023, 11:29 AM
Mustapha ouaniza
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