我有一个字符串和二进制序列(0和1s的整数)
sent1 = 'ERAGSJKDLLDERRR'
bin_seq = 100101010100011
所以我想通过与bin_seq
比较来获取字母。因此,如果bin_seq
在相应位置的值为1,它将返回字母。
因此它应该返回:
'EGJDLRR'
我正在使用itertools.compress
进行上述操作。
from itertools import compress
sent1 = 'ERAGSJKDLLDERRR'
bin_seq = 100101010100011
print("".join(list(itertools.compress(sent1, str(bin_seq)))))
哪个返回输出:
'ERAGSJKDLLDERRR'
我知道我可以通过使用for
循环轻松做到这一点:
sent_new = []
for i,j in zip(sent1, str(bin_seq)):
if j == '1':
sent_new.append(i)
print("".join(sent_new))
但是我更担心为什么它不能通过itertools.compress
给出预期的输出。
答案 0 :(得分:2)
您的compress
方法已经接近。它不起作用,因为字符串“ 0”和“ 1”在布尔上下文中都评估为True。一种快速的解决方案是将它们转换为int,因为在布尔上下文中0为False而1为True:
import itertools
sent1 = 'ERAGSJKDLLDERRR'
bin_seq = 100101010100011
print("".join(itertools.compress(sent1, map(int, str(bin_seq)))))
结果:
EGJDLRR
答案 1 :(得分:1)
from itertools import compress
''.join(compress(sent1, map(int, str(bin_seq))))
'EGJDLRR'
问题是您需要将{0,1)的列表作为整数输入compress
。
答案 2 :(得分:1)
您可以使用LC代替for循环
syllable duration
ba c(0.20414850,0.18228837,0.22232325)
a c(0.06804950,0.09877130)
na c(0.11525535,0.36774874)
编辑:
为了使我感兴趣,我为这两个现在可用的解决方案定了时间:
''.join([c for c, b in zip(sent1, str(bin_seq)) if b=='1'])