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
#1
Guys,

Fairly new to python so for sure could use your expertise!

I have two lists, I want to check if the first list has values which match (partially) with list2.
And in the end I want
a list with the matching character(s) after "cookie."
a list with the value
neglect the " randomothertext" (yes, the space in front of random is always there)

output1, output2=[],[]
preset="cookie."
list1=['cookie.A001', 'cookie.H004', 'cookie.H004 andsomeothertext', 'cookie.ABC031', 'cookie.FAIL002']
list2=['A', 'H', 'ABC']
list2_total=[preset+x for x in list2]

(let's say i = cookie.H004 to illustrate expected values)
for i in list1:
	if any(n in i for n in list2_total):
#kt should and values: 3
		kt = len(preset)
#km should value: 3 + len(H) = 3+1 = 4
		km = kt + len(n)
		output1.append(i[kt])
		output2.append(i[km:km+2])
# output1 after the full loop would be ["A", "H", "H", "ABC"]
# output2 ["001", "004", "004", "031"]
# so the SAME output would be if "i" is 'cookie.H004 andsomeothertext' and 'cookie.H004'
Question
1. n is unknown in using it as parameter. How do I get that to act as a reusable parameter?
2. is this the way to go? Or would you suggest another (better) way?
Reply


Messages In This Thread
for any loop _ want to know which matches - by 3Pinter - Jun-13-2018, 05:36 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  delete a Python object that matches an atribute portafreak 2 2,214 Feb-19-2020, 12:48 PM
Last Post: portafreak
  Detecting if image matches another kainev 2 2,933 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,943 Jul-19-2018, 06:27 PM
Last Post: woooee
  checking for matches between lists henrik0706 2 2,711 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,393 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