我有一个称为sowpods的单词列表,我需要验证哪种字母组合作为一个单词或一个单词存在。
For example, if my letters are ['r', 't', 'e', 'f']
, one of the possible combinations is 're'
which is within 'red'
, therefore the word 'red' should be kept.
我已经有一些代码可以找出所有可能的组合,但是现在我想找到如何将所有符合要求的单词添加到列表中。
我已经完成以下工作:
import pandas as pd
sowpods = pd.read_csv('sowpods.csv', names=['Word'])
possible_combination = 'RE'
possible_words = pd.DataFrame([], columns=['Word'])
comb_in_word = lambda _: True if (possible_combination in _) else False # ------ line 8
sowpods_bool = sowpods['Word'].apply(comb_in_word) # --------------------------- line 10
possible_words.append(sowpods.loc[sowpods_bool, 'Word'])
但是然后我得到:
File "c:\tests.py", line 10, in <module>
sowpods_bool = sowpods['Word'].apply(comb_in_word)
File "C:\Python38-32\lib\site-packages\pandas\core\series.py", line 3848, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)
File "pandas\_libs\lib.pyx", line 2329, in pandas._libs.lib.map_infer
File "c:\Users\lenovo\OneDrive\Prog\Projects\Scrabble\tests.py", line 8, in <lambda>
comb_in_word = lambda _: True if possible_combination in _ else False
TypeError: argument of type 'float' is not iterable
我在一个更受控制的环境中测试了我的lambda函数,并且该函数运行良好,因此我确信该错误不是从那里来的。
我不明白为什么我自己不遍历任何东西时为什么会收到此错误。我知道熊猫正在遍历DataFrame的列,但在使用浮点数而不是整数的情况下不应出现错误。