String 类型错误:';在<;字符串>';需要字符串作为左操作数,而不是浮点
我有这个代码使用假日期(如下),它工作良好:String 类型错误:';在<;字符串>';需要字符串作为左操作数,而不是浮点,string,python-3.x,loops,String,Python 3.x,Loops,我有这个代码使用假日期(如下),它工作良好: exg = ["I love apple.", "there are lots of health benefits of apple.", "apple is especially hight in Vitamin C,", "alos provide Vitamin A as a powerful antioxidant!"] fruit_list = ["pear", "banana",
exg = ["I love apple.",
"there are lots of health benefits of apple.",
"apple is especially hight in Vitamin C,",
"alos provide Vitamin A as a powerful antioxidant!"]
fruit_list = ["pear", "banana", "mongo", "blueberry", "kiwi", "apple", "orange"]
for j in range(0, len(exg)):
sentence = exg[j]
if any(word in sentence for word in fruit_list):
print(sentence)
输出如下:仅显示水果列表中包含单词的句子
I love apple.
there are lots of health benefits of apple.
apple is especially hight in Vitamin C,
然后我将水果列表更改为我的真实数据(真实列表),真实数据来自excel工作表中的一列。读取数据代码如下所示:
import pandas as pd
data = pd.read_excel('C:/Users/my/Desktop/my_list.xlsx', 'Sheet1')
real_list = data['name'].tolist()
但我的代码不再工作,错误如下所示:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-101-5a886e386099> in <module>()
7 for j in range(0, len(exg)):
8 sentence = exg[j]
----> 9 if any(word in sentence for word in real_list):
10 print(sentence)
<ipython-input-101-5a886e386099> in <genexpr>(.0)
7 for j in range(0, len(exg)):
8 sentence = exg[j]
----> 9 if any(word in sentence for word in real_list):
10 print(sentence)
TypeError: 'in <string>' requires string as left operand, not float
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
7对于范围(0,len(exg))内的j:
8句=exg[j]
---->9如果有(在真实列表中逐字逐句):
10印刷品(句子)
英寸(.0)
7对于范围(0,len(exg))内的j:
8句=exg[j]
---->9如果有(在真实列表中逐字逐句):
10印刷品(句子)
TypeError:“in”需要字符串作为左操作数,而不是浮点
我相信问题出在真正的清单上。我不能在这里显示真正的_列表(希望你能理解),但是以前有人遇到过这个错误消息吗?你知道是什么原因吗?请派人来帮忙。非常感谢 固定:
real_列表中有一个“N/a”,这是一个浮动
谢谢你的评论 我肯定也是。但是我想看看。当没有给出重现问题的方法时,诊断问题并不容易。嗨,谢谢你的评论。对不起,我不能给你看真正的清单。如果有帮助的话,real_list是一个产品名称列表,因此可能会有标点符号(如&、-、(,)、空格等)。该列表包含700多个元素。这有帮助吗?@S.J你能确认你的
real_列表
只包含字符串而不包含任何十进制数字(浮动)吗?@S.J你应该对列表进行二进制搜索,缩小哪些项目可能会引起悲伤,然后将内容隐藏到非私有字符串中,如果你想在这个问题上获得帮助,可以与我们共享。是的,可能它们不是字符串,可能其中一些值被转换为无,或者数字,您可以尝试在任意调用中将单词转换为字符串(如果有的话)(水果列表中的单词对应句子中的str(单词):