sort list c lambda

Asked

Viewed 90 times

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, only it’s not running.

The following error appears:

Typeerror: 'int' Object is not callable

And I don’t understand why.

  • On what line exactly?

  • sorted = Sorted(line.split(), key=(alphabet.get(i, Ord(i))) nessa

  • Somewhere you redefined one of the functions of the line?

  • What do you mean? ;-; .. ;-;

2 answers

1

The problem is that alfabeto.get(i, ord(i)) will return an integer value, whatever the value is in alfabeto or its ord. How do you pass this result to key, it waits for a searchable object; since the whole is not searchable, it 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, just as I suggested in the other questions:

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

And use such a 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 thus the function uses the object alfabeto as a whole, which is not always a good idea, and here the solution could be improved. In fact, there is a lot of loop of repetition 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)
  • Ahhh sorry. is that this is not the whole code. the other two for’s are for another part of the code. That if, is because the text has some repeated words, and I want them to appear only once in the ordered list. but thank you very much. I had not understood right

0

The problem is in key=, you are passing a number when the expected should be a "function" or a lambda (as documented: https://docs.python.org/3/library/functions.html#sorted).

That is to say sorted tries to execute the value returned from 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 take the amount ord(i) which is the alternative within the .get (see documentation: https://docs.python.org/2.4/lib/typesmapping.html) (I’m not sure, but this function I believe is only used in Python 2)

Have to be sure of exactly what you want to sort, an example of using the key=, supposing you want to compare the alfabeto.get with the current item generated by line.split:

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

Seria similar to do this:

def comparacao(lineitem):
     return lineitem == outrovaloracomparar

Then after the : in the lambda would be the same as the return

Note that the above codes are only examples, I don’t really understand what kind of condition you want to pass to ordering, the examples are just to understand how to use lambda or def with key=, then just adapt your need.

Browser other questions tagged

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