orders list lambda

0
alfabeto = {'t':0, 's':1, 'h':2, 'j':3, 'm':4, 'p':5, 'n':6, 'z':7, 'w':8, 'l':9, 'r':10, '    c':11, 'b':12, 'x':13, 'k':14, 'q':15, 'v':16, 'd':17, 'g':18, 'f':19}
d = open('txtA.txt', 'r')
ordenar = []
for line in d:
    for word in line.split():
        if word not in ordenar:
            ordenar.append(word)
        for c in ordenar:
            for i in c:
                ordenado = sorted(line.split(), key=(alfabeto.get(i, ord(i))))
print(ordenado)

This is a code to sort a text in a custom way, but it is not running.

The following error appears:

  

TypeError: 'int' object is not callable

And I do not understand why.

    
asked by anonymous 30.05.2018 / 21:49

2 answers

1

The problem is that alfabeto.get(i, ord(i)) will return an integer value, either the value that is in alfabeto or its ord . As you pass this result to key , it expects a callable object; as integer is not callable, gives the error.

In this case, you would have to create a function that returns the list of values of each word according to the alphabet, as I suggested in the other questions:

def lista_valores(palavra):
    return [alfabeto.get(i, ord(i)) for i in palavra]

And use this function as key :

for line in d:
    for word in line.split():
        if word not in ordenar:
            ordenar.append(word)
        for c in ordenar:
            for i in c:
                ordenado = sorted(line.split(), key=lista_valores)
print(ordenado)

But, so, the function uses the object alfabeto as global, which is not always a good idea and at this point the solution could be improved. In fact, there is a lot of repetition loop that did not make sense there, because the code below would generate the same result:

for line in d:
    ordenado = sorted(line.split(), key=lista_valores)
    print(ordenado)
    
30.05.2018 / 22:39
0

The problem is in key= , you are passing a number when you expect it to be a "function" or a lambda (as documented: link ).

That is sorted tries to execute the returned value of alfabeto.get(i, ord(i)) as a function, but as alfabeto.get returns one of the values in:

{'t':0, 's':1, 'h':2, 'j':3, 'm':4, 'p':5, 'n':6, 'z':7, 'w':8, 'l':9, 'r':10, '    c':11, 'b':12, 'x':13, 'k':14, 'q':15, 'v':16, 'd':17, 'g':18, 'f':19}

Or get the value ord(i) which is the alternative within .get (see documentation: link ) (I'm not sure, but this function I believe is only used in Python 2)

You have to be sure exactly what you want to use to sort, an example of using key= , assuming you want to compare alfabeto.get current with current item generated by line.split :

sorted(line.split(), key=lambda lineitem: lineitem == alfabeto.get(i, ord(i)))

It would be similar to do this:

def comparacao(lineitem):
     return lineitem == outrovaloracomparar

So after : in lambda would be the same as return

Note that the above codes are just examples, I really do not understand what kind of condition you want to go through for sorting, examples are just to understand how to use lambda or def with key= so just adapt your need.

    
30.05.2018 / 22:42