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
#2
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

Reply


Messages In This Thread
RE: for any loop _ want to know which matches - by buran - Jun-13-2018, 06:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  delete a Python object that matches an atribute portafreak 2 2,232 Feb-19-2020, 12:48 PM
Last Post: portafreak
  Detecting if image matches another kainev 2 2,945 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,985 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,445 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