Py学习  »  Python

Python把嵌套的字符串列表转换为整数列表的两种方法

Python小屋 • 6 年前 • 741 次点击  

假设有如下嵌套的字符串列表:

testMatrix = [['1', '2', '3'],
              ['4', '5', '6'],
              ['7', '8', '9']]

现要求将其转换为嵌套的整数列表。

方法一(循环,内置函数,函数式编程):

for index, item in enumerate(testMatrix):
    testMatrix[index] = list(map(int, item))

方法二(函数式编程,借助于扩展库numpy):

import numpy as np

testMatrix = list(map(list, np.int64(testMatrix)))

上面两种方法的转换结果均为:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]


今天看啥 - 高品质阅读平台
本文地址:http://www.jintiankansha.me/t/WWmhGBLh3N
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/3075
 
741 次点击