1

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.

4
  • 3
    You could also compress (pun intended) your zip approach in a single line: "".join(c for c, b in zip(sent1, str(bin_seq)) if b == '1') Commented Feb 15, 2019 at 15:07
  • @tobias_k I am practicing with itertools so I was more concern about itertool.compress solution. Commented Feb 15, 2019 at 15:12
  • 1
    BTW, IMHO using an int for bin_seq is 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. Commented Feb 15, 2019 at 15:12
  • You caused the problem by trying to pass in bin_seq = 100101010100011, which makes it an integer, not a binary sequence (or even a string). The problem then becomes how to convert it back to a sequence (or iterable) of binary digits. So the question title is a misnomer: your "binary sequence" stopped being a binary sequence the moment you converted it to integer, which makes tthings harder. Commented Feb 18, 2024 at 2:19

3 Answers 3

2

Your compress approach is close. It's not working because the strings "0" and "1" both evaluate to True in a boolean context. One quick solution is to convert them to ints, since 0 is False and 1 is True in a boolean context:

import itertools

sent1 = 'ERAGSJKDLLDERRR'
bin_seq = 100101010100011

print("".join(itertools.compress(sent1, map(int, str(bin_seq)))))

Result:

EGJDLRR
Sign up to request clarification or add additional context in comments.

Comments

1
from itertools import compress

''.join(compress(sent1, map(int, str(bin_seq))))

'EGJDLRR'

The issue is you need to feed compress a list of (0, 1) as ints.

Comments

1

You could use LC instead of for loop

''.join([c for c, b in zip(sent1, str(bin_seq)) if b=='1'])

Edit:
For the sake of interest I timed the two by now available solutions:

''.join(compress(sent1, map(int, str(bin_seq))))
1.360 s                                                    
''.join([c for c, b in zip(sent1, str(bin_seq)) if b=='1']) 
0.766 s                            

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.