string - 类型错误:'在' 需要字符串作为左操作数,而不是浮点数
问题描述
我有这个使用假日期的代码(如下),它工作正常:
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)
输出如下:只显示fruit_list中包含单词的句子。
I love apple.
there are lots of health benefits of apple.
apple is especially hight in Vitamin C,
然后我将fruit_list更改为我的真实数据(real_list),真实数据来自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
我确定问题出在 real_list 上。而且我无法在此处显示 real_list(希望您能理解),但是之前有人遇到过此错误消息吗?知道可能是什么原因吗?请发送帮助。非常感谢!!
解决方案
固定的:
real_list 中有一个“N/A”,它是一个浮点数。
谢谢你们的评论!!
推荐阅读
- angular - 错误:无法匹配任何路由。URL 段:Ionic Angular App 中的“null”
- c++ - 无法编译 std::reduce 调用,而 std::accumulate 调用编译相同的参数
- android - FIREBASE - 从 Play 商店更新后删除和创建的实时数据库记录
- c++ - 获取正在运行的进程的pid
- jquery - 按钮未禁用且文本值在单击后不会更改
- node.js - 如何在 package.json 脚本之间传递参数
- python - 如何在 Visual Studio (2017) 中禁用 Python 的错误曲线
- javascript - axios回调中的Vue实时更新props数组
- php - PHP/SQL - 注册页面中的提示立即出现,无需输入任何内容
- c - 在C中删除链表的第一个元素