I have an ascending list of integers e that starts from 0 and I would like to have a binary list b whose i-th element is 1 if and only if i belongs to e.
For example, if e=[0,1,3,6], then this binary list should be [1,1,0,1,0,0,1],
where the first 1 is because 0 is in e, the second 1 is because 1 is in e, the
third 0 is because 2 is not in e, and so on.
You can find my code for that below.
My question is: is there something built-in in python for that? If not, is my approach the most efficient?
def list2bin(e):
b=[1]
j=1
for i in range(1, e[-1]+1):
if i==e[j]:
b.append(1)
j+=1
else:
b.append(0)
return(b)