2011-08-11 133 views
6

我有一個產生錯誤的Python程序:類型錯誤: 'NoneType' 對象是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 

錯誤:

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

什麼是錯誤的意思是,如何解決?

+0

我的意思是我寫的只是這麼多在一個單獨的文件中第一次並與用戶提供的輸入工作。它是我研究項目的代碼。當我將這部分加入到大代碼中時,我正在研究從大文件列表中獲取輸入的位置,然後顯示此錯誤。 – zingy

回答

8

這部分代碼沒有問題。這意味着要麼

phonlist 

phonlist[next_row] 

不是list(或subscritable型),None。尋找你創建列表的位置。

編輯:

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

因爲錯誤是在第二行,我覺得phonlist[next_row]None

+0

謝謝。我編輯了這個問題,並添加了調用該函數的部分。是否因爲phonlist [next_row] [next_col]中沒有傳入值?與所有的比較一樣,使用phonlist [curr_row] [curr_col]。 – zingy

+0

對不起,有很多變量,函數,嵌套循環,很難弄清楚發生了什麼。 – utdemir

+0

我想我明白了。 – zingy

11

這是錯誤的Python,當您嘗試從None值(Python的null當量)訪問下標(索引)值返回:

>>> a = None 
>>> a[0] 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: 'NoneType' object is not subscriptable 
相關問題