0

What is the best way to use regex in a for loop while testing for data type?

For context, I'm looping over large unclean data sets with multiple data types and need to find extensions of strings, if they exist. Small changes to my code, like converting values to string costs me minutes.

I read through this question Python: How to use RegEx in an if statement? but couldn't find a way of testing for a match without first converting to a string.

Values:

vals = [444444, '555555-Z01']
pattern = re.compile('[-]*[A-Z]{1}[0-9]{2}$')
# new_vals = [444444, 555555]

Slow method: (2.4 µs ± 93.6 ns per loop)

new_vals = []
for v in vals:
    if type(v)==str:
        if pattern.search(v) is not None:
            new_v = pattern.findall(v)[0].replace('-','')
            new_vals.append(new_v)
    else:
        new_vals.append(v)

Fast method: (1.84 µs ± 34.7 ns per loop)

f = lambda x: x if type(x)!=str else pattern.findall(x)[0].replace('-','')

new_vals = []
for v in vals:
    new_vals.append(f(v))

Unsucessful Method:

new_vals = []
for v in vals:
    if ((type(v)==str) & (pattern.search(v) is not None)):
        new_vals.append(v)

Error:

TypeError: expected string or bytes-like object
8
  • 2
    argh: if ((type(v)==str) and (pattern.search(v) is not None)):. & doesn't short circuit Commented May 28, 2019 at 19:10
  • @Jean-FrançoisFabre make that an answer. Commented May 28, 2019 at 19:11
  • well, I'd vote for a typo instead. And OP isn't clear about the result. What is the point of the third attempt when there's already a "fast" attempt, and should we keep the non-string values, or filter them out? Commented May 28, 2019 at 19:12
  • 1
    Don't use type(v) in comparisons. Use isinstance(v, str) instead. Commented May 28, 2019 at 19:15
  • @chepner: testing type is faster when you're sure that the object is of the exact type Commented May 28, 2019 at 19:18

1 Answer 1

1

I tried to beat your attempts using try/except blocks but the exception handling seems to take too much time. So much for "better ask forgiveness than permission" ...

Your last attempt is the most promising, if you just change & by and, because & is the logical and and doesn't short circuit.

I'll go for this, in a list comprehension to speed it up slightly more, and drop the is not None test which is useless since if search succeeds, it returns a regex object, which is truthy:

new_vals = [v for v in vals if type(v)==str and pattern.search(v)]

or with isinstance (same speed, tests subclasses of str too):

new_vals = [v for v in vals if isinstance(v,str) and pattern.search(v)]
Sign up to request clarification or add additional context in comments.

2 Comments

AHA! if type(v)==str and pattern.search(v): solves it!
good. Try the list comprehension approach I'm mentionning. First you'll learn about them (it's worth) and second I think you'll save a few milliseconds more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.