user8044502
user8044502

Reputation: 13

Converting list strings to integers

I'm trying to converting string lists to integers but it always fails with the same error no matter which method I use to convert.

The error is TypeError: int() argument must be a string or a number, not 'list'.

Here's the code that I've tried:

#list2 = [int(s) for s in list1]
#list2 = map(int, list1)
try:
    for i in list1 :
        list2.append(int(list1));
except :
    print "The int conversion failed"

print list2

The initial list, list1, just contains some string numbers:

[['4183'], ['4034'], ['3342'], ['3482'], ['8567'], ['1052'], ['8135'], ['5561'], ['517'], 
 ['1218'], ['8877']]

How can get I avoid the list input error?

Upvotes: 1

Views: 104

Answers (5)

nucleon
nucleon

Reputation: 1158

If the inner lists always have length 1:

list2 = map(lambda xs: int(xs[0]), list1)

For arbitrary length inner lists:

from functools import partial
list2 = map(partial(map, int), list1)

Upvotes: 0

Gabriel Belini
Gabriel Belini

Reputation: 779

The error you get is because you'e trying to convert your whole list into integer. The correct way to do that would be list2.append(int(i)). But this also won't work because you have a 2D array and not a simple list. When you loop through your array using for i in list1you are getting a list with 1 element on it every iteration.

['4183'] # 1st iteration
['4034'] # 2nd iterativo etc...

The problem with that is you can't converte a list into an integer directly, even if it have only 1 element on it.

To solve that you could do another for loop for every i (very inapropriate approach, but works!). Or you can try to flatten your original list and then your for loop will work (just don't forget to change list1to i).

Although, I think that the best way to solve your problem, a more "pythonic" approach to this problem would be using the built-in function map. And do something like: map(list1, int). In this approach you would also need to flatten your list first.

Upvotes: 0

When you say that something is, for example, i = ['3342'], you're saying it's a list with one string ('3342').

Furthermore, when you use a for loop, you should refer to each object in the list as the variable you declared after the for:

list1 = ['4183', '4034', '3342', '3482', '8567', '1052', '8135', '5561', '517', '1218', '8877']
list2 = []
try:
    for i in list1 :
        list2.append(int(i))
except :
    print("The int conversion failed")

print(list2)

Upvotes: 1

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476534

Well the problem is that you list1 contains a list of list of strings. Not a list of strings.

It depends on what you want.

  • If you want a list of ints, you can do:

    [int(x) for line in list1 for x in line]
    

    This generates:

    >>> [int(x) for line in list1 for x in line]
    [4183, 4034, 3342, 3482, 8567, 1052, 8135, 5561, 517, 1218, 8877]
    
  • If on the other hand, you want a list of list of ints, you can work with:

    [[int(x) for x in line] for line in list1]
    

    This generates:

    >>> [[int(x) for x in line] for line in list1]
    [[4183], [4034], [3342], [3482], [8567], [1052], [8135], [5561], [517], [1218], [8877]]
    

Upvotes: 4

Óscar López
Óscar López

Reputation: 235994

You're dealing with a list of lists of strings. This should work, for obtaining a list of integers:

lst = [['4183'], ['4034'], ['3342'], ['3482'], ['8567'], ['1052'], ['8135'], ['5561'], ['517'], ['1218'], ['8877']]

[int(x[0]) for x in lst]
=> [4183, 4034, 3342, 3482, 8567, 1052, 8135, 5561, 517, 1218, 8877]

Or, if you intend to keep the list of lists, but with integers:

[[int(x[0])] for x in lst]
=> [[4183], [4034], [3342], [3482], [8567], [1052], [8135], [5561], [517], [1218], [8877]]

Upvotes: 2

Related Questions