u012585224
生风虎
采纳率100%
2017-10-09 01:17

TypeError: 'NoneType' object is not subscriptable

python3写的程序不知道这个错误怎么改,报错的内容在图片里面。图片说明

 # -*- coding: utf-8 -*-
"""
Created on Sun Oct  8 20:20:44 2017

@author: ll
"""

import numpy as np

def findMax1(array):
    if len(array)<1:#len(array)得到列表长度
        return [-1,-1]
    index = 1
    unit = array[index]
    for j in range(1,len(array)):
        if array[j] > array[index+1] and array[j] > array[index-1]:#if的含多条件语句的写法
            unit = array[j]
            return [unit,j]#返回的是一个列表类型,最大值和它的坐标1

def getMax1(matrix, column_Num):
    array = [col[column_Num] for col in matrix]#将找到的列循环遍历后放到列表中
    return findMax1(array)

def getPeak(matrix, leftCol, rightCol):
    if rightCol <= leftCol:
        r = getMax1(matrix,leftCol)
        r.append(leftCol)
    mid = int((leftCol + rightCol) / 2)
    unit = getMax1(matrix, mid)#返回的变量名要和接收传参的名字一样

    rightElem = getMax1(matrix, mid+1)
    if leftCol == rightCol - 1:
        leftElem = unit
        if leftElem[0] > rightElem[0]:
            r = leftElem
            r.append(mid)
        else:
            r = rightElem
            r.append(mid+1)
        return r

    leftElem = getMax1(matrix, mid-1)
    if unit[0] >= leftElem[0] and unit[0] >= rightElem[0]:
        unit.append(mid)
        return unit
    if unit[0] < leftElem[0]:
        return getPeak(matrix,leftCol,mid-1)
    return  getPeak(matrix,mid+1,rightCol)



print("please input row:")
n = int(input())
print("please input column:")
m = int(input())
matrix = np.random.randint(100,999,size=[n,m])
print(matrix)

result = getPeak(matrix,0,len(matrix[0])-1)

print(result)


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • yeahDeDiQiZhang Gods_Dusk 4年前

    你的unit,leftElem,rightElem中至少有一个是None,打印出来看看吧

    点赞 2 评论 复制链接分享

为你推荐