“逐字逐句"是什么意思?语法在Python中意味着什么? [英] What does "word for word" syntax mean in Python?

查看:98
本文介绍了“逐字逐句"是什么意思?语法在Python中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 gensim教程页面中看到以下脚本片段.

I see the following script snippet from the gensim tutorial page.

下面的Python脚本中的单词对单词的语法是什么?

What's the syntax of word for word in below Python script?

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

推荐答案

这是

This is a list comprehension. The code you posted loops through every element in document.lower.split() and creates a new list that contains only the elements that meet the if condition. It does this for each document in 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

从示例中可以看到,列表推导可以嵌套.

As you can see from your example, list comprehensions can be nested.

这篇关于“逐字逐句"是什么意思?语法在Python中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆