Python Forum
for any loop _ want to know which matches
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
for any loop _ want to know which matches
#8
How do I catch the 'not matched ones'?

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)
#result = sum([pat.findall(item) for pat in list2_pats for item in list1], [])
result =[]
for item in list1:
	for pat in list2_pats:
		if item in pat:
			result.append(pat.findall(item))
		else:
			result.append("eeew")
			
output1, output2, output3 = zip(*result)
Ideally I get another output (output3) which contains all the non-matches-inputs. Like "cookie.FAIL002".

Untangling the shorthand version is a bit of a struggle atm (still learning :) )

What am I doing wrong?
Reply


Messages In This Thread
RE: for any loop _ want to know which matches - by 3Pinter - Jun-14-2018, 08:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  delete a Python object that matches an atribute portafreak 2 2,227 Feb-19-2020, 12:48 PM
Last Post: portafreak
  Detecting if image matches another kainev 2 2,941 Dec-02-2018, 02:00 PM
Last Post: kainev
  How to detect and tell user that no matches were found in a list RedSkeleton007 6 3,977 Jul-19-2018, 06:27 PM
Last Post: woooee
  checking for matches between lists henrik0706 2 2,720 Oct-24-2017, 11:08 AM
Last Post: henrik0706
  Check to see if a string exactly matches an element in a list DBS 1 22,433 Nov-02-2016, 10:16 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020