for spamicities in sorted(map(lambda body: self.word_spamicity(body),re.sub("[^\w]", " ", body).split())
,key=lambda x: abs(0.5 - x),reverse=True)[0:38]:
hamicities = map(lambda x: 1-x, spamicities)
垃圾邮件是一个浮动,我收到错误TypeError: 'float' object is not iterable
如何修复我的代码?
答案 0 :(得分:0)
这将失败:
spamicities = 1.0
hamicities = map(lambda x: 1-x, spamicities)
map
仅适用于列表。因此,如果您想使用map
,请将您的浮动放入列表中:
spamicities = [1.0]
hamicities = map(lambda x: 1-x, spamicities) # returns [0.0]
更传统的例子(多个列表值):
spamicities = [1.0, 2.0, 3.0, 20.0]
hamicities = map(lambda x: 1-x, spamicities) # returns [0.0, -1.0, -2.0, -19.0]
我用完整的代码更新了我的问题
摆脱你的for循环,因为它迭代sorted
返回的列表并一次给你一个浮点数。你想要整个列表如下:
spamicities = sorted(map(lambda body: self.word_spamicity(body),re.sub("[^\w]", " ", body).split()),key=lambda x: abs(0.5 - x),reverse=True)[0:38]
hamicities = map(lambda x: 1-x, spamicities)