2016-08-13 9 views
0

私はPythonには新しくありません。私はjavascriptでコーディングしていますので、私はすべてのエラーに慣れていません。forループでif文を使用しているときにリストのインデックスが範囲外になりました

私は空のオブジェクトにforループを使ってプロパティを追加しようとしていますが、リストのインデックスが範囲外であるというエラーが表示されます。私のforループの中にif文を追加すると、エラーが起きているようです。私はseats_info配列をループするインデックスを使用していません。どうしてこんなことが起こっているのか理解してください、ありがとう!

seats_info = ['\n1,133,0,A', '\n1,133,2,C', '\n1,133,1,B', '\n1,133,4,E', '\n1,133,3,D', '\n2,132,0,24', '\n2,132,1,25', '\n2,132,2,26'] 

    def read_manifest(self, manifest): 
    file = manifest_dir + '/' + manifest 
    data = open(file, 'r') 
    seats_info = data.read().split('\r')[1:] 
    data.close() 

    self.seat_information = {} 
    for seat_info in seats_info: 
     one_seat = seat_info.split(',') 
     section_id = one_seat[0][1:] 
     one_seat.remove(one_seat[0]) 
     one_seat.insert(0, section_id) 

     if one_seat[1] not in self.seat_information: 
      self.seat_information[one_seat[1]] = [one_seat] 
     else: 
      self.seat_information[one_seat[1]].append(one_seat) 


     Error Message that I received 

     Traceback (most recent call last): 
     File "path/normalizer.py", line 75, in <module> 
normalizer.read_manifest(citifield) 
     File "path/normalizer.py", line 36, in read_manifest 
    if one_seat[1] not in self.seat_information: 
    IndexError: list index out of range 
+0

ここではprintステートメントのデバッグを使用することをおすすめします。ファイル形式は、おそらくあなたの期待から逸脱しているかもしれません。ループ内に 'print seat_info'や' print one_seat'を置くことで、より多くのことができます。 –

+0

Javascriptでは、範囲外の配列インデックスにアクセスする際にもエラーがスローされます。 –

+0

one_seat = seat_info.split( '、') section_id = one_seat [0] [1:]のようなインデックス値をハードコーディングしていますが、seat_info.split( '、')がそれを超える可能性が高い範囲。あなたのデータをチェックしてください。あなたがわからないときにtryブロックを使用することを推奨し、例外 – slysid

答えて

0

エラーを再現するために、データファイルに空白行を挿入します。この時点で

section_id = one_seat[0][1:] 
one_seat[0] = section_id 

、one_seat = [ '']、そう正しく上昇する範囲のうち何one_seat [1]従って

if one_seat[1] ... 

リストインデックスは存在しません。

代わりに、ハードコードされたインデックスを使用する場合は、前提条件が成立していることを確認してください。ここではサンプルの再書き込みです:

def execute(): 
    seats_info = ['\n', '\n1,133,0,A', '\n1,133,2,C', '\n1,133,1,B', '\n1,133,4,E', '\n1,133,3,D', '\n2,132,0,24', '\n2,132,1,25', '\n2,132,2,26'] 
    for seat_info in seats_info: 
     one_seat = seat_info.split(',') 
     if len(one_seat): 
      section_id = one_seat[0][1:] 
      one_seat[0] = section_id 
      # maybe check for a legal section_id here? 
      if len(one_seat) > 1: 
       row_id = one_seat[1]     
       if row_id not in seat_information: 
        seat_information[row_id] = [one_seat] 
       else: 
        seat_information[row_id].append(one_seat) 


seat_information = {} 
execute() 
for row, seats in seat_information.items(): 
    print "row", row 
    for seat in seats: 
     print "seat:", seat 
-1

ケニー・オストロムによって溶液が十分ですが、私は実際にそれを読んだ後、前処理データファイルを示唆している。この方法は、あなたは常にチェックする必要はありません。前提条件を満たし、コードを簡素化します。具体的には、私は心の中で次のように持っていた:

seats_info = ['\n', '\n1,133,0,A', '\n1,133,2,C', '\n1,133,1,B', '\n1,133,4,E', '\n1,133,3,D', '\n2,132,0,24', '\n2,132,1,25', '\n2,132,2,26'] 

#remove new line and other markup characters 
seats_info = [str.strip(s) for s in seats_info] 

#remove elements that have no content:  
seats_info = [s for s in seats_info if len(s)>0] 

ので、今、seats_infoはきれいですし、あなたにエラーがスローされません。

In [21]: seats_info 
Out[21]: 
['1,133,0,A', 
'1,133,2,C', 
'1,133,1,B', 
'1,133,4,E', 
'1,133,3,D', 
'2,132,0,24', 
'2,132,1,25', 
'2,132,2,26'] 

あなたが\nをインデックス化が、今している現在ので、

for seat_info in seats_info: 
    one_seat = seat_info.split(',') 
    section_id = one_seat[0] 

    if one_seat[1] not in seat_information: 
     self.seat_information[one_seat[1]] = [one_seat] 
    else: 
     self.seat_information[one_seat[1]].append(one_seat) 

最後に、私は非常にwithステートメントを使用することをお勧め:に、あなたのデータ抽出ループを簡素化する必要がありますする必要はありません。あなたのデータを目で見て分かりやすく読むことができます。私はもっとPythonicです。具体的には、read_manifest関数の次の定義を考慮してください。

def read_manifest(self, manifest): 
    file = manifest_dir + '/' + manifest 

    #read in the file: 
    with open(file, 'r') as f: 
     #because we later use str.strip, you don't need to do [1:] to get rid of \r 
     seats_info = f.read().split('\r') 

    #pre-process: 
    seats_info = [str.strip(s) for s in seats_info]  
    seats_info = [s for s in seats_info if len(s)>0] 
関連する問題