2

After process the values from a stdin I got the following list in my python program:

[['92', '022'], ['82', '12'], ['77', '13']]

I am trying to have the values as:

[[92, 22], [82, 12], [77, 13]]

I have tried to map the values but found error:

print map(int, s)
Traceback (most recent call last):
  File "C:/Users/lenovo-pc/PycharmProjects/untitled11/order string.py", line 13, in <module>
    print map(int, s)
TypeError: int() argument must be a string or a number, not 'list'

Where s is my list.

Kindly, suggest me what is the optimized way to make the list of str to convert into integer.

0

1 Answer 1

2

Simple list comprehension:

>>> [ list(map(int,ele)) for ele in l ]

#driver values :

IN : l = [['92', '022'], ['82', '12'], ['77', '13']]
OUT : [[92, 22], [82, 12], [77, 13]]

The error :

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

is thrown since the map function takes in a flat iterable or list / 1D list in a loose sense. Since you are sending it a multi-dimensional list, it iterates over the sub-list and tries to apply the function on them and hence throws the error.

map(function, iterable, ...)

The iterable arguments may be a sequence or any iterable object; the result is always a list.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes this is what I was looking for thank you. I will accept after 12 minutes... :P
@JafferWilson : hahaha. Glad to help.
Might be worth adding a line explaining what the problem was and how it was solved? (namely, with a two dimensional list, map(int,s) was sending the sublists, not values, to int()
@ArthurSpoon : doing the edit at the moment. Was googling references to add.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.