我试图将二进制值转换为每个1/0的列表,但我得到的是默认的二进制值,而不是列表。
我有一个字符串,我用二进制转换每个字符,它给我一个列表,每个字符都有一个字符串。现在我试图将每个字符串拆分为值为0/1的整数,但我无法得到任何东西。
# if message = "CC"
message="CC"
# just a debug thing
for c in message:
asci = ord(c)
bin = int("{0:b}".format(asci))
print >> sys.stderr, "For %c, asci is %d and bin is %d" %(c,asci,bin)
c = ["{0:b}".format(ord(c)) for c in message]
# c = ['1000011', '1000011']
bin = [int(c) for c in c]
#bin should be [1,0,0,0,0,1,1,1,0,0,0,0,1,1]
# but I get [1000011, 1000011]
print >> sys.stderr, bin
答案 0 :(得分:2)
如果你有这个:
c = ['1000011', '1000011']
你想实现这个目标:
[1,0,0,0,0,1,1,1,0,0,0,0,1,1]
你可以这样做:
modified_list=[int(i) for element in c for i in element]
或者您可以使用itertools.chain
from itertools import chain
modified_list=list(chain(*c))
如果你想加入列表理解,你可以这样做:
bin= list( chain( *["{0:b}".format(ord(c)) for c in message] )
答案 1 :(得分:0)
尝试;
bin = [int(x)for s in c in x in s in s]