Python列表不可调用错误

时间:2017-10-31 21:26:11

标签: python

代码(很多,抱歉):

#nea practice

def re_election():
    print("A re-election is needed")


choice = int(input("Which test vote file would you like to use: "))

correct_choices = [1, 2]

while choice not in correct_choices:
    choice = int(input("Error, try again: "))
if choice == 1:
    filename = "Test1_Votes.txt"
elif choice == 2:
    filename = "Test2_Votes.txt"

with open(filename, 'r'):
    f = open(filename)
    lines = f.readlines()
    linecount = 0
    candidates = ['a', 'b', 'c', 'd', 'e']
    for line in lines:
        linecount +=1

    valid_votes = linecount
    num_seats = 2
    quota = int((valid_votes/(num_seats + 1))+ 1)
    print("\n")
    print("Quota is: " + str(quota))
    all_lines = []
    a_scores = []
    b_scores = []
    c_scores = []
    d_scores = []
    e_scores = []

    for line in lines:
        line = line.rstrip("\n")  # strip new line character
        lst = [int(x) for x in line.split(',')]  # split line and cast to int
        all_lines.append(lst)
        a_scores.append(lst[0])
        b_scores.append(lst[1])
        c_scores.append(lst[2])
        d_scores.append(lst[3])
        e_scores.append(lst[4])

    print(all_lines)

    #fp
    a_fp_votes = a_scores.count(1)
    b_fp_votes = b_scores.count(1)
    c_fp_votes = c_scores.count(1)
    d_fp_votes = d_scores.count(1)
    e_fp_votes = e_scores.count(1)

    all_fp_votes = [a_fp_votes, b_fp_votes, c_fp_votes, d_fp_votes, e_fp_votes]
    print(all_fp_votes)

    candidates_fp_votes = dict(zip(candidates, all_fp_votes))

    #sp
    a_sp_votes = a_scores.count(2)
    b_sp_votes = b_scores.count(2)
    c_sp_votes = c_scores.count(2)
    d_sp_votes = d_scores.count(2)
    e_sp_votes = e_scores.count(2)

    all_sp_votes = [a_sp_votes, b_sp_votes, c_sp_votes, d_sp_votes, e_sp_votes]
    candidates_sp_votes = dict(zip(candidates, all_sp_votes))

    #tp
    a_tp_votes = a_scores.count(3)
    b_tp_votes = b_scores.count(3)
    c_tp_votes = c_scores.count(3)
    d_tp_votes = d_scores.count(3)
    e_tp_votes = e_scores.count(3)

    all_tp_votes = [a_tp_votes, b_tp_votes, c_tp_votes, d_tp_votes, e_tp_votes]
    candidates_tp_votes = dict(zip(candidates, all_tp_votes))

    all_new_fp_votes = all_fp_votes
    candidates_new_fp_votes = candidates_fp_votes


    if (sum([x >= quota for x in [a_fp_votes, b_fp_votes, c_fp_votes, d_fp_votes, e_fp_votes]])) >= 2:
        print("Quota Achieved")
        quota_achieved = True



    else:
        print("Quota Not Achieved")
        quota_achieved = False

    if quota_achieved == True:

        """
        Tiebreaker system 
        """
        final_vals = [a for a, b in candidates_fp_votes.items() if
                      list(candidates_fp_votes.values()).count(b) > 1]
        print("Duplicates are: ")
        for i in final_vals:
            print(i)
        print(final_vals)

        if not final_vals:
            print("No tiebreaker sys required")
            winners = []
            for i in range(2):
                winner = max(candidates_fp_votes, key=candidates_fp_votes.get)
                winners.append(winner)
                del candidates_fp_votes[winner]
            print("Winners are: " + winners[0] + " and " + winners[1])
            f.close()
            exit()

        else:
            print("Implementing step 1")
            candidates_fp_votes[final_vals[0]] = candidates_fp_votes[final_vals[0]] + candidates_sp_votes[
                final_vals[0]]
            candidates_fp_votes[final_vals[1]] = candidates_fp_votes[final_vals[1]] + candidates_sp_votes[
                final_vals[1]]

            step1_vals = [a for a, b in candidates_fp_votes.items() if
                          list(candidates_fp_votes.values()).count(b) > 1]
            print("Duplicates are: ")
            for i in step1_vals:
                print(i)
            if not step1_vals:
                print("No tiebreaker sys required")
                winners = []
                for i in range(2):
                    winner = max(candidates_fp_votes, key=candidates_fp_votes.get)
                    winners.append(winner)
                    del candidates_fp_votes[winner]

                print("Winners are: " + winners[0] + " and " + winners[1])
                f.close()
                exit()

            else:
                print("Implementing step 2b")
                candidates_fp_votes[step1_vals[0]] = candidates_fp_votes[step1_vals[0]] + \
                                                           candidates_tp_votes[final_vals[0]]
                candidates_fp_votes[step1_vals[1]] = candidates_fp_votes[step1_vals[1]] + \
                                                           candidates_tp_votes[final_vals[1]]

                step2b_vals = [a for a, b in candidates_fp_votes.items() if
                               list(candidates_fp_votes.values()).count(b) > 1]
                print("Duplicates are: ")
                for i in step2b_vals:
                    print(i)
                print(step2b_vals)

                if not step2b_vals:
                    print("No tiebreaker sys required")
                    winners = []
                    for i in range(2):
                        winner = min(candidates_fp_votes, key=candidates_fp_votes.get)
                        winners.append(winner)
                        del candidates_fp_votes[winner]
                    print("Winners are: " + winners[0] + " and " + winners[1])
                    f.close()
                    exit()

                else:
                    print("There is a tie breaker which cannot be resolved between " + step2b_vals[0] + " and " +
                          step2b_vals[1])
                    f.close()
                    exit()

    """
    STV
    """

    eliminated = 0



    while quota_achieved == False:

        new_candidates = ['a', 'b', 'c', 'd', 'e']
        eliminated_candidate = min(candidates_new_fp_votes, key=candidates_new_fp_votes.get)
        print(eliminated_candidate + " has been eliminated ")
        eliminated +=1

        candidate_positions_in_list = [0, 1, 2, 3, 4]
        candidate_and_positions = dict(zip(candidates, candidate_positions_in_list))

        eliminated_candidate_position_in_list = (candidate_and_positions.get(eliminated_candidate))

        for i, row in enumerate(all_lines):
        #traversing each column
           for j, c in enumerate(row):
           #in each column in the row check for '1', if found replace by '0'
              if j == eliminated_candidate_position_in_list and c== 1:
                 row[j] = 0
                 #in the same row check for '2', if found replace my 1
                 if 2 in row:
                    ind = row.index(2)
                    row[ind] = 1

        print(all_lines)

        a_new_scores = []
        b_new_scores = []
        c_new_scores = []
        d_new_scores = []
        e_new_scores = []

        for list in all_lines:
            a_new_scores.append(list[0])
            b_new_scores.append(list[1])
            c_new_scores.append(list[2])
            d_new_scores.append(list[3])
            e_new_scores.append(list[4])

        a_new_fp_votes = a_new_scores.count(1)
        b_new_fp_votes = b_new_scores.count(1)
        c_new_fp_votes = c_new_scores.count(1)
        d_new_fp_votes = d_new_scores.count(1)
        e_new_fp_votes = e_new_scores.count(1)

        all_new_fp_votes = [a_new_fp_votes, b_new_fp_votes, c_new_fp_votes, d_new_fp_votes, e_new_fp_votes]
        candidates_new_fp_votes = dict(zip(new_candidates, all_new_fp_votes))
        all_candidates_fp_votes = candidates_new_fp_votes

        # sp
        a_new_sp_votes = a_new_scores.count(2)
        b_new_sp_votes = b_new_scores.count(2)
        c_new_sp_votes = c_new_scores.count(2)
        d_new_sp_votes = d_new_scores.count(2)
        e_new_sp_votes = e_new_scores.count(2)

        all_new_sp_votes = [a_new_sp_votes, b_new_sp_votes, c_new_sp_votes, d_new_sp_votes, e_new_sp_votes]
        candidates_new_sp_votes = dict(zip(new_candidates, all_new_sp_votes))

        # tp
        a_new_tp_votes = a_new_scores.count(3)
        b_new_tp_votes = b_new_scores.count(3)
        c_new_tp_votes = c_new_scores.count(3)
        d_new_tp_votes = d_new_scores.count(3)
        e_new_tp_votes = e_new_scores.count(3)

        all_new_tp_votes = [a_new_tp_votes, b_new_tp_votes, c_new_tp_votes, d_new_tp_votes, e_new_tp_votes]
        candidates_new_tp_votes = dict(zip(new_candidates, all_new_tp_votes))

        if eliminated_candidate == 'a':
            candidates.remove('a')
            try:
                all_new_fp_votes.remove(a_new_fp_votes)
            except ValueError:
                pass

        elif eliminated_candidate == 'b':
            candidates.remove('b')
            try:
                all_new_fp_votes.remove(b_new_fp_votes)
            except ValueError:
                pass

        elif eliminated_candidate == 'c':
            candidates.remove('c')
            try:
                all_new_fp_votes.remove(c_new_fp_votes)
            except ValueError:
                pass

        elif eliminated_candidate == 'd':
            candidates.remove('d')
            try:
                all_new_fp_votes.remove(d_new_fp_votes)
            except ValueError:
                pass

        elif eliminated_candidate == 'e':
            candidates.remove('e')
            all_new_fp_votes.remove(e_new_fp_votes)

        del candidates_new_fp_votes
        candidates_new_fp_votes = dict(zip(new_candidates, all_new_fp_votes))

        if (sum([x >= quota for x in [a_new_fp_votes, b_new_fp_votes, c_new_fp_votes,
                                      d_new_fp_votes, e_new_fp_votes]])) >= 2:
            print("Quota Achieved")
            quota_achieved = True
            """
            Tiebreaker system 
            """
            print(all_candidates_fp_votes)
            final_vals = [a for a, b in candidates_new_fp_votes.items() if list(candidates_new_fp_votes.values()).count(b) > 1]
            print("Duplicates are: ")
            for i in final_vals:
                print(i)
            print(final_vals)

            if not final_vals:
                print("No tiebreaker sys required")
                winners = []
                for i in range(2):
                    winner = max(candidates_new_fp_votes, key=candidates_new_fp_votes.get)
                    winners.append(winner)
                    del candidates_new_fp_votes[winner]
                print("Winners are: " + winners[0] + " and " + winners[1])
                f.close()
                exit()

            else:
                print("Implementing step 1")
                candidates_new_fp_votes[final_vals[0]] = candidates_new_fp_votes[final_vals[0]] + candidates_new_sp_votes[
                    final_vals[0]]
                candidates_new_fp_votes[final_vals[1]] = candidates_new_fp_votes[final_vals[1]] + candidates_new_sp_votes[
                    final_vals[1]]

                step1_vals = [a for a, b in candidates_new_fp_votes.items() if
                              list(candidates_new_fp_votes.values()).count(b) > 1]
                print("Duplicates are: ")
                for i in step1_vals:
                    print(i)
                if not step1_vals:
                    print("No tiebreaker sys required")
                    winners = []
                    for i in range(2):
                        winner = max(candidates_new_fp_votes, key=candidates_new_fp_votes.get)
                        winners.append(winner)
                        del candidates_new_fp_votes[winner]
                    print("Winners are: " + winners[0] + " and " + winners[1])
                    f.close()
                    exit()

                else:
                    print("Implementing step 2b")
                    candidates_new_fp_votes[step1_vals[0]] = candidates_new_fp_votes[step1_vals[0]] + \
                                                             candidates_new_tp_votes[final_vals[0]]
                    candidates_new_fp_votes[step1_vals[1]] = candidates_new_fp_votes[step1_vals[1]] + \
                                                             candidates_new_tp_votes[final_vals[1]]

                    step2b_vals = [a for a, b in candidates_new_fp_votes.items() if
                                   list(candidates_new_fp_votes.values()).count(b) > 1]
                    print("Duplicates are: ")
                    for i in step2b_vals:
                        print(i)
                    print(step2b_vals)

                    if not step2b_vals:
                        print("No tiebreaker sys required")
                        winners = []
                        for i in range(2):
                            winner = min(candidates_new_fp_votes, key=candidates_new_fp_votes.get)
                            winners.append(winner)
                            del candidates_new_fp_votes[winner]
                        print("Winners are: " + winners[0] + " and " + winners[1])

                    else:
                        print("There is a tie breaker which cannot be resolved between " + step2b_vals[0] + " and " +
                              step2b_vals[1])
                        f.close()
                        exit()
        else:
            if eliminated == 2:
                re_election()
            else:
                quota_achieved = False
                print("Quota Not Achieved")
                pass
            pass

Txt文件样本:

1,2,3,0,0

0,0,3,2,1

1,0,0,3,2

1,0,0,2,3

很抱歉让您阅读所有代码。 我不得不包括我的所有代码,因为这是整个代码的问题,我无法让你更简单。

在代码的第293行中,我尝试了candidates_new_fp_votes的值列表,但它没有列出清单。在我的IDE中始终为紫色的list不再是紫色。这不是我的IDE的错,因为我在默认的python IDE中尝试了相同的操作,并且还将我的代码放入另一个python文件中,同样的情况仍然存在。我认为我的代码中的某些内容会导致这种情况发生,但我不知道。

当我运行代码并选择2(上面的txt文件示例)时。它抛出错误:

final_vals = [a for a, b in candidates_new_fp_votes.items() if list(candidates_new_fp_votes.values()).count(b) > 1]
TypeError: 'list' object is not callable

我假设这是candidates_new_fp_votes.values无法成为列表。

我已经尝试将此错误谷歌连续2个小时,结果对我来说毫无希望。

有任何帮助吗? 提前谢谢。

2 个答案:

答案 0 :(得分:0)

不要覆盖python中的任何内置函数/方法/语句 你已经覆盖了列表():

for list in all_lines:
    a_new_scores.append(list[0])
    b_new_scores.append(list[1])
    c_new_scores.append(list[2])
    d_new_scores.append(list[3])
    e_new_scores.append(list[4])

因为列表变量覆盖list()之后会出现异常。它[变量]不可调用:

TypeError: 'list' object is not callable

答案 1 :(得分:0)

我已经覆盖了列表():

for list in all_lines:
    a_new_scores.append(list[0])
    b_new_scores.append(list[1])
    c_new_scores.append(list[2])
    d_new_scores.append(list[3])
    e_new_scores.append(list[4])

我有一个例外,因为我通过列表变量覆盖了list()。它不可调用:

TypeError: 'list' object is not callable

相反,我应该说:

for lst in all_lines:
    a_new_scores.append(lst[0])
    b_new_scores.append(lst[1])
    c_new_scores.append(lst[2])
    d_new_scores.append(lst[3])
    e_new_scores.append(lst[4])

因为这不会覆盖列表()。