我有以下值列表:
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]
请帮我解决这个问题。谢谢!
答案 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'>