numpy array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

numpy array

I want to make a array like this q0 q1 q2 q0 q1 q2 i write below code A = np.empty([n + 1, n + 1],dtype=str) for i in range(0, n): for j in range(0, 1): A[i][j] = "q" .format(i) for i in range(n, n + 1): for j in range(1, n + 1): A[i][j] = "q{}".format(j) but it displays q q q q q q and it dosent have the numbers q0 q1 q2 how can i fix it?

3rd May 2021, 9:42 PM
nima rasi
2 Answers
+ 1
str is object in numpy, so try to replace `dtype=str` to `dtype=object` And on your line 4 `A[i][j] = "q".format(i) should be `A[i][j] = "q{}".format(i) or you can use f-strings for shorter approach f"q{j}" Here's the fixed code.
4th May 2021, 1:06 AM
noteve - avatar
+ 1
Txn a lottttt Eve it works
4th May 2021, 6:14 AM
nima rasi