python多行代码简化

Posted 嶙羽

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python多行代码简化相关的知识,希望对你有一定的参考价值。

python中,可以把多行代码简化为一行,把for循环和if条件判断都集中到一行里来写,示例如下:

>>> from nltk.corpus import stopwords
>>> english_stopwords = stopwords.words(english)#加载nltk中的英文停用词数据
#创建一个列表,内含3个单词列表
>>> texts_tokenized = [[writing, ii, rhetorical, composing, rhetorical, composing],[engages, series, interactive, reading],[research, composing, activities, along, assignments, designed, help]]
#用多行代码对texts_tokenized去停用词
>>> text_filtered_stopwords = [[word for word in document if not word in english_stopwords] for document in texts_tokenized] >>> text_filtered_stopwords [[writing, ii, rhetorical, composing, rhetorical, composing], [engages, series, interactive, reading], [research, composing, activities, along, assignments, designed, help]]

然后改成用多行的常规写法:

>>> texts_tokenized = [[writing, ii, rhetorical, composing, rhetorical, composing],[engages, series, interactive, reading],[research, composing, activities, along, assignments, designed, help]]
>>> documents = []
>>> texts_filtered_stopwords =[]
>>> for document in texts_tokenized:
      for word in document:
          if word not in english_stopwords:
              documents.append(word)
      texts_filtered_stopwords.append(document)

    
>>> texts_filtered_stopwords
[[writing, ii, rhetorical, composing, rhetorical, composing], [engages, series, interactive, reading], [research, composing, activities, along, assignments, designed, help]]

可以看到得出一样的结果,但是代码的效率和简洁程度大大提升



以上是关于python多行代码简化的主要内容,如果未能解决你的问题,请参考以下文章

在具有多行标题的选项卡上设置片段

为啥我的 C 代码片段不起作用?简化版可以。为 unsigned long long 传递不带 VA_ARGS 的 args

20个简洁的 JS 代码片段

github issue 如何写多行代码块

python基础函数

20个简洁的 JS 代码片段