简体   繁体   English

Python:将文件中的单词加载到集合中

[英]Python: load words from file into a set

I have a simple text file with several thousands of words, each in its own line, eg 我有一个简单的文本文件,其中包含数千个单词,每个单词都在自己的行中,例如

aardvark
hello
piper

I use the following code to load the words into a set (I need the list of words to test membership, so set is the data structure I chose): 我使用以下代码将单词加载到一个集合中(我需要单词列表来测试成员资格,所以set是我选择的数据结构):

my_set = set(open('filename.txt'))

The above code produces a set with the following entries (each word is followed by a space and new-line character: 上面的代码生成一个包含以下条目的集合(每个单词后跟一个空格和换行符:

("aardvark \n", "hello \n", "piper \n")

What's the simplest way to load the file into a set but get rid of the space and \\n? 将文件加载到集合中但删除空间和\\ n的最简单方法是什么?

Thanks 谢谢

string的strip()方法从两端移除空格。

set(line.strip() for line in open('filename.txt'))

只需加载所有文件数据并将其拆分,每行将处理一个单词或每行由空格分隔多个单词,除非您的文件是GB,否则一次加载整个文件会更快

words =  set(open('filename.txt').read().split())
my_set = set(map(str.strip, open('filename.txt')))

仅删除右侧空格。

set(map(str.rstrip, open('filename.txt')))
with open("filename.txt") as f:
    mySet = map(str.rstrip, f)

If you want to use this in Python 2.5, you need 如果你想在Python 2.5中使用它,你需要

from __future__ import with_statement
with open("filename.txt") as f:
    s = set([line.rstrip('\n') for line in f])

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

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