数据结构 - python-如何把元素为字符串的二维列表转化为元素为整型的二维列表?
PHPz
PHPz 2017-04-17 17:16:25
0
5
754

我有一个二维列表:

li = [['1','2'],['3','4']]

如何把元素化为整型?
要处理的数据量很大,遍历li是否会降低效率,有其他的方法吗?

PHPz
PHPz

学习是最好的投资!

reply all(5)
小葫芦

Traverse, since every number needs to be converted, in theory the conversion program must visit each element at least once

迷茫

Same idea as above, use map

map(lambda x:map(int, x), li)
伊谢尔伦

Use list parsing, but still need to traverse.
As mentioned above, because every element is accessed and then converted, let’s traverse it. . .

阿神

If the values ​​are used in scattered places, convert each time a value is used and replace the original value.

阿神

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template