search

Home  >  Q&A  >  body text

数据结构 - python-如何把元素为字符串的二维列表转化为元素为整型的二维列表?

我有一个二维列表:

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

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

PHPzPHPz2808 days ago756

reply all(5)I'll reply

  • 高洛峰

    高洛峰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

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:18:25

    Same idea as above, use map

    map(lambda x:map(int, x), li)

    reply
    0
  • 伊谢尔伦

    伊谢尔伦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. . .

    reply
    0
  • 阿神

    阿神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.

    reply
    0
  • 阿神

    阿神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.

    reply
    0
  • Cancelreply