Python: Unsupported error operand type(s) for /: tuple and 'int'

Asked

Viewed 3,340 times

2

Hi, I’m having a problem with a Python function, which involves a tuple stack. When performing this assembly function calling the tuple tuple votes:

votacoes = (
    (0, 15729, 220408, 1297, 0, 3040, 993, 0, 1354, 1046, 0, 3284, 99652, 19327, 0), 
    (0, 19000, 23173, 255, 0, 532, 201, 0, 306, 232, 0, 1980, 22307, 3890, 0), 
    (0, 23731, 244971, 1959, 0, 2710, 1465, 0, 1094, 1114, 0, 4264, 159476, 20488, 0), 
    (0, 1956, 47716, 282, 0, 0, 175, 0, 165, 247, 0, 417, 19728, 1732, 0),
    (0, 5384, 52325, 403, 0, 770, 543, 0, 428, 0, 0, 1454, 38317, 4609, 0), 
    (0, 14138, 113419, 662, 0, 2535, 600, 0, 591, 557, 0, 2014, 66199, 13034, 0),
    (0, 18967, 31260, 237, 0, 649, 216, 0, 168, 207, 0, 1810, 25010, 4225, 0),
    (0, 17255, 99745, 2076, 0, 3285, 0, 0, 1069, 700, 0, 3160, 46082, 16347, 0),
    (0, 3299, 53450, 251, 0, 520, 199, 0, 178, 191, 0, 755, 26263, 3114, 0),
    (0, 12351, 148762, 977, 0, 3029, 633, 0, 595, 453, 0, 2502, 51518, 0, 0), 
    (0, 111661, 560365, 4135, 0, 16913, 2410, 0, 5897, 4270, 0, 14419, 322034, 66874, 0),
    (0, 7910, 26257, 176, 0, 333, 162, 0, 151, 135, 0, 1031, 19963, 2753, 0), 
    (0, 61832, 488402, 2413, 0, 9072, 3386, 0, 1551, 1525, 0, 9640, 318113, 51002, 0),
    (0, 21347, 118028, 1454, 0, 2220, 692, 0, 832, 726, 0, 3413, 61194, 13712, 0),
    (0, 82159, 156444, 1682, 0, 6282, 1133, 0, 1595, 847, 0, 0, 112764, 29667, 0),
    (0, 6648, 76961, 384, 0, 926, 0, 0, 213, 331, 0, 1473, 35327, 5928, 0), 
    (0, 3656, 71840, 304, 0, 617, 254, 0, 147, 574, 0, 675, 34825, 2784, 0), 
    (0, 5810, 123184, 696, 0, 1229, 465, 0, 266, 626, 0, 1456, 54107, 5786, 0),
    (0, 2288, 53518, 314, 0, 756, 293, 0, 219, 271, 0, 669, 23189, 3965, 0),
    (0, 5096, 87597, 2560, 0, 2385, 2992, 0, 617, 538, 0, 1967, 20360, 5568, 0),
    (0, 803, 6306, 101, 0, 192, 83, 0, 48, 50, 0, 132, 7205, 602, 0),
    (0, 127, 8938, 87, 0, 0, 0, 0, 64, 47, 0,52, 2714, 165, 0)
)

def assembleia(t):
    lst_votos = list (t)
    lst = []
    for e in t:
        lst = lst + [0]    

    n = (16, 3, 19, 3, 4, 9, 3, 9, 4, 10, 47, 2, 39, 9, 18, 6, 5, 9, 5, 6, 2, 2)
    lst_mand = list (n)
    lstm = []
    for i in n:
        lstm = lstm + [0]        

    for z in n:
        while z > 0:
            maior = lst_votos[0]
            for i in range(0, len(lst_votos)):
                if lst_votos[i] > maior:
                    maior = lst_votos[i]         

            for j in range(0, len(lst_votos)):
                if maior == lst_votos[j]:
                    p = j     

            lst[p] = lst[p] + 1

            lst_votos = lst_votos[:p] + [(lst_votos[p] * lst[p]) / (lst[p] +1)] + lst_votos[p+1:]

            z = (z - 1)

    return tuple(lst) 

The mistake that gives is:

line 61, in <module>
    lst_votos = lst_votos[:p] + [(lst_votos[p] * lst[p]) / (lst[p] +1)] + lst_votos[p+1:]
builtins.TypeError: unsupported operand type(s) for /: 'tuple' and 'int'

I can understand why that mistake happened, but I can’t seem to get around it and make it work.... I can make it work if you do it one-on-one, now calling Thiuplo, broke... The idea is that later a Tuple is returned with the results of the distribution of votes (then I will have to add this return, but I haven’t put it in the code).

  • hmm, it’s kind of hard to answer your question. The mistake you’ve already noticed: list_votos[p] is a tuple and lst[p] is a whole then list_votos[p] * lst[p] is a tuple and tuples cannot be the numerator of a division. But I’m not quite sure what you want the program to actually do to suggest a fix.

  • Thanks for the reply. What I want is for the program to make a calculation according to the D'Hondt method. Each number inside the list n is a number of seats to be distributed according to the vote that is present in the corresponding Pool within votacoes.

  • The output would be a tuple stack with the result of seating distribution. And I can make it work if I give each element individually, like this: assembleia(16, (0, 15729, 220408, 1297, 0, 3040, 993, 0, 1354, 1046, 0, 3284, 99652, 19327, 0)) get the expected return (0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0)

1 answer

2


One problem with your code is that you are manipulating tuples, when in fact you would like to manipulate whole numbers that are part of these tuples.

The problem comes down to accessing data in matrices, i.e., tuples and multidimensional lists.

For example, when you start your function, you’re just turning the outside tuple into a list, not the internal tuples:

def assembleia(t):
    lst_votos = list(t)

In that case, you’d just be doing that:

((10, 20, 30), (40, 50, 60))

If that were to become:

[(10, 20, 30), (40, 50, 60)]

Suppose, after all, that:

lst_votos = [(10, 20, 30), (40, 50, 60)]

Further on, when you do maior = lst_votos[0], is passing the first tuple as value for maior. What will entail in:

>>> print(maior)
(10, 20, 30)

In this context, you want to assign the first value of this tuple to the variable maior, then you must pass the first element of the first tuple:

>>> lst_votos = [(10, 20, 30), (40, 50, 60)]
>>> maior = lst_votos[0][0]
>>> print(maior)
10

If you had three dimensions:

>>> exemplo = [((1, 2), 20, 30), (40, 50, 60)]
>>> print(exemplo[0][0][0])
1

To fix this, you will need to change several points of your code. I advise you to divide the problem into smaller parts within functions:

  • A function that resolves the allocation of mandates relative to a tuple, taking as arguments the number of mandates and a voting tuple, as assembly();
  • Another function that takes as arguments the mandate tuple and the tuple of voting tuples, which leads to a loop by calling the assembly() function n times.

So you won’t have to access multidimensional data and make your code clearer.

  • Thank you so much for your reply. It really helped :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.