Like this:
numrows = len(input) # 3 rows in your example
numcols = len(input[0]) # 2 columns in your example
Assuming that all the sublists have the same length (that is, it's not a jagged array).
Answer from Óscar López on Stack OverflowLike this:
numrows = len(input) # 3 rows in your example
numcols = len(input[0]) # 2 columns in your example
Assuming that all the sublists have the same length (that is, it's not a jagged array).
You can use numpy.shape.
import numpy as np
x = np.array([[1, 2],[3, 4],[5, 6]])
Result:
>>> x
array([[1, 2],
[3, 4],
[5, 6]])
>>> np.shape(x)
(3, 2)
First value in the tuple is number rows = 3; second value in the tuple is number of columns = 2.
Videos
I am using two for loops, on the inside of the second loop I want to create a 2d array that adds a new row once the second loop is finished. Is it possible to do something like this in Python?
Desired[i,j] = thing.thing(j) I’m trying to add to the array to make it bigger with each iteration.
for i in range(height):
for j in range(width):
array[i][j] = charI puted the values in the array like this
but I dont konw how to do like this (I want to know length of "i" and "j")
array[i][j] len(i) , len(j)
Non-shitty languages:
int[] myArray = new int[5][5]
Python:
myArray = [[0]*5]*5
Next:
myArray[1][0] = 1
print(myArray[0][0])
Non-shitty languages:
0
Python:
1
Hello!
I have 5 sets, each set contains a number of elements of variable length (eg. set 1 has 100 elements, set 2 has 150 and so on)
Is it possible to store these sets in a single structure?
For example, if I want to print the 35th elements of the 3rd set, I could call it simply by saying something like MyContainer[setN][elementN]
I was trying to use a 2D array but I can't initialize its shape, since each set has a variable number of elements.
Can anybody point me to the right direction / best practice?
Thank you