Suppose I need a n elements long with each element seperated by a space. Further, I need the ith element to be a 1 and the rest to be a 0. What's the easiest pythonic way to do this?
Thus if n = 10, and i = 2, I would want
0 1 0 0 0 0 0 0 0 0
I want something like a new string[] that you can get in C++ but the Python version is eluding me.
Thanks!
Use a list.
n = 10
i = 2
mylist = ["0"] * n
mylist[i-1] = "1"
print " ".join(mylist)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With