Jun-14-2018, 10:04 AM
(Jun-14-2018, 08:55 AM)3Pinter Wrote: What am I doing wrong?you misunderstood scidam's approach with regex
in slow motion:
import re preset=r"cookie\." list1=['cookie.A001', 'cookie.H004', 'cookie.H004 andsomeothertext', 'cookie.ABC031', 'cookie.FAIL002'] list2=['A', 'H', 'ABC'] list2_pats = map(lambda x: re.compile(preset + r'(%s)([0-9]+)' % x), list2) result1 = [pat.findall(item) for pat in list2_pats for item in list1] print('result1: {}\n'.format(result1)) result2 = sum(result1, []) print('result2: {}\n'.format(result2)) result3 = zip(*result2) print('result3: {}\n'.format(result3)) output1, output2 = result3 print('output1: {}\n'.format(output1)) print('output2: {}\n'.format(output2))
Output:result1: [[('A', '001')], [], [], [], [], [], [('H', '004')], [('H', '004')], []
, [], [], [], [], [('ABC', '031')], []]
result2: [('A', '001'), ('H', '004'), ('H', '004'), ('ABC', '031')]
result3: [('A', 'H', 'H', 'ABC'), ('001', '004', '004', '031')]
output1: ('A', 'H', 'H', 'ABC')
output2: ('001', '004', '004', '031')
your line 10: if item in pat
never will be True
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