简体   繁体   English

使用python中的大数据,比map()更快的方法(通过stdin.readlines())

[英]Faster way than map() for big data (via stdin.readlines()) in python

I've written a piece of code where i have to handle a lot of lines coming from stdin, split them, and then convert the values to integers (it's for a typical programming challenge). 我已经编写了一段代码,其中我必须处理很多来自stdin的行,将它们拆分,然后将值转换为整数(这是典型的编程挑战)。

My script needs ~20 sec for ~50,000 lines, but with cProfile i found out that all the map calls allready take ~8 seconds (i call split and map for every line, looks inefficient to me). 我的脚本需要大约20秒才能完成50,000条线,但是使用cProfile时,我发现所有的地图调用已经准备就绪需要大约8秒的时间(我对每行调用split和map,对我来说效率低下)。

Here's what i do: 这是我的工作:

inp = [i[0:-1] for i in stdin.readlines()][1:]
inp = [map(int, i.split()) for i in inp]

What's the more pythonic way to do this? 还有什么更蟒蛇的方式做到这一点?

Input looks like: 输入看起来像:

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

thanks a lot for help! 非常感谢您的帮助!

Use numpy.loadtxt or numpy.genfromtxt. 使用numpy.loadtxt或numpy.genfromtxt。 That will load the numbers into an array far more quickly than you can load them with loops in Python, because the implementation is in a compiled language and it's highly optimized. 这将数字加载到数组中的速度比用Python中的循环加载它们快得多,因为实现是使用编译语言编写的,并且高度优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM