TypeError: 'NoneType' object is not subscriptable when creating a pd.Dataframe dictionary

TypeError: 'NoneType' object is not subscriptable when creating a pd.Dataframe dictionary

我收到的错误是:TypeError: 'NoneType' object is not subscriptable

在这个方法中,我尝试对两个文件 (test&master) 进行字符串匹配。 主文件包含拼写正确的产品名称,而测试文件包含这些产品的拼写错误或拼写不同的版本。 我正在尝试将它们与 extractBests 函数匹配,以包含某个分数的截止值。此外,如果我正在打印输出的早期步骤,例如 fhp_new 变量,它仍然有效。

我认为错误是由以下事实引起的,即某些行不提供 score_cutoff 限制内的任何匹配项,因为当我将限制放在 say 上时我没有收到错误20. 所以理论上这些行应该保持为空,但我认为这会导致错误。

这行代码导致错误:

for x in range (1, num_match + 1):
            d["Match{0}".format(x)] = [y[0] for y in aggregated_matches["Match" + str(x)]]

这是错误行

之前的完整代码
def StringMatch (master, testfile, num_match=3, score_cutoff=95, limit=3):
        master_names = master.iloc[:,3]
        test_names = testfile.iloc[:,0]    
        fhp_new = [process.extractBests(x, master_names, score_cutoff=score_cutoff,limit=limit) for x in test_names]
        lab=" "
        i=1

        while i<=num_match:
            lab = lab + " " + "Match" + str(i)
            i = i+1
        aggregated_matches = pd.DataFrame(fhp_new, columns = lab.split())

        d={}
        for x in range (1, num_match + 1):
            d["Match{0}".format(x)] = [y[0] for y in aggregated_matches["Match" + str(x)]]
        print(d)

如果我没理解错的话,你只是想检查是否 y is None:

def StringMatch (master, testfile, num_match=3, score_cutoff=95, limit=3):
        master_names = master.iloc[:,3]
        test_names = testfile.iloc[:,0]    
        fhp_new = [process.extractBests(x, master_names, score_cutoff=score_cutoff,limit=limit) for x in test_names]
        lab=" "
        i=1

        while i<=num_match:
            lab = lab + " " + "Match" + str(i)
            i = i+1
        aggregated_matches = pd.DataFrame(fhp_new, columns = lab.split())

        d={}
        for x in range (1, num_match + 1):
            d["Match{0}".format(x)] = [None if y is None else y[0] for y in aggregated_matches["Match" + str(x)]]
        print(d)