Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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_List - Fatal编程技术网

Python 如何将此字符串列表拆分为整数列表?

Python 如何将此字符串列表拆分为整数列表?,python,list,Python,List,我有一个字符串列表,如下所示: ['25 32 49 50 61 72 78 41\n', '41 51 69 72 33 81 24 66\n'] 我想把这个字符串列表转换成整数列表。所以我的名单是: [[25, 32, 49, 50, 61, 72, 78, 41], [41, 51, 69, 72, 33, 81, 24, 66]] 我已经考虑了一段时间,没有找到解决办法。 顺便说一下,我上面给出的字符串列表是使用 open("file", "r").readlines() 使用s

我有一个字符串列表,如下所示:

['25 32 49 50 61 72 78 41\n',
 '41 51 69 72 33 81 24 66\n']
我想把这个字符串列表转换成整数列表。所以我的名单是:

[[25, 32, 49, 50, 61, 72, 78, 41], [41, 51, 69, 72, 33, 81, 24, 66]]
我已经考虑了一段时间,没有找到解决办法。 顺便说一下,我上面给出的字符串列表是使用

open("file", "r").readlines()
使用
split()
将字符串拆分为列表,然后使用
int()
将其转换为整数

使用
map()

或使用列表理解:

In [14]: [[int(y) for y in x.split()] for x in lis]
Out[14]: [[25, 32, 49, 50, 61, 72, 78, 41], [41, 51, 69, 72, 33, 81, 24, 66]]
您也可以从文件中直接创建此列表,无需
readlines()

with open("file") as f:
    lis=[map(int,line.split()) for line in f]
    print lis
...
[[25, 32, 49, 50, 61, 72, 78, 41], [41, 51, 69, 72, 33, 81, 24, 66]]
使用
split()
将字符串拆分为列表,然后使用
int()
将其转换为整数

使用
map()

或使用列表理解:

In [14]: [[int(y) for y in x.split()] for x in lis]
Out[14]: [[25, 32, 49, 50, 61, 72, 78, 41], [41, 51, 69, 72, 33, 81, 24, 66]]
您也可以从文件中直接创建此列表,无需
readlines()

with open("file") as f:
    lis=[map(int,line.split()) for line in f]
    print lis
...
[[25, 32, 49, 50, 61, 72, 78, 41], [41, 51, 69, 72, 33, 81, 24, 66]]
试试这个列表


试试这个列表

谢谢!接下来的问题是:map()在Python中是某种类型的类型转换吗?@TheConjuring
map()
将传递给它的函数应用于iterable的每一项,并在Python2.x中返回一个列表(它在Python3.x中返回map对象),因此我们基本上是对每个x.split()调用int()函数?我现在很清楚了。非常感谢,谢谢!接下来的问题是:map()在Python中是某种类型的类型转换吗?@TheConjuring
map()
将传递给它的函数应用于iterable的每一项,并在Python2.x中返回一个列表(它在Python3.x中返回map对象),因此我们基本上是对每个x.split()调用int()函数?我现在很清楚了。非常感谢你。
  b=[[int(x) for x in i.split()] for i in open("file", "r").readlines()]