python中的lambda表达式错误
我试图开发一个lambda表达式分类。以下是我到目前为止的代码python中的lambda表达式错误,python,list,lambda,nltk,Python,List,Lambda,Nltk,我试图开发一个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) pr
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'
如何修复此错误。请帮帮我
&
是位运算符,而不是逻辑的和和字和:规则=[(lambda x:x[result!=[]和[['PRP'in(tags)],“happy”),
可能值得注意的是一些表达式和['PRP'in(tags)]]
将具有与您刚刚编写的某个表达式本身相同的布尔结果。[[False]]
与[[True]]一样真实
。您试图编写的lambda函数的语义是什么?您的语法有几个问题。我认为您需要阅读有关lambda语法、布尔表达式、三元运算符的文档,以及……嗯,您在这里尝试使用的几乎每种小表达式类型。x in(列表)
在python中不是有效的语法。
列表中的x将是有效的。