> texts = [[word for word in document.low-6ren">
gpt4 book ai didi

python - "word for word"语法在 Python 中意味着什么?

转载 作者:太空宇宙 更新时间:2023-11-04 06:47:25 25 4
gpt4 key购买 nike

我从 gensim tutorial page 中看到以下脚本片段.

下面 Python 脚本中逐字的语法是什么?

>> texts = [[word for word in document.lower().split() if word not in stoplist]
>> for document in documents]

最佳答案

这是一个 list comprehension .您发布的代码遍历 document.lower.split() 中的每个元素并创建一个新列表,其中仅包含满足 if 条件的元素。它为 documents 中的每个文档执行此操作。

尝试一下...

elems = [1, 2, 3, 4]
squares = [e*e for e in elems] # square each element
big = [e for e in elems if e > 2] # keep elements bigger than 2

正如您从示例中看到的,列表理解可以嵌套。

关于python - "word for word"语法在 Python 中意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20953143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com