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

时间:2016-09-23 00:18:39

标签: python python-3.x

我有以下值列表:

DATA =  [['5', '1'], ['5', '5'], ['3', '1'], ['6', '1'], ['4', '3']]

如何将其转换为:

DATA = [[5, 1], [5, 5], [3, 1], [6, 1], [4, 3]]

注意:我已经尝试过以下操作,但所有这些都不适用于 Python 3

   1. DATA = [int(i) for i in DATA] 
   2. DATA = list(list(int(a) for a in b) for b in DA if a.isdigit())
   3. DATA = [map(int,x) for x in DATA]

请帮我解决这个问题。谢谢!

1 个答案:

答案 0 :(得分:5)

你的第三个实际上是正确的。在Python 3 map中返回一个地图对象,因此您只需要在其上调用list即可获得列表

DATA =  [['5', '1'], ['5', '5'], ['3', '1'], ['6', '1'], ['4', '3']]

d = [list(map(int, x)) for x in DATA]

# Output:
# [[5, 1], [5, 5], [3, 1], [6, 1], [4, 3]]

# type of one of the items in the sublist
# print(type(d[0][0])
# <class 'int'>