'List'对象不可调用

时间:2014-10-06 03:12:47

标签: python sorting search

我有一个分配程序,它从文本文件中生成一个整数列表。然后,它使用插入或冒泡排序对数据进行排序。我选择了冒泡排序。我可以在必要时切换,但它们都是O(n ^ 2)(我相信)。整数表示街道上的距离。目标是通过查找列表的中位数来计算打开新商店的最佳位置。但是,当我运行到目前为止,我得到一个错误,指出'list'对象不可调用。我不明白这个错误。

def make_list(perkList,fileName):


    with open(fileName) as f:
        for line in f.readlines():
            line = line.split()
            perkList.append(int(line[1]))# reason for this is there is a string in line[0]
    return perkList


def perkSort(perkList):

    sorted = False
    while not sorted:
        sorted = True
        for i in range(len(perkList)-1):
            if perkList[i] > perkList[i+1]:
                sorted = False
                perkList[i], perkList[i+1] = perkList[i+1], perkList[i]
    print("sorted List",perkList)




def median(perkList):
    middle = len(perkList)//2
    if middle % 2 == 0:
        return (perkList(middle) + perkList(middle - 1))//2
    else:
        return perkList(middle)
def main()
    perkList = []
    fileName = input("Enter file name: ")
    make_list(perkList,fileName)
    print(perkList)
    perkSort(perkList)
    median(perkList)
main()

我的名单是[70,120,170,200]

Traceback (most recent call last): sorted List [70, 120, 170, 200] File "C:/Users/User'/PycharmProjects/messes/findSlow.py", line 43, in <module> median(perkList) File "C:/Users/User'/PycharmProjects/messes/findSlow.py", line 34, in median return (perkList(middle) + perkList(middle - 1))//2 TypeError: 'list' object is not callable

1 个答案:

答案 0 :(得分:0)

而不是:

perkList(middle)

你需要这个:

perkList[middle]