Skip to main content

12.8) Exercise 35 - Creating Arrays


What are the Python commands to:

  1. Create an array from the list [[23,45.5,3],[34,56.3,4]]?
  2. [tippy title="Solution 1"]

    np.array([[23,45.5,3],[34,56.3,4]])
    

    [/tippy]

  3. Create a vector (1D array) with integers from 3 to 5 (inclusive)?
  4. [tippy title="Solution 2"]

    np.arrange(3,6)
    

    [/tippy]

  5. Create a array with 2 columns and 5 lines filled with 0.0?
  6. [tippy title="Solution 3"]

    np.zeros((5,2))
    

    [/tippy]

  7. Create a three dimensional arrays of shape (3,4,2) filled with 1 (integers)?
  8. [tippy title="Solution 4"]

    np.ones((3,4,2),dtype='i')
    

    [/tippy]

  9. Create a two dimensional array that is 3by3 and filled with NaNs?
  10. [tippy title="Solution 5"]

    A=np.empty((3,4,2))
    A[:]=np.nan
    # OR
    A[:]=None
    

    [/tippy]

  11. Create a two dimensional array 3by3 filled with 5.5?
  12. [tippy title="Solution 6"]

    np.ones((3,4,2)) * 5.5
    # OR
    A=np.empty((3,4,2))
    A[:]=5.5
    

    [/tippy]