首页 > 解决方案 > 类型错误:'在' 需要字符串作为左操作数,而不是浮点数

问题描述

我有这个使用假日期的代码(如下),它工作正常:

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(希望您能理解),但是之前有人遇到过此错误消息吗?知道可能是什么原因吗?请发送帮助。非常感谢!!

标签: stringpython-3.xloops

解决方案


固定的:

real_list 中有一个“N/A”,它是一个浮点数。

谢谢你们的评论!!


推荐阅读