【问题标题】:IF statements not calling my data after continue statementIF 语句在 continue 语句后未调用我的数据
【发布时间】:2021-12-15 04:12:06
【问题描述】:

当我运行下面的代码时,我收到一堆行,说“没有结果”

我想通过使用string_len_test 函数来查看地址是否相似,然后每个地址是否不相似以进行某种模糊匹配。如果每次比较的结果都是某个值 % 值,那么我想对它们执行一些逻辑(现在我只有打印语句)。我知道我的数据具有很高的匹配分数,但它们似乎在 if/else 语句中被跳过了。

def string_len_test(l_value, v_value):
  if abs(len(l_value) - len(v_value)) >2:
    #print('not matched')
    return False
  elif abs(len(l_value) - len(v_value)) == 0:
    #print('potential match', l_value, v_value)
    return True
  else:
    return False
    

for link in Address1.itertuples():
  if link._5 == 'X':
      match_table = Adddress_main_X
  else:
      match_table = Adddress_main_Y

  for v in match_table.itertuples():
    
    if not string_len_test(link.Address_1, v.Address_2):
       continue 

    lev_score = lev(link.Address_1, v.Address2)
    fuzz_score = fuzz.token_sort_ratio(link.Address_1, v.Address2)
      #print(lev_score)
      #print(fuzz_score)
    if lev_score >=98 | fuzz_score >90:
      print('match', link.Address_1, v.Address2)
    if lev_score >= 80:
      print('close', link.Address_1, v.Address2)
    else:
      print('no results')

【问题讨论】:

  • 这是因为continuefor 循环中跳过了它之后的所有代码并移至循环中的下一个迭代。
  • edit您的问题并发布一个独立的minimal reproducible example,包括示例数据、所需结果、实际结果以及任何错误或追溯的全文。
  • 看起来模糊匹配码应该在if,你不应该有continue
  • 或者干脆去掉not
  • 哇哦,是的,让逻辑继续!

标签: python pandas dataframe if-statement fuzzywuzzy


【解决方案1】:

添加了一个 else pass 语句并去掉了 continue 语句。谢谢大家的帮助!

【讨论】:

    猜你喜欢
    • 2011-10-09
    • 2016-08-09
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 2018-01-27
    相关资源
    最近更新 更多