简体   繁体   English

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

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

I have a text file 'news.txt' , and I'm trying to save the third word of every line in a list(three) and I just keep getting a vector with the third word in the line over and over it doesn't seem to register the other words in the following lines.我有一个文本文件'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

You need to create a list before the for cycle and then append results to this list:您需要在for循环之前创建一个列表,然后将 append 结果添加到此列表中:

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

Otherwise you're just getting the third word from the last line the cycle ran through.否则,您只会从循环运行的最后一行中获得第三个单词。

Alternatively, you can do it with a list comprehension:或者,您可以使用列表推导来做到这一点:

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

I'd create an iterator (using a generator comprehension) and then supply the iterator to a list.我会创建一个迭代器(使用生成器理解),然后将迭代器提供给一个列表。

Like so:像这样:

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)

Optionally you could create a generator function separately rather than using a generator comprehension.或者,您可以单独创建一个生成器 function 而不是使用生成器理解。 A nice thing about doing it this way is you can tell the program how to handle the IndexError if there isn't a 3rd word.这样做的好处是,如果没有第三个单词,您可以告诉程序如何处理 IndexError。 You could even make it a utility function that accepts arguments:您甚至可以将其设为接受 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