Dec-28-2016, 04:18 PM
Here a hint for integer convert.
Can wirte a helper function that catch
>>> my_list = [['john', '1', '2:00pm'], ['becky', '2', '3:15pm'], ['tom', '3', '3:20pm']] >>> [[int(x) for x in lst] for lst in my_list]
Error:Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
File "<interactive input>", line 1, in <listcomp>
File "<interactive input>", line 1, in <listcomp>
ValueError: invalid literal for int() with base 10: 'john
Now it try to convert all element in the nested list.Can wirte a helper function that catch
ValueError
def try_int(x): try: return int(x) except ValueError: return xNow try again:
>>> [[try_int(x) for x in lst] for lst in my_list] [['john', 1, '2:00pm'], ['becky', 2, '3:15pm'], ['tom', 3, '3:20pm']]
Quote:3rd element to date.time?Do you know how to this,like with only 1 value
'3:20pm'
?