python中open函数是不是一次性载入内存?
PHP中文网
PHP中文网 2017-04-17 15:23:16
[Python讨论组]

背景:语料库,词袋

形如:

dictionary=corpora.Dictionary(line.split() for line in open(mycorpora.txt))

文本文件是不是一次性载入内存?

PHP中文网
PHP中文网

认证0级讲师

全部回复(2)
怪我咯

open 不会
for ... in 不会

文件读取貌似只有open(xxx).read()会一次性读完整个文件内容,其他方式(readlines之类的)都不会

但是corpora.Dictionary 是什么行为就不清楚了

ringa_lee

line.split() for line in open(mycorpora.txt)会返回一个generatorcorpora.Dictionary.

We can use the generator expressions as arguments to various functions
that consume iterators.

sum(((x*x for x in range(10))) 285
When there is only one argument to the calling function, the
parenthesis around generator expression
can be omitted.

sum(x*x for x in range(10)) 285

open函数只是给你个handle,他可不知道你要去读还是写。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号