python - Quickly create a boolean array based on inclusion of the ith element in another list -


im working large list, approximately 56,000 elements (all strings) in size. im trying cut down run time.

is there way shorten line: x = [int(i in list2)for in list1]

given dictionary of words(list1) , sentence(list2), im trying create binary representation based of sentence such [1,0,0,0,0,0,1........0] 1 indicates ith word in dictionary shows in sentence.

whats fastest way can this?

example data:

dictionary =  ['aardvark', 'apple','eat','i','like','maize','man','to','zebra', 'zed'] sentence = ['i', 'like', 'to', 'eat', apples'] result = [0,0,1,1,1,0,0,1,0,0] 

i suggest this:

words = set(['hello','there']) #have words available set sentance = ['hello','monkey','theres','there'] rep = [ 1 if w in words else 0 w in sentance ] >>>  [1, 0, 0, 1] 

i take approach because sets have o(1) lookup time, check if w in words takes constant time. results in list comprehension being o(n) must visit each word once. believe close or efficient get.

you mentioned creating 'boolean' array, allow have following instead:

rep = [ w in words w in sentance ] >>>  [true, false, false, true] 

Comments