Python Forum
Search for string values in - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Search for string values in (/thread-9094.html)



Search for string values in - begood321 - Mar-20-2018

	
arr2 = [x for x in arr if re.search('MINS', str(x), flags=0)]
for item in arr2:
		print(item)



RE: Search for string values in - buran - Mar-20-2018

what is your goal? what is your question? elaborate what problem you have.


RE: Search for string values in - begood321 - Mar-20-2018

I'm trying to find multiple values via if conditions as seen below in my code. If either is found in original string array (arr), print out entire string with value. Any help is greatly appreciated, thanks.

	
arr2 = [x for x in arr 
if re.search('MINS', str(x), flags=0) 
eif re.search('NM', str(x), flags=0) 
eif re.search('min', str(x), flags=0) 
else print("No values found" ]
   for item in arr2:
	print(item)



Search for multiple string values in array - begood321 - Mar-20-2018

Hi Guys, I didn't explain well previously. I'm trying to find specific string if available in a string array via if conditions as seen below in my code.

The current code doesn't work, but will only work for the 1st if statement only, without other elif and else statements. This is what I'd like to achieve, if any of the strings found in if conditions found in original string array (arr), print out entire string with string value searched for.

Any help is greatly appreciated, thanks Wall .

arr2 = [x for x in arr 
if re.search('MINS', str(x), flags=0) 
eif re.search('NM', str(x), flags=0) 
eif re.search('min', str(x), flags=0) 
else print("No values found" ]
   for item in arr2:
    print(item)



RE: Search for multiple string values in array - tannishpage - Mar-21-2018

Why is there a print statement inside a list?
arr2 = [x for x in arr if (re.search('MINS', str(x), flags=0)) or (re.search('NM', str(x), flags=0)) or re.search('min', str(x), flags=0) else x = "No values found" ]
for item in arr2:
    print(item)
Try that. I think thats what you are asking for.


RE: Search for string values in - micseydel - Mar-21-2018

(I've merged the threads. Please don't start a new thread for the same issue.)


RE: Search for string values in - begood321 - Mar-21-2018

Thanks Tannishpage, your solution works, much appreciated


RE: Search for string values in - tannishpage - Mar-21-2018

You are welcome. Smile