Problem with list creation

Asked

Viewed 57 times

0

I’m trying to create a function that has as input a list. The function calculates the number of characters of each component of the list and returns another list of numbers. After that would have to put in ascending order the first list.

textos = ["casa", "mamamia", "jejum", "girimum"]
def calcula_assinatura(palavra):
    x = len(palavra)
    return x

 def verifica_assinatura(textos):
     assinatura = []
     for i in range (0,len(textos)):
       calcula_assinatura(i)
       assinatura = assinaura.append[i]
     x = assinatura.sort()
     return x
  • Look at the duplicate that was suggested above, as I understand it is what you want. If that’s not what you need, you can [Dit] the question explaining which result you want

  • 1

    really that was it, the way I was doing, I was writing a lot of code. thank you very much for the help even.

  • Hello Brunno, if you have managed to solve your problem, I ask you to mark the question as solved, in the symbol 'v' in the answer you answered, so that it is recorded that this question has already been solved. Thank you.

2 answers

1

Hello, I think this what you’ve been looking for. sorry for my Portuguese, I’m American and learning.

textos = ["casa", "mamamia", "jejum", "girimum"]


def calcula_assinatura(palavras):
    caracteres = []
    for palavra in palavras:
        caracteres.append(len(palavra))
    return caracteres


caracteres = calcula_assinatura(textos)
caracteres.sort()
print(caracteres)
  • Thanks for the attention and the code, at first it would look like this code, but the first answer cleared my doubts. At first I was planning to make it so, it helped a lot.

1

Greetings by Bruno Boff,

In the first function you were calculating the number of list elements and not the number of characters you had in each element.

I didn’t understand the second function, but if the intention was to return the first ordered list, then to simplify put as the second value returned in the same function.

palavras = ["casa", "mamamia", "jejum", "girimum"]

def calcula_assinatura(lista):
    total = []
    for i in lista:
        total.append(len(i))
    return total, sorted(lista)

total, palavras = calcula_assinatura(palavras)

print(total)
print(palavras)
  • The first answer fell like a glove, but thanks for the attention and the code helped in my development, to understand a little more of the structure.

Browser other questions tagged

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