首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用lambda的Python3语法错误

使用lambda的Python3语法错误
EN

Stack Overflow用户
提问于 2017-09-09 01:47:14
回答 1查看 3.2K关注 0票数 1

我收到了一个语法错误,下面是一个教程。感觉就像Python3getcha。提前谢谢你!

代码语言:javascript
复制
def lda_description(review_text, min_topic_freq=0.05):
"""
accept the original text of a review and (1) parse it with spaCy,
(2) apply text pre-processing steps, (3) create a bag-of-words
representation, (4) create an LDA representation, and
(5) print a sorted list of the top topics in the LDA representation
"""

# parse the review text with spaCy
parsed_review = nlp(review_text)

# lemmatize the text and remove punctuation and whitespace
unigram_review = [token.lemma_ for token in parsed_review
                  if not punct_space(token)]

# apply the first-order and secord-order phrase models
bigram_review = bigram_model[unigram_review]
trigram_review = trigram_model[bigram_review]

# remove any remaining stopwords
trigram_review = [term for term in trigram_review
                  if not term in spacy.en.STOPWORDS]

# create a bag-of-words representation
review_bow = trigram_dictionary.doc2bow(trigram_review)

# create an LDA representation
review_lda = lda[review_bow]

# sort with the most highly related topics first
review_lda = sorted(review_lda, key=lambda (topic_number, freq): -freq)

for topic_number, freq in review_lda:
    if freq < min_topic_freq:
        break

    # print the most highly related topic names and frequencies
    print ('{:25} {}'.format(topic_names[topic_number],)
                            round(freq, 3))

弹出的错误是:

代码语言:javascript
复制
  File "<ipython-input-62-745b97e51bcb>", line 31
review_lda = sorted(review_lda, key=lambda (topic_number, freq): -freq)
                                           ^

SyntaxError:无效语法

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-09 03:25:31

在Python3中,您不能再使用元组参数解包了。您可以这样重写lambda:

代码语言:javascript
复制
review_lda = sorted(review_lda, key=lambda topic_and_freq: -topic_and_freq[1])
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46126438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档