I have the following list of lists (denoted by lol
):
JavaScript
x
6
1
[['365', '336', '365', 'E<;EjD'],
2
['365', '336', '365', 'E<;EkD'],
3
['365', '336', '365', 'E<;F0D'],
4
['0', '0', '335', 'E<;GaQ'],
5
['0', '0', '335', 'E<;GbQ']]
6
I am trying to convert lol
in such a way that each first three strings of a sub-list to be converted to int
and each last string of the same sub-list to be converted to datetime
(dateConverter()
is to convert string to daytime).
I am expecting to have as output the following:
JavaScript
1
6
1
[[365, 336, 365, '2021-12-11T21:58:20'],
2
[365, 336, 365, '2021-12-11T21:58:20'],
3
[365, 336, 365, '2021-12-11T21:59:20'],
4
[0, 0, 335, '2021-12-11T22:0:20'],
5
[0, 0, 335, '2021-12-11T22:1:20']]
6
I tried the following:
- I knew how to convert the first three strings to
int
.
JavaScript
1
2
1
[list(map(int, li[:-1])) for li in lol]
2
- I knew how to convert the last string to
daytime
.
JavaScript
1
2
1
list(map(dateConverter, [li[-1] for li in lol]))
2
- I did not know how to do that 2-in-1 using
map
function or any other way. I tried the following but did not work for me.
JavaScript
1
2
1
[list(map(int, dateConverter, li[:-1], li[-1])) for li in lol]
2
Advertisement
Answer
What about this?
JavaScript
1
10
10
1
lol = [['365', '336', '365', 'E<;EjD'],
2
['365', '336', '365', 'E<;EkD'],
3
['365', '336', '365', 'E<;F0D'],
4
['0', '0', '335', 'E<;GaQ'],
5
['0', '0', '335', 'E<;GbQ']]
6
7
converted = [
8
[int(x) if i < 3 else date_converter(x) for (i, x) in enumerate(li)] for li in lol
9
]
10