Skip to content

Instantly share code, notes, and snippets.

@naemazam
Created October 30, 2021 09:58
Show Gist options
  • Save naemazam/25a60f0e45bb6138b9017726d9248e88 to your computer and use it in GitHub Desktop.
Save naemazam/25a60f0e45bb6138b9017726d9248e88 to your computer and use it in GitHub Desktop.
''' Create a one-dimensional all 0 ndarray object with length n, and then make
the m-th element equal to 1
Enter n, m separated by spaces, and n > = m
[sample input]
10 5
[sample output]
[[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]]
'''
import numpy as np
n, m = input().split()
n =int(n)
m = int(m)
new_list= []
for i in range(n):
if i == m-1:
new_list.append(1)
else:
new_list.append(0)
x = np.array([new_list])
print(x)
@naemazam
Copy link
Author

You can do it by this way

import numpy as np
n,m=map(int,input("Enter n,m: ").split())
ans=np.zeros(n)
ans[m-1]=1
print(ans)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment