Python3 Typeerror: Unsupported operand type(s) for +: 'int' and 'list'

Asked

Viewed 2,434 times

0

Gentlemen, starting in python and cracking my head here... I have a cvs file with hundreds of lines, (I know that pandas would be ideal for this, but as I said I’m starting, and I need to understand some concepts before). And in this file I need to read only the last 100 lines, and a few columns (so far ok, I was able to do), but csv returns me a list of lists and I want to join all the lists in just one type this, but in integers. Obs.: The map function is not converting to integers Ex.: lista = [['1','2','3'], ['4','5','6']] list = [1, 2, 3, 4, 5, 6] follows the program below:

import csv
with open('arquivo.csv', 'r', encoding='utf-8') as arq:
    linhas = csv.reader(arq, delimiter=',')
    dados = list(linhas)
    dados = dados[1730:1830]
    map(int, dados)

    print([dados[2:17] for dados in dados]) # Aqui imprime corretamente da coluna 2 à 17
    print(sum([dados[2:17] for dados in dados])) # mas se eu tentar juntar aqui me retorna o erro citado acima.```

  • map(int, dados), this line of code is doing nothing. It does not modify dados, it has a return that needs to be assigned to a variable.

1 answer

1


The problem was basically due to an inconsistent call to the function sum:

sum([dados[2:17] for dados in dados])

If the documentation:

sum(iterable[, start])

Sums start and the items of an iterable from left to right and Returns the total. start defaults to 0. The iterable’s items are normally Numbers, and the start value is not allowed to be a string.

Where it is possible to check that the default value of start is 0. You have a list of lists, something like [['1', '2'], ['3', '4']], then in the first iteration within the function sum the operation equivalent to result = start + ['1', '2']; as start is 0, basically you will be adding a int with a list, giving the cited error.

A simple way around this would be to set the initial value, start, as an empty list, thus the operation equivalent to result = [] + ['1', '2']. To do this, just define the second argument value of the function sum:

sum([dados[2:17] for dados in dados], [])

Some caveats about your code:

  • In doing dados = list(linhas) you will store the entire contents of the file in memory. If the read file is too large it may lock the application. For each row of the file an object will be created list and for each item in the list an object str, which would require even more memory than the file size itself;

  • The line map(int, dados) has no use in the code the way it is. What function map makes returns an iterator that applies the defined function on the elements of the informed iterable, in this case applies int in the items of dados. The function does not modify the parameters, so if you do not assign the return of the function in any variable it will be in vain in the code;

To bypass the first one, you can work directly on the iterators, without loading everything in memory. For example, linhas will be an eternal with the lines of your file, as you just want the lines between 1730 and 1830, just you take that part of the eternal:

linhas_que_voce_quer = itertools.islice(linhas, 1730, 1830)

This will return another everlasting, but only to the lines you need. All others will not be loaded in memory (not at the same time, at least). From it you can use the function map to convert to integer and function sum to join, or use the function itertools.chain.from_iterable, that also makes this unification.

Browser other questions tagged

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