【问题标题】:Try Except Pass while creating a dictionary在创建字典时尝试除通过
【发布时间】:2021-01-28 15:37:25
【问题描述】:

我有很多数据框,我想根据前缀和后缀将它们分组到字典中:

prefixes = ['one_season_bucket',
'two_season_bucket',
'three_season_bucket',
'four_season_bucket']

后缀:

suffixes = ['racer_bio',
'spring_rate',
'neaps_rate',
'spring_raw',
'neap_raw',
'opposing_team',
'opposing_team_distribution',
'stern_score',
'bow_score',
'team_score']

一些数据框名称的示例:

...
two_season_bucket_year1_racer_bio
two_season_bucket_year1_spring_rate
two_season_bucket_year1_neaps_rate
two_season_bucket_year1_spring_raw
two_season_bucket_year1_neap_raw
two_season_bucket_year1_opposing_team
two_season_bucket_year1_opposing_team_distribution
two_season_bucket_year1_stern_score
two_season_bucket_year1_bow_score
two_season_bucket_year1_team_score
four_season_bucket_year4_racer_bio
four_season_bucket_year4_spring_rate
...
four_season_bucket_year4_neaps_rate
four_season_bucket_year4_spring_raw
four_season_bucket_year4_neap_raw
four_season_bucket_year4_opposing_team
four_season_bucket_year4_opposing_team_distribution
four_season_bucket_year4_stern_score
four_season_bucket_year4_bow_score
four_season_bucket_year4_team_score

基本上我想使用这些列表来制作以数据框名称为键,数据框为值的字典,由前缀分解,包括所有后缀,例如:

two_season_bucket_suffixes = {'two_season_bucket_year1_racer_bio':two_season_bucket_year1_racer_bio,
'two_season_bucket_year1_spring_rate':two_season_bucket_year1_spring_rate,
'two_season_bucket_year1_neaps_rate':two_season_bucket_year1_neaps_rate,
'two_season_bucket_year1_spring_raw':two_season_bucket_year1_spring_raw,
'two_season_bucket_year1_neap_raw':two_season_bucket_year1_neap_raw,
'two_season_bucket_year1_opposing_team':two_season_bucket_year1_opposing_team,
'two_season_bucket_year1_opposing_team_distribution':two_season_bucket_year1_opposing_team_distribution,
'two_season_bucket_year1_stern_score':two_season_bucket_year1_stern_score,
'two_season_bucket_year1_bow_score':two_season_bucket_year1_bow_score,
'two_season_bucket_year1_team_score':two_season_bucket_year1_team_score,
'two_season_bucket_year2_racer_bio':two_season_bucket_year2_racer_bio,
'two_season_bucket_year2_spring_rate':two_season_bucket_year2_spring_rate,
'two_season_bucket_year2_neaps_rate':two_season_bucket_year2_neaps_rate,
'two_season_bucket_year2_spring_raw':two_season_bucket_year2_spring_raw,
'two_season_bucket_year2_neap_raw':two_season_bucket_year2_neap_raw,
'two_season_bucket_year2_opposing_team':two_season_bucket_year2_opposing_team,
'two_season_bucket_year2_opposing_team_distribution':two_season_bucket_year2_opposing_team_distribution,
'two_season_bucket_year2_stern_score':two_season_bucket_year2_stern_score,
'two_season_bucket_year2_bow_score':two_season_bucket_year2_bow_score,
'two_season_bucket_year2_team_score':two_season_bucket_year2_team_score}

所以我用这段代码来做这个:

from itertools import product
years = ['year1', 'year2']
prefix = 'two_season_bucket'
two_season_bucket_suffixes = {'%s_%s_%s' %(pre, y, suff): eval('%s_%s_%s' %(pre, y, suff)) for y, suff in product(years, suffixes)}

但是有些存储桶不存在,例如“two_season_bucket_year2_opposing_team”可能不存在。每次我运行代码时,某些代码可能不存在。所以我想添加一些东西来忽略任何可能不存在的数据框并继续制作字典:

two_season_bucket_suffixes = try:
     {'%s_%s_%s' %(pre, y, suff): eval('%s_%s_%s' %(pre, y, suff)) for y, suff in product(years, suffixes)}
 except:
     pass

但我认为我做得不对。

【问题讨论】:

  • 是的,你猜对了,try 是一个应该将代码包装在一个块中的语句,它不是一个返回值的函数,你不能为你的 try 语句分配一个变量。定义一个空字典,编写一个简单的循环(不理解),并在循环的每一步将您想要的值分配给您的字典。要将其包装在 try except 语句中,只需使用 try except 语句将您的分配包装到字典中。编辑:这与@piyush 的回答背后的逻辑相同
  • 避免使用一般的 except 语句。如果发生另一个错误,您将无法看到它,因此请使用except YourException:。如果循环花费太多时间,您将无法中断您的代码,除非您终止整个进程(仅仅是因为 KeyboardInterrupt 是一个异常,并且您要求您的程序在收到任何类型的异常时通过)。
  • "但是有些桶不存在,例如 'two_season_bucket_year2_opposing_team' 可能不存在" 而不是尝试这样做,您应该修复分配给这些变量的代码,以便它直接插入而是字典。例如,考虑 stackoverflow.com/questions/1373164/…reddit.com/r/learnpython/wiki/… 中的建议

标签: python pandas


【解决方案1】:
from itertools import product
years = ['year1', 'year2']
prefix = 'two_season_bucket'
two_season_bucket_suffixes = {'%s_%s_%s' %(pre, y, suff): eval('%s_%s_%s' %(pre, y, suff)) for y, suff in product(years, suffixes) if '%s_%s_%s' %(pre, y, suff) in globals()}

【讨论】:

    【解决方案2】:

    try ... except 构造的代码如下所示:

    try:
       ...
    except:
       ...
    

    所以你的代码会变成:

    try:
       two_season_bucket_suffixes = {'%s_%s_%s' %(pre, y, suff): eval('%s_%s_%s' %(pre, y, suff)) for y, suff in product(years, suffixes)}
    except:
       pass
    

    但是,这将无法捕获代码中发生的任何异常。 理想情况下,您希望跳过与 'two_season_bucket_year2_bow_score' 不存在相关的错误。您可以通过查看 python 控制台的错误提要找到与您的问题相关的异常。但是我认为它将是KeyError,在这种情况下,您的代码将变成:

    try:
       two_season_bucket_suffixes = {'%s_%s_%s' %(pre, y, suff): eval('%s_%s_%s' %(pre, y, suff)) for y, suff in product(years, suffixes)}
    except KeyError:
       pass
    

    【讨论】:

      【解决方案3】:

      解决方案:
      此代码 sn-p 可能会解决您的问题:

      def return_keyval_pair(years, suffixes):
          final_dict = {}
          for y, suff in product(years, suffixes):
              try:
                  final_dict['%s_%s_%s' %(pre, y, suff)] = eval('%s_%s_%s' %(pre, y, suff))
              except:
                  pass
          return final_dict
      
      two_season_bucket_suffixes = return_keyval_pair(years, suffixes)
      

      【讨论】:

        猜你喜欢
        • 2017-09-16
        • 2022-01-04
        • 2017-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多