我有以下值列表:
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]
请帮帮我。谢谢!!
最佳答案
你的第三个确实是对的在Python 3中map返回一个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'>
关于python - 如何在python中将字符串列表转换为整数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39650735/