Lambda函数无法解压的值太多 [英] Too many values to unpack in lambda function

查看:76
本文介绍了Lambda函数无法解压的值太多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Python.我正在使用API​​构建TFIDFs模型,但是lambda函数遇到了一些我无法解决的错误.这是生成TFIDF的类的一部分:

I just started learning Python. I'm using an API to build a TFIDFs model, however I'm facing some errors with the lambda functions which I cannot resolve. This is part of the class that generates the TFIDFs:

class tfidf(ModelBuilder, Model):

    def __init__(self, max_ngram=1, normalize = True):
        self.max_ngram = max_ngram
        self.normalize = normalize

    def build(self, mentions, idfs):
        m = mentions\
            .map(lambda (target, (span, text)): (target, text))\  """error is triggered here  """
            .mapValues(lambda v: ngrams(v, self.max_ngram))\
            .flatMap(lambda (target, tokens): (((target, t), 1) for t in tokens))\
            .reduceByKey(add)\
            .map(lambda ((target, token), count): (token, (target, count)))\
            .leftOuterJoin(idfs)\

这是mentions类的示例输出(这是引起tdfidf类错误的输入):

And here is a sample output of the mentions class (which is the input causing the error in tdfidf class):

Out[24]:                                                                        
[{'_id': u'en.wikipedia.org/wiki/William_Cowper',
  'source': 'en.wikipedia.org/wiki/Beagle',
  'span': (165, 179),
  'text': u'References to the dog appear before the 19th century in works by such writers as William Shakespeare, John Webster, John Dryden, Thomas Tickell, Henry Fielding, and William Cowper, as well as in Alexander Pope\'s translation of Homer\'s "Iliad".'},
 {'_id': u"en.wikipedia.org/wiki/K-Run's_Park_Me_In_First",
  'source': 'en.wikipedia.org/wiki/Beagle',
  'span': (32, 62),
  'text': u" On 12 February 2008, a Beagle, K-Run's Park Me In First (Uno), won the Best In Show category at the Westminster Kennel Club show for the first time in the competition's history."},

错误消息是:

 .map(lambda (target, (span, text)): (target, text))\
ValueError: too many values to unpack

我尝试了:.map(lambda ( src, target, span, text) : (target, text))\,因为我只需要在mentions\中引起相同错误的目标和文本.

I tried: .map(lambda ( src, target, span, text) : (target, text))\ since I only need the target and text which causes the same error in mentions\.

一个简单且可编译的示例:

A simple and a compilable example:

import math
import numpy


Data = [{'_id': '333981',

  'source': 'Apple',

  'span': (100, 119),

  'text': ' It is native to the northern Pacific.'}, {'_id': '27262',

  'source': 'Apple',

  'span': (4, 20),

  'text': ' Apples are yummy.'}]



m = map(lambda (ID, (span, text)) : (ID, text) , Data)

print(list(m))

我正在使用python 2.7.任何帮助或指导将不胜感激.

I'm using python 2.7. Any help or guidance will be much appreciated.

非常感谢,

推荐答案

如果要创建仅包含字段sourcetext的新字典数组,则可以使用

If you want to create a new array of dictionaries containing only the fields source and text, you could use

m = map(lambda item: {field: item.get(field) for field in ['source', 'text']}, Data)

如果要创建键sourcetext的内容的元组数组:

If you want to create an array of tuples of the contents for the keys source and text:

m = map(lambda item: (item.get('source'), item.get('text')), Data)

这篇关于Lambda函数无法解压的值太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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