【问题标题】:TypeError: 'function' object is not subscriptable - length issueTypeError:“函数”对象不可下标 - 长度问题
【发布时间】:2021-11-03 17:19:25
【问题描述】:

我想知道什么更大?索引为 1 的数字的总和或列表前半部分的总和。我得到了这个错误。

TypeError: 'function' object is not subscriptable


def funky2(b):
    c = checker[:len(b)/2]
    a = sum(b) / len(b)
    if a > sum(c):
        return f'Statistic means is more than sum of 1 to half of list'
    else:
        return f'Statistic means is less than sum of 1 to half of list'



def checker(list):
    b = []
    for elem in list:
        if list.index(elem) % 3 == 1:
            b.append(elem)
    return funky2(b)
print(checker([1,2,3,4,5,6,7,8,9]))

【问题讨论】:

    标签: python list function sum


    【解决方案1】:

    checker[:len(b)/2] 导致问题,因为checker 是一个函数。相反,您想查看需要使用b 的列表的前半部分。此外,您应该使用// 进行整数除法,因为正常除法将返回一个浮点数;并且您不能按浮点数对列表进行切片。还有一点就是尽量避免变量名与一个类型相同(这里list

    def funky2(b):
        c = b[:len(b)//2]
        a = sum(b) / len(b)
        if a > sum(c):
            return f'Statistic means is more than sum of 1 to half of list'
        else:
            return f'Statistic means is less than sum of 1 to half of list'
    
    
    
    def checker(lst):
        b = []
        for elem in lst:
            if lst.index(elem) % 3 == 1:
                b.append(elem)
        return funky2(b)
    print(checker([1,2,3,4,5,6,7,8,9]))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2021-10-22
      • 2017-08-08
      • 2016-07-20
      相关资源
      最近更新 更多