[Tutor] Splitting lists with strings and integers
Mark Lawrence
breamoreboy at yahoo.co.uk
Wed Nov 27 02:13:00 CET 2013
On 27/11/2013 00:57, Dominik George wrote:
> Hi,
>
>> I have a list with mixed strings/integers. I want to convert this to a
>> list of lists, and I want the numbers to get stored as integers.
>
> First, let's do it with a list comprehension. You should really learn those if you do serious Python programming ;)!
>
> list2 = [[int(z) if z.isdigit() else z for z in y] for y in [x.split(" ") for x in list1]]
>
> Now, to convert every possible numeric string in a list to int in a more
> readable fashion:
>
> for x in xrange(len(animal)):
> if animal[x].isdigit():
> animal[x] = int(animal[x])
>
Before posting anything else would you please do a tutorial yourself.
The above for loop is appalling newbie code, I'll leave you to post the
Pythonic format.
--
Python is the second best programming language in the world.
But the best has yet to be invented. Christian Tismer
Mark Lawrence
More information about the Tutor
mailing list