Python:TypeError:"NoneType"对象不可下标 [英] Python: TypeError: 'NoneType' object is not subscriptable

查看:350
本文介绍了Python:TypeError:"NoneType"对象不可下标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#Uses python3

import sys

def max_dot_product(a,b,n):


    a = a.sort(reverse=True)
    b = b.sort(reverse=True)
    res = 0

    for i in range(n):
        res += a[i]*b[i]
    return res

if __name__ == '__main__':
    data = [int(x) for x in input("").split()]
    n = data[0]
    a = data[1:(n + 1)]
    b = data[(n + 1):]
    print(max_dot_product(a,b,n))

当我运行此代码时,它表明"TypeError:'NoneType'对象不可下标".

When I run this code, it reveals that "TypeError: 'NoneType' object is not subscriptable".

推荐答案

sort对列表进行排序,并(隐式)返回None.删除返回值的分配,您应该可以:

sort sorts a list in place and (implicitly) returns None. Drop the assignment of the return value and you should be OK:

a.sort(reverse=True)
b.sort(reverse=True)

这篇关于Python:TypeError:"NoneType"对象不可下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆