something like
def lookup(lookup_item, reference, prefix): for ref_item in reference: full_ref = ''.join((prefix, ref_item)) if lookup_item.startswith(full_ref): s = len(full_ref) - len(lookup_item) return (ref_item, item[s:].split(' ')[0]) PREFIX = "cookie." REFERENCE = ['A', 'H', 'ABC'] lookup_list = ['cookie.A001', 'cookie.H004', 'cookie.H004 andsomeothertext', 'cookie.ABC031', 'cookie.FAIL002'] result = [lookup(item, REFERENCE, PREFIX) for item in lookup_list] output1, output2 = zip(*[item for item in result if item]) print(output1) print(output2)
Output:('A', 'H', 'H', 'A')
('001', '004', '004', 'BC031')
Hope this could put you on the right path. As you can see there is problem with A/ABC match - you need to explain further how you process such cases.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs