如何解决此列表索引超出范围错误

发布于 2025-01-12 09:11:53 字数 2310 浏览 0 评论 0原文

我想从两个数据集(一个 lambda 另一个参考)和 fuzzywuzzy(python 中的 levensthein 模型)获取正确的船只标记和模型列表,但我的代码中有一个问题,我不理解

这两个数据集:

< a href="https://www.transfernow.net/dl/202203070QxpVjYJ" rel="nofollow noreferrer">https://www.transfernow.net/dl/202203070QxpVjYJ

有我的代码:

   #%%
    from fuzzywuzzy import process
    import pandas as pd
    
    #%%
    BASE_LAMBDA_PATH = '../ressources/marques_modeles_lambda_entier.csv'
    BASE_REF_PATH = '../ressources/marques_modeles_ref_entier.csv'
    #%%
    lambda_df = pd.read_csv(BASE_LAMBDA_PATH, sep=";")
    #%%
    ref_df = pd.read_csv(BASE_REF_PATH, sep=";")
    
    #%% j'ai créé ma liste de résultat (initée à vide)
    df_result = pd.DataFrame(columns=['marque', 'lambda','ref','score'])
    
    #%% je parcours ma table de modèles lambda
    for ind in lambda_df.index:
        marque = lambda_df['MARQUE_REF'][ind]
        modele_lambda = lambda_df['MODELE'][ind]
        ref_list = (ref_df[(ref_df['lib_marque'] == marque)]['lib_model']).to_list()
        choices = process.extract(modele_lambda, ref_list, limit=1)
        approx = choices[0][0]
        score = choices[0][1]
        df2 = pd.DataFrame(data = [(marque, modele_lambda, approx, score)],\
             columns=['marque', 'lambda','ref','score'])
        df_result = pd.concat([df_result, df2], axis=0, ignore_index=True)
    
    df_result.to_csv('output_matching_groupe.csv', sep=';', index=False)
    
    '''
    tdep = time.time()
    tfin = time.time()
    print(f"duree de {tfin-tdep} secondes")
    '''
    # %%

错误:

    IndexError                                Traceback (most recent call last)
    c:\Users\boats\src\list_matching_groupe.py in <cell line: 1>()
          20 ref_list = (ref_df[(ref_df['lib_marque'] == marque)]['lib_model']).to_list()
          21 choices = process.extract(modele_lambda, ref_list, limit=1)
    ----> 22 approx = choices[0][0]
          23 score = choices[0][1]
          24 df2 = pd.DataFrame(data = [(marque, modele_lambda, approx, score)],\
          25      columns=['marque', 'lambda','ref','score'])
    
    IndexError: list index out of range

我不明白它,因为 choices[0][0] 实际上我获得的作品:'Guy Couach 1401'

i want to obtain proper list of marks and models of boats from two dataset (one lambda an another of reference) with fuzzywuzzy (levensthein model in python) but i have an issue in my code that i don't understand

the two datasets:

https://www.transfernow.net/dl/202203070QxpVjYJ

there is my code :

   #%%
    from fuzzywuzzy import process
    import pandas as pd
    
    #%%
    BASE_LAMBDA_PATH = '../ressources/marques_modeles_lambda_entier.csv'
    BASE_REF_PATH = '../ressources/marques_modeles_ref_entier.csv'
    #%%
    lambda_df = pd.read_csv(BASE_LAMBDA_PATH, sep=";")
    #%%
    ref_df = pd.read_csv(BASE_REF_PATH, sep=";")
    
    #%% j'ai créé ma liste de résultat (initée à vide)
    df_result = pd.DataFrame(columns=['marque', 'lambda','ref','score'])
    
    #%% je parcours ma table de modèles lambda
    for ind in lambda_df.index:
        marque = lambda_df['MARQUE_REF'][ind]
        modele_lambda = lambda_df['MODELE'][ind]
        ref_list = (ref_df[(ref_df['lib_marque'] == marque)]['lib_model']).to_list()
        choices = process.extract(modele_lambda, ref_list, limit=1)
        approx = choices[0][0]
        score = choices[0][1]
        df2 = pd.DataFrame(data = [(marque, modele_lambda, approx, score)],\
             columns=['marque', 'lambda','ref','score'])
        df_result = pd.concat([df_result, df2], axis=0, ignore_index=True)
    
    df_result.to_csv('output_matching_groupe.csv', sep=';', index=False)
    
    '''
    tdep = time.time()
    tfin = time.time()
    print(f"duree de {tfin-tdep} secondes")
    '''
    # %%

the error:

    IndexError                                Traceback (most recent call last)
    c:\Users\boats\src\list_matching_groupe.py in <cell line: 1>()
          20 ref_list = (ref_df[(ref_df['lib_marque'] == marque)]['lib_model']).to_list()
          21 choices = process.extract(modele_lambda, ref_list, limit=1)
    ----> 22 approx = choices[0][0]
          23 score = choices[0][1]
          24 df2 = pd.DataFrame(data = [(marque, modele_lambda, approx, score)],\
          25      columns=['marque', 'lambda','ref','score'])
    
    IndexError: list index out of range

I don't understand it because choices[0][0] actually works i obtain: 'Guy Couach 1401'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文