Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将二进制数转换为每个二进制数的数组_Python_String_List_Binary_List Comprehension - Fatal编程技术网

Python 将二进制数转换为每个二进制数的数组

Python 将二进制数转换为每个二进制数的数组,python,string,list,binary,list-comprehension,Python,String,List,Binary,List Comprehension,我试图将二进制值转换为每个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 &

我试图将二进制值转换为每个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
尝试

bin=[int(x)表示s在c中表示s在s中表示x]

如果您有:

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] )

非常感谢。有没有办法在一行中理解我的两个列表?
bin=[int(i)for element in[“{0:b}”。format(ord(c))for message中的c]for element中的i]