python - 对于大数据(通过 stdin.readlines())在 python 中比 map() 更快的方法

标签 python dictionary int stdin

我写了一段代码,我必须处理来自标准输入的大量行,拆分它们,然后将值转换为整数(这是一个典型的编程挑战)。

我的脚本需要大约 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/

相关文章:

java - 从 Map 中的对象检索值

将二进制转换为 int

python - 在 Python 中将 16 个字节的随机数据转换为整数

python - 获取已发送电子邮件的 MessageID 的最简单/最干净的方法是什么?

python - pip3 安装 PyYAML 失败。(python3.7,macOS High Sierra)

python pandas isin 方法?

json - 从 json 合并两个 map[string]interface{}

java - 扫描仪JAVA的某些要求

python - pandas 中的回归列

python - 如何在 Textblob python 语言检测库中获得语言扩展