2017-05-31 6 views
-3

リストインデックスはラインリストインデックス範囲エラーのうち3.6

if large_l[lg_index] ==small_l[sl_index]: 

で発生範囲パイソンのうちここでは、コードは以下の値である場合、正常に動作し

correctword = "syria" 
incorrectword = "siria" 

が、値を割り当てる場合次のように、

correctword = "syria" 
incorrectword = "syri" 

上記エラーが発生しました。可能であれば、このエラーを取り除く方法を教えてください。あなたはより多くのコードを確認する必要があり、そこ

if l1_item == l2_item: 

があるかもしれません他のバグ:

if large_l[lg_index] ==small_l[sl_index]: 

持つ: はおそらく、24行目の条件を置き換えたい

correctword = "syria" 
incorrectword = "syri" 

l1 = list(correctword) 
l2 = list(incorrectword) 

if len(l1) < len(l2): 
    large_l= l2 
    small_l = l1 
else: 
    large_l =l1 
    small_l = l2 

missmatchstart = False 
lg_mismatch_start_index = 0 
sl_mismatch_start_index = 0 
lg_index = 0 
sl_index = 0 
del_count=0 
add_count = 0 
sub_count = 0 
for l1_item in large_l: 
    for l2_item in small_l: 
     if large_l[lg_index] ==small_l[sl_index]: 
      if missmatchstart == False: 
       sl_mismatch_start_index = sl_index 
       lg_mismatch_start_index = lg_index 
      print(large_l[lg_index]) 
      print("addition ") 
      print(add_count) 
      print("deletion ") 
      print(del_count) 
      print("substitution ") 
      print(sub_count) 
      if lg_index-lg_mismatch_start_index == sl_index-sl_mismatch_start_index: 
       sub_count += sl_index-sl_mismatch_start_index 
       lg_index+= 1 
       sl_index+= 1 
      elif lg_index-lg_mismatch_start_index > sl_index-sl_mismatch_start_index: 
       sub_count += sl_index-sl_mismatch_start_index 
       del_count += ((lg_index-lg_mismatch_start_index) - (sl_index-sl_mismatch_start_index)) 
       lg_index+= 1 
       sl_index+= 1 
      elif lg_index-lg_mismatch_start_index < sl_index-sl_mismatch_start_index: 
       sub_count += lg_index-lg_mismatch_start_index 
       add_count += ((sl_index-sl_mismatch_start_index) - (lg_index-lg_mismatch_start_index)) 
       lg_index+= 1 
       sl_index+= 1 
      missmatchstart = False 
      break 
     else: 
      print(large_l[lg_index]) 
      if(missmatchstart == False): 
       lg_mismatch_start_index = lg_index 
       sl_mismatch_start_index = sl_index 
       print("missmatch large") 
       print(lg_mismatch_start_index) 
       print("missmatch small") 
       print(sl_mismatch_start_index) 
       missmatchstart =True 
       sl_index+=1 
      else: 
       sl_index+=1 
      if sl_index== len(small_l)-1: 
       lg_index +=1 
       sl_index = sl_mismatch_start_index 
       #del_count +=1 
       break 
      # elif lg_index == sl_index == len(small_l): 
      #  sub_count += 

if lg_index >=len(large_l)-1: 
    del_count += len(large_l)- lg_index 
#elif missmatchstart ==True: 

print(add_count) 
print(del_count) 
print(sub_count) 

答えて

1

ありがとうそれを機能に分割することによってモジュール化することができます(妥当な問題で) - 維持してデバッグするのがより簡単になります!

関連する問題