繁体   English   中英

将我文件中每一行的第三个单词分配给 Python 中的列表

[英]Assign the third word of every line in my file to a list in Python

我有一个文本文件'news.txt' ,我试图将每一行的第三个单词保存在一个列表(三个)中,我只是不断地得到一个向量,该行中的第三个单词一遍又一遍地没有' t 似乎在以下几行中注册了其他单词。

with open('news.txt', 'r') as file:
    content = file.read()
    words = content.split() 
    for bwords in words:
        three = bwords[2]
with open('news.txt', 'r') as file:
    content = file.read()
    words = content.split()
    for bwords in words[::3]:
        three = bwords

您需要在for循环之前创建一个列表,然后将 append 结果添加到此列表中:

with open('news.txt', 'r') as file:
    content = file.read()
    words = content.split()
    three = [] 
    for bwords in words:
        three.append(bwords[2])

否则,您只会从循环运行的最后一行中获得第三个单词。

或者,您可以使用列表推导来做到这一点:

three = [word[2] for word in words]

我会创建一个迭代器(使用生成器理解),然后将迭代器提供给一个列表。

像这样:

with open('news.txt', 'r') as file:
    # create the iterator using a generator comprehension
    iter_word_3 = (word_list[2] for word_list in 
                   (line.split() for line in file))
    # feed the iterator to the list function
    threes = list(iter_word_3)

或者,您可以单独创建一个生成器 function 而不是使用生成器理解。 这样做的好处是,如果没有第三个单词,您可以告诉程序如何处理 IndexError。 您甚至可以将其设为接受 arguments 的实用程序 function:

def iter_xth_word(x, filestream):
    """Iterates over xth word from each line of a file."""
    for line in filestream:
        try:
            yield line.split()[x-1]
        except IndexError:
            # ignore lines without a xth word
            continue

with open('news.txt', 'r') as file:
    # create the iterator using a generator comprehension
    iter_word_3 = iter_xth_word(3, file)
    # feed the iterator to the list function
    threes = list(iter_word_3)

暂无
暂无

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

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