I am trying to collect information from an ancient email system. I need to join an email id with the receiver information. Each email will have one or more receivers and be either "to", "cc", or "bcc". The data is in the following format. There are thousands of records
em_id = ['e1', 'e2']
rec = [[['To', 'Bob'], ['To', 'Billy'], ['Cc', 'Jane']], [['Bcc', 'Bob']]]]
I need the result to be like ['id', 'to', 'name'] such as:
em_rec = [['e1', 'To', 'Bob'], ['e1', 'To', 'Billy'], ['e1', 'Cc', 'Jane'], ['e2', 'Bcc', 'Bob']]
I can work out how to insert one, but not how to loop it, particularly because the loop length varies for every record.
I asked a similar question that used zip at Combine a list with a list of varied length within a list and the answer was awesome. I suspect the answer will be similar but this one needs 3 values per list item and has no empty cases. I am very new to python and can't figure out how it works or how it could be tweaked to fit this.
Thanks in advance.
CodePudding user response:
em_rec = [['e1' if condition(r) else 'e2'] r for r in rec]
CodePudding user response:
Use a list comprehension with zip
:
>>> [[[x] i for i in y] for x, y in zip(em_id, rec)]
[[['e1', 'To', 'Bob'], ['e1', 'To', 'Billy'], ['e1', 'Cc', 'Jane']], [['e2', 'Bcc', 'Bob']]]
>>>
Or unpacking:
[[[x, *i] for i in y] for x, y in zip(em_id, rec)]
Edit:
If you want to repeat em_id
:
from itertools import cycle
[[[x] i for i in y] for x, y in zip(cycle(em_id), rec)]