Reputation: 83
I have my list:
l = ['a;b;c;2:38', 'd;e;f;3:16', 'g;h;i;3:20']
I want to separate it in:
l = [[['a'], ['b'], ['c'], ['2:38']], [['d'], ['e'], ['f'], ['3:16']], [['g'], ['h'], ['i'], ['3:20']]]
This is because I have to select the first element of one of the lists (a, d, or g) by the condition if the last element (the number) is greater than an int input. So I thought it would be easier to transform the list but I don't know how to do it. In this example, I receive the number 190 and I want to make a function that returns the first element of the list that has the number that is greater than this input (in this case 190), this happens with the 3:16 (3 minutes and 16 seconds are 196 seconds) and the 3:20 (3 minutes and 20 seconds are 200 seconds), so my function would return:
['d','g']
I tried doing something like:
for i in range(len(l)):
l[i] = l[i].split(",")
And then doing it with the ';' but it doesn't do what I want. I'm also complicated with the part of evaluating if the minutes in the list are greater than "n" seconds.
Upvotes: 1
Views: 75
Reputation: 6483
To create the list of list you want:
l = ['a; b; c; 2:38', 'd; e; f; 3:16', 'g; h; i; 3:20']
newl=[[[[ele]for ele in i.split('; ')] for i in l]]
print(newl)
You could try this, instead of creating the list of lists to get the desired output:
l = ['a; b; c; 2:38', 'd; e; f; 3:16', 'g; h; i; 3:20']
def get_sec(time_str):
return sum(int(x) * 60 ** i for i,x in enumerate(reversed(time_str.split(":"))))
def func(ls, inp):
allow=filter(lambda x: get_sec('0:'+x.split('; ')[-1])>inp , ls)
return [x[0] for x in allow]
inp=190 #input
ge=func(l,inp)
print(ge)
Output:
['d', 'g']
Upvotes: 1
Reputation: 27577
Here is a way you can use a list comprehension:
l = ['a;b;c;2:38', 'd;e;f;3:16', 'g;h;i;3:20']
l = [[[a] for a in s.split(';')] for s in l]
print(l)
Output:
[[['a'], ['b'], ['c'], ['2:38']],
[['d'], ['e'], ['f'], ['3:16']],
[['g'], ['h'], ['i'], ['3:20']]]
Upvotes: 1
Reputation: 36674
This will do what you need.
l = ['a;b;c;2:38', 'd;e;f;3:16', 'g;h;i;3:20']
list(map(lambda x: [[i] for i in x], map(lambda x: x.split(';'), l)))
[[['a'], ['b'], ['c'], ['2:38']],
[['d'], ['e'], ['f'], ['3:16']],
[['g'], ['h'], ['i'], ['3:20']]]
Upvotes: 1