2011-08-11 45 views
6

tôi có một chương trình python mà tạo ra một lỗi:Lỗi Loại: đối tượng 'NoneType' là unsubscriptable

def update_ranges(phonemelist) : 
    """ updating the rows and columns of the list of input phonemes""" 
    # make a copy of the list as we're going to modify it (optional) 
    phonlist = phonemelist[:] 
    # we don't need the row titles, they just complicate things 
    rowtitles, phonlist = zip(*phonlist) 
    rows = len(phonlist) 
    columns = range(len(phonlist[0])) 

    # for each row except the last 
    for i in xrange(rows - 1): 
     # update it by going down all the rows below it 
     for k in xrange(i+1, rows): 
      # for both columns 
      for j in columns: 
       update(phonlist, i, j, k, j) 

    # put the row titles back in (optional) 
    phonlist = zip(rowtitles, phonlist) 
    return phonlist 

def update(phonlist, curr_row, curr_col, next_row, next_col) : 
    """ applying co-articulation rules for comparing the ranges """ 
    curr_low, curr_high = phonlist[curr_row][curr_col] 
    next_low, next_high = phonlist[next_row][next_col] 

    # Rule 1: when one of the ranges is (-1,-1) 
    # replace the current range if it's (-1, -1) as its empty 
    if (curr_low, curr_high) == (-1, -1) : 
     phonlist[curr_row][curr_col] = next_low, next_high 
     return 
    # do nothing if the next range is (-1,-1) 
    if (next_low, next_high) == (-1, -1) : 
     return 

    # Rule 2: when ranges don't overlap 
    # replace the lowest value of current range when the next range is lower than the current range 
    elif curr_low > next_high : 
     phonlist[curr_row][curr_col] = curr_low, curr_low 
     return 
    # replace the highest values of current range when the next range is higher than the current range 
    elif next_low > curr_high : 
     phonlist[curr_row][curr_col] = curr_high, curr_high 
     return 

    # Rule 3: when ranges completely overlap 
    elif curr_low <= next_low and next_high <= curr_high or curr_low >= next_low and next_high >= curr_high : 
     # replace the values of the next range when the next range completely lies in the current range 
     if curr_high - curr_low > next_high - next_low : 
      phonlist[curr_row][curr_col] = next_low, next_high 
      return 
     # replace the values of the current range when the current range completely lies in the next range 
     else : 
      phonlist[curr_row][curr_col] = curr_low, curr_high 
      return 

    # Rule 4: when the ranges partially overlap 
    else : 
     # replace the values that is common to both ranges when next range is further ahead of the current 
     if curr_low < next_low and curr_high < next_high : 
      phonlist[curr_row][curr_col] = next_low, curr_high 
      return 
     # replace the values that is common to both ranges when current range is further ahead of the next 
     else : 
      phonlist[curr_row][curr_col] = curr_low, next_high 
      return 

Lỗi:

File "coarticulation.py", line 217, in update 
    next_low, next_high = phonlist[next_row][next_col] 
TypeError: 'NoneType' object is unsubscriptable 

không lỗi có nghĩa là gì và làm thế nào để sửa chữa nó?

+0

Ý tôi là tôi đã viết chỉ nhiều này trong một file riêng biệt đầu tiên và làm việc với đầu vào do người dùng cung cấp. Và đó là mã của dự án nghiên cứu của tôi. Khi tôi tham gia phần này vào mã lớn tôi đang làm việc trên nơi nó có đầu vào từ tập tin đó là một danh sách lớn sau đó nó cho thấy lỗi này. – zingy

Trả lời

8

Nó không phải là một vấn đề với phần mã này. Nó có nghĩa là một trong hai

phonlist 

hoặc

phonlist[next_row] 

không phải là một list (hoặc một loại subscritable), một None. Tìm kiếm nơi bạn tạo danh sách.

Edit:

curr_low, curr_high = phonlist[curr_row][curr_col] 
next_low, next_high = phonlist[next_row][next_col] 

Bởi vì lỗi là ở dòng thứ hai, tôi nghĩ phonlist[next_row]None.

+0

Cảm ơn bạn. Tôi đã chỉnh sửa câu hỏi và thêm phần mà chức năng này đang được gọi. Có phải vì không có giá trị nào được chuyển vào danh sách phát [next_row] [next_col] không? Như với tất cả các so sánh phonlist [curr_row] [curr_col] được sử dụng. – zingy

+0

Xin lỗi, có rất nhiều biến, hàm, vòng lặp lồng nhau, thật khó để tìm ra những gì đang xảy ra. – utdemir

+0

Tôi nghĩ mình đã hiểu. – zingy

11

Đây là lỗi mà Python trả về khi bạn cố gắng truy cập vào một giá trị subscripted (lập chỉ mục) từ giá trị None (Python của null tương đương):

>>> a = None 
>>> a[0] 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: 'NoneType' object is not subscriptable 
Các vấn đề liên quan