vinita
vinita

Reputation: 587

how to vectorize an array in python

Ive got two vectors of different lengths :-

a=[1,2,4,6]
b=[2,3,4,8,9]

based on the indices of these vectors I want a 1/0 in the output as

[1,1,0,1,0,1,0,0,0,0] 
[0,1,1,1,0,0,0,1,1,0]

Can anyone help on this ?

Upvotes: 1

Views: 142

Answers (1)

mata
mata

Reputation: 69022

A simple solution would be:

>>> a=[1,2,4,6]
>>> b=[2,3,4,8,9]
>>> 
>>> [int(i in a) for i in range(1,11)]
[1, 1, 0, 1, 0, 1, 0, 0, 0, 0]
>>> [int(i in b) for i in range(1,11)]
[0, 1, 1, 1, 0, 0, 0, 1, 1, 0]

Upvotes: 2

Related Questions