Reputation: 7287
If I have a python list with the shape like this:
T = [
['07,07,2012 22:10', ['people','drama','melody','bun']],
['08,07,2012 21:04', ['queen','group']],
['08,07,2012 21:23', ['printing','market','shopping']],
['08,07,2012 21:04', ['people','bun']],
['08,11,2012 11:14', ['kangaroo']]
]
What I need is to convert this list into this format:
T =[
['07,07,2012 22:10', 'people'],
['07,07,2012 22:10', 'drama'],
['07,07,2012 22:10', 'melody'],
['07,07,2012 22:10', 'bun'],
['08,07,2012 21:04', 'queen'],
['08,07,2012 21:04', 'group'],
['08,07,2012 21:23', 'printing'],
['08,07,2012 21:23', 'market'],
['08,07,2012 21:23', 'shopping'],
['08,07,2012 21:04', 'people'],
['08,07,2012 21:04', 'bun'],
[''08,11,2012 11:14'', 'kangaroo']
]
i.e. For each element having length of its first sub-element greater than 1 (in the original list T), split the first sub-element (a[1] for a in T if len(a[1] > 1)
) and append it as another list with the same time stamp. Maybe my words lack the explanation, but the samples above are definitely explaining what I need to do. Any help would be appreciated.
Upvotes: 0
Views: 109
Reputation: 3386
Use a list comprehension:
[(timestamp, item) for timestamp, items in T for item in items]
This will give you a list of tuples, which is probably suitable here. But you can modify it to get a list of lists:
[[timestamp, item] for timestamp, items in T for item in items]
Upvotes: 5
Reputation: 2512
Try this on T:
def process(t):
new = []
for i in t:
for j in i[1]:
new.append([i[0], j])
return new
Upvotes: 1