Given a 2D list, create a numpy 2D array using it.Note: Given 2D list is [[1, 2, 3], [4, 5, 6], [7, 8, 9]]Print the Numpy array.
Question
Given a 2D list, create a numpy 2D array using it.
Note: Given 2D list is [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Print the Numpy array.
Solution
Sure, here is how you can convert a 2D list into a numpy 2D array:
Step 1: First, you need to import the numpy library. If you haven't installed it yet, you can do so using pip:
pip install numpy
Step 2: Once you have numpy installed, you can import it in your python script as follows:
import numpy as np
Step 3: Now, you can convert your 2D list into a numpy array. Here is how you can do it:
# Given 2D list
list_2d = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Convert list to numpy array
numpy_array = np.array(list_2d)
# Print the numpy array
print(numpy_array)
When you run this script, it will print the following numpy 2D array:
[[1 2 3]
[4 5 6]
[7 8 9]]
This is your desired output.
Similar Questions
What is a correct syntax to create a NumPy array?np.createArray([4,5,6])np.numpyArray([4,5,6])np.array([4,5,6])np.create_array([4,5,6])
Convert heights and positions, which are regular lists, to numpy arrays. Call them np_heights and np_positions
Create a matrix having diagonal elements as 1 and all other elements as 0 of size (5, 6).Print the Numpy array.
What will be the output of the following Python code?A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] B = [[3, 3, 3], [4, 4, 4], [5, 5, 5]] zip(A, B)
Write the output?import numpy as np a = np.array( [1 ,2 , 3])b = np.append(a , [4, 5, 6])print( b, len(b) )
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.