从嵌套列表中的第二项删除引号

时间:2014-04-15 11:06:34

标签: python

我需要从嵌套列表中的第二项中删除引号。例如,更改:

a = [['first', '41'], ['second', '0'], ['third', '12']]

为:

[['first', 41], ['second', 0], ['third', 12]]

我试过

[map(int, [n[1]]) for n in a]
[[41], [0], [12], [0], [45], [17], [3], [10], [1], [19], [98], [0]]

但它删除了第一个元素。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:2)

[[item[0], int(item[1])] for item in a]

<强>输出:

[['first', 41], ['second', 0], ['third', 12]]

答案 1 :(得分:0)

你可以这样做:

a = [['first', '41'], ['second', '0'], ['third', '12']]
a = [[i[0], int(i[1])]for i in a]

>>> print a
[['first', 41], ['second', 0], ['third', 12]]