I have a string and binary sequence(which is int of 0s and 1s)
sent1 = 'ERAGSJKDLLDERRR'
bin_seq = 100101010100011
So I want to fetch the letters by comparing with bin_seq. So it will return the letter if the value of bin_seq is 1 at corresponding positions.
So it should return:
'EGJDLRR'
I am using itertools.compress for the above operation.
from itertools import compress
sent1 = 'ERAGSJKDLLDERRR'
bin_seq = 100101010100011
print("".join(list(itertools.compress(sent1, str(bin_seq)))))
Which returns the output:
'ERAGSJKDLLDERRR'
I know I can do it easily by using for loop:
sent_new = []
for i,j in zip(sent1, str(bin_seq)):
if j == '1':
sent_new.append(i)
print("".join(sent_new))
But I am more concern about why it is not giving expected output with itertools.compress.
compress(pun intended) yourzipapproach in a single line:"".join(c for c, b in zip(sent1, str(bin_seq)) if b == '1')itertoolsso I was more concern aboutitertool.compresssolution.intforbin_seqis not a good choice. You should only use numeric types if you intend to do math, e.g. not for phone numbers, zip codes, or other strings that just happen to be all-numbers.