python中的lambda表达式错误

2024-04-19 08:17:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试开发一个lambda表达式分类。以下是我目前的代码

import nltk
list2=nltk.word_tokenize("I win it")
list1 = ['gain', 'archive', 'win', 'success']

set1 = set(list1)
result = []
for item, nextitem in zip(list2, list2[1:]):
if item in set1:
    result.append(nextitem)
print result

new_result =nltk.pos_tag(result)
words, tags = zip(*new_result)

def classify(s, rls):
  for (f, emotion) in rls:
  if f(s):
     return emotion
 return "neutral"

rules = [(lambda x: x[result != []] & [['PRP' in(tags)]], "happy"),
     (lambda x: x[result != []] & [['PRP' not in(tags)]], "neutral"),]

 print classify("I successfully done my exam", rules)

但它会产生以下错误。你知道吗

rules = [(lambda x: x[result != []] & [['PRP' in(tags)]], "happy"),
TypeError: unsupported operand type(s) for &: 'str' and 'list'

如何修复此错误。请帮帮我!你知道吗


Tags: lambdainfortagsresultzipitemwin