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])
So:
- the isdigit() method of a string tells you if it is numeric
- int() casts to an integer
The list comprehension does the whole converion with list
comprehensions. You should consider readability when doing such things,
but you should learn it.
-nik
--
# apt-assassinate --help
Usage: apt-assassinate [upstream|maintainer] <package>
PGP-Fingerprint: 3C9D 54A4 7575 C026 FB17 FD26 B79A 3C16 A0C4 F296
signature.asc
Description: Digital signature
_______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
