我有一个二维列表:
li = [['1','2'],['3','4']]
如何把元素化为整型?
要处理的数据量很大,遍历li
是否会降低效率,有其他的方法吗?
高洛峰2017-04-17 17:18:25
Traverse, since every number needs to be converted, in theory the conversion program must visit each element at least once
伊谢尔伦2017-04-17 17:18:25
Use list parsing, but still need to traverse.
As mentioned above, because every element is accessed and then converted, let’s traverse it. . .
阿神2017-04-17 17:18:25
If the values are used in scattered places, convert each time a value is used and replace the original value.
阿神2017-04-17 17:18:25
One line of code to get it done: li_int = [map(int, e) for e in li]
The complexity of the conversion is linear and can be tolerated if the amount of data is not particularly large. If there is really a lot of data, whichever one is used will be used for conversion.