0

我在一个列表中有这个列表

a = [['1','2','3','4'],['1','2','3','4'],['1','2','3','4']]

但我需要它是 int ,我不确定在哪里使用 int() 将 str 更改为 int

a = [[1,2,3,4],[1,2,3,4],[1,2,3,4]]
4

4 回答 4

3

您可以像这样使用嵌套列表推导:

a = [['1','2','3','4'],['1','2','3','4'],['1','2','3','4']]
b = [ [int(j) for j in i] for i in a]
于 2012-12-02T19:30:26.253 回答
1
In [51]: a = [['1','2','3','4'],['1','2','3','4'],['1','2','3','4']]

In [52]: [map(int, l) for l in a]
Out[52]: [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
于 2012-12-02T19:34:46.453 回答
0

使用嵌套列表理解的示例:

In [1]: a = [['1','2','3','4'],['1','2','3','4'],['1','2','3','4']]

In [2]: [[int(s) for s in l] for l in a]
Out[2]: [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
于 2012-12-02T19:29:51.100 回答
0

这是一个想法:

>>> a = [['1','2','3','4'],['1','2','3','4'],['1','2','3','4']]
>>> map(lambda l: map(int, l), a)
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
于 2012-12-02T19:37:18.237 回答