我写了一段代码,我必须处理来自标准输入的大量行,拆分它们,然后将值转换为整数(这是一个典型的编程挑战)。
我的脚本需要大约 20 秒来处理大约 50,000 行,但是使用 cProfile 我发现所有映射调用都需要大约 8 秒(我为每一行调用拆分和映射,对我来说效率很低)。
这是我的做法:
inp = [i[0:-1] for i in stdin.readlines()][1:]
inp = [map(int, i.split()) for i in inp]
执行此操作的更 pythonic 方法是什么?
输入看起来像:
2 # number of testcases
2 # depth of following numeric triangle
1
1 2
3 # depth of next numeric triangle
1
1 2
1 2 3
非常感谢您的帮助!
最佳答案
使用 numpy.loadtxt 或 numpy.genfromtxt。与在 Python 中使用循环加载数字相比,这会将数字加载到数组中的速度要快得多,因为该实现是在编译语言中进行的,并且是高度优化的。
关于python - 对于大数据(通过 stdin.readlines())在 python 中比 map() 更快的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21659689/