首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查找两个大的非结构化文本文件之间的常用单词

查找两个大的非结构化文本文件之间的常用单词
EN

Stack Overflow用户
提问于 2016-02-10 10:46:15
回答 1查看 1K关注 0票数 0

我有两个大的非结构化的文本文件,无法装入内存。我想找出他们之间的常用语。

什么是最有效的(时间和空间)方式?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-11 18:38:06

我给了这两份文件

pi_poem

代码语言:javascript
复制
Now I will a rhyme construct
By chosen words the young instruct
I do not like green eggs and ham
I do not like them Sam I am

pi_prose

代码语言:javascript
复制
The thing I like best about pi is the magic it does with circles.
Even young kids can have fun with the simple integer approximations.

代码很简单。第一个循环逐行读取第一个文件,将单词插入字典集。第二个循环读取第二个文件;它在第一个文件的词典中找到的每个单词都会进入一组常用的单词。

这能满足你的需要吗?您将需要将其调整为标点符号,并且您可能希望在更改后删除额外的打印。

代码语言:javascript
复制
lexicon = set()
with open("pi_poem", 'r') as text:
    for line in text.readlines():
        for word in line.split():
            if not word in lexicon:
                lexicon.add(word)
print lexicon

common = set()
with open("pi_prose", 'r') as text:
    for line in text.readlines():
        for word in line.split():
            if word in lexicon:
                common.add(word)

print common

输出:

代码语言:javascript
复制
set(['and', 'am', 'instruct', 'ham', 'chosen', 'young', 'construct', 'Now', 'By', 'do', 'them', 'I', 'eggs', 'rhyme', 'words', 'not', 'a', 'like', 'Sam', 'will', 'green', 'the'])
set(['I', 'the', 'like', 'young'])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35313051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档