Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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_Integer - Fatal编程技术网

如何在Python中将字符串列表转换为整数列表?

如何在Python中将字符串列表转换为整数列表?,python,string,list,binary,integer,Python,String,List,Binary,Integer,如果我有一个二进制文件列表 a=['0b11001000', '0b01001100', '0b00111100', '0b00011111', '0b11110000', '0b01011010', '0b10010110', '0b00011110'] 我想将所有字符串元素转换为整数,并将其很好地放回二进制列表中: a=[0b11001000, 0b01001100, 0b00111100, 0b00011111, 0b11110000, 0b01011010, 0b100

如果我有一个二进制文件列表

a=['0b11001000', '0b01001100', '0b00111100', '0b00011111', 
   '0b11110000', '0b01011010', '0b10010110', '0b00011110']
我想将所有字符串元素转换为整数,并将其很好地放回二进制列表中:

a=[0b11001000, 0b01001100, 0b00111100, 0b00011111, 
   0b11110000, 0b01011010, 0b10010110, 0b00011110]
我该怎么办?

试试看:

a = [int(x, 2) for x in a]
a = [int(x, 2) for x in a]
尝试一下:

a = [int(x, 2) for x in a]
a = [int(x, 2) for x in a]

int
与基数2一起使用:

>>> a=['0b11001000', '0b01001100', '0b00111100', '0b00011111', 
...        '0b11110000', '0b01011010', '0b10010110', '0b00011110']
>>> [int(x, 2) for x in a]
[200, 76, 60, 31, 240, 90, 150, 30]

int
与基数2一起使用:

>>> a=['0b11001000', '0b01001100', '0b00111100', '0b00011111', 
...        '0b11110000', '0b01011010', '0b10010110', '0b00011110']
>>> [int(x, 2) for x in a]
[200, 76, 60, 31, 240, 90, 150, 30]

尽管在第二个列表中,您将整数表示为二进制,但它们仍然是
int
类型。但是,您可以转换字符串:


尽管在第二个列表中,您将整数表示为二进制,但它们仍然是
int
类型。但是,您可以转换字符串: