1405/python-2d-array

You can use NumPy library of Python, below I have mentioned few ways to create 2-D arrays using NumPy: >>> import numpy >>> numpy.zeros((5, 5)) array([[ 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0.]]) numpy provides a matrix type as well. It's less commonly used, and some people recommend against using it. But it's useful for people coming to numpy from Matlab, and in some other contexts. I thought I'd include it since we're talking about matrices! >>> numpy.matrix([[1, 2], [3, 4]]) matrix([[1, 2], [3, 4]]) Here are some other ways to create 2-d arrays and matrices (with output removed for compactness): numpy.matrix('1 2; 3 4') # use Matlab-style syntax numpy.arange(25).reshape((5, 5)) # create a 1-d range and reshape numpy.array(range(25)).reshape((5, 5)) # pass a Python range and reshape numpy.array([5] * 25).reshape((5, 5)) # pass a Python list and reshape numpy.empty((5, 5)) # allocate, but don't initialize numpy.ones((5, 5)) # initialize with ones numpy.ndarray((5, 5)) # use the low-level constructor

>>> import numpy >>> numpy.zeros((5, 5)) array([[ 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0.]])

>>> matrix = 5*[5*[0]] >>> matrix [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] >>> matrix[4][4] = 2 >>> matrix [[0, 0, 0, 0, 2], [0, 0, 0, 0, 2], [0, 0, 0, 0, 2], [0, 0, 0, 0, 2], [0, 0, 0, 0, 2]]

You should make a list of lists, and the best way is to use nested comprehensions:

>>> matrix = [[0 for i in range(5)] for j in range(5)] >>> pprint.pprint(matrix) [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

Hey @Laksha, you can try something like ...READ MORE

print(*names, sep = ', ') This is what ...READ MORE

Slicing is basically extracting particular set of ...READ MORE

Hey @abhijmr.143, you can print array integers ...READ MORE

suppose you have a string with a ...READ MORE

You can also use the random library's ...READ MORE

Syntax : list. count(value) Code: colors = ['red', 'green', ...READ MORE

can you give an example using a ...READ MORE

Python doesn't have a native array data ...READ MORE

You can try the below code which ...READ MORE

OR

At least 1 upper-case and 1 lower-case letter

Minimum 8 characters and Maximum 50 characters

Already have an account? Sign in.