Max
Max

Reputation: 219

How to get length of specific object of a list

Why the following line work as I wish

print(len(lista[cont])-1)

but this one gets me an error

z = len(lista[cont]) - 1
lista.append(z)

The error message is:

TypeError: object of type 'int' has no len()

Why I can print the number of elements but cannot store the same value in a variable? Has some way to do this? This is my list and for eg list[0] need to return 15. RAW txt code can be found here.

[['1', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU'], ['2', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU'], ['3', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU'], ['4', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU'], ['5', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU'], ['6', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'ES', 'ES', 'CPU', 'CPU'], ['7', 'CPU', 'ES', 'CPU', 'ES', 'CPU', 'ES', 'CPU', 'ES', 'CPU'], ['8', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'ES', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'ES', 'ES', 'CPU', 'CPU']]

Full code:

lista = []

nomeArquivo = 'entrada.txt'
f = open(nomeArquivo,'r')
cont = 0

for a in f.readlines():
    linha = a.replace('\n', '')
    lista.append(linha.split(";"))
    z = len(lista[cont]) - 1
    lista.append(z)
    cont+=1
print(lista)

Upvotes: 1

Views: 309

Answers (1)

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20500

When you do lista.append(z) you are adding a integer to lista, and then when you try to len(lista[idx]) - 1, you end up trying to calculate the length of an integer, hence the exception TypeError: object of type 'int' has no len()

Instead you want to append the length at the end of the sublist you add using lista[idx].append(z). You would also want to use the with context manager to interact with the files

lista = []

#Open your file
with open('entrada.txt') as f:

    #Use enumerate to iterate over the lines and get index and element
    for idx, a in enumerate(f.readlines()):
        linha = a.replace('\n', '')
        lista.append(linha.split(";"))
        z = len(lista[idx]) - 1

        #Append length at the end of sublist
        lista[idx].append(z)

print(lista)

The output will be

[['1', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 15], 
['2', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 7], 
['3', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 'CPU', 28]
....

Upvotes: 1

Related Questions