I am making a Python script that finds the word “Hold” in a list string and confirms if it is holdable or not.
File = [ "Hold_Small_Far_BG1_123456789.jpg", "Firm_Large_Near_BG1_123456789.jpg", "Move_Large_Far_BG1_123456789.jpg", "Firm_Large_Far_BG1_123456789.jpg", "Hold_Small_Hold_BG1_123456789.jpg", "Hold_Small_Near_BG1_123456789.jpg", "Small_Small_Far_BG1_123456789.jpg", ] for item in File: if "Hold" in item: return print('Yes, object is holdable.') else: return print('No, object is not holdable.')
The code above sees the first ‘Hold’ word and returns true. The holdable objects are the ones that have ‘Hold’ as the third word.
The problem is the code sees the first ‘Hold’ word and returns true. I want the code to check if there’s a word ‘Hold’ in the filename while ignoring the first ‘Hold’ word.
Please note that I cannot split the string using the ‘_’ because it is generated by people. So, sometimes it can be a comma, dot, or space even.
Is there an expression for this? Sorry for the bad English.
Thank you. :)
Advertisement
Answer
You can use a regex pattern:
import re holdables = ['yes' if re.findall(r'w.*(Hold)', x) else 'no' for x in File] for x in holdables: print(x)
The regex here only assumes that ‘Hold’ is not the first word in the string but does exist elsewhere, since you said you can’t be sure whether underscores or other delimiters will be present. If you need more stringent conditions for the regex pattern, you can always update it.