1

I am new to Python and trying to learn python from Perl. In Perl, if I want to compare a string against multiple sub-strings, I would use the following:

sub matchCity {
    my $cityName = shift;
    print "$cityName is a valid city name\n" if ($cityName =~ /kyo|par|omba/);
}

matchCity('tokyo'); # tokyo is a valid city name
matchCity('paris'); # paris is a valid city name
matchCity('bombay'); # bombay is a valid city name
matchCity('chicago'); # Doesn't print anything

How can I do this in python?

1
  • 2
    WHAT'S WRONG WITH CHICAGO!?! Commented Oct 29, 2013 at 0:53

4 Answers 4

3

You don't actually need regex for this:

>>> def matchCity(s):
...     if any(r in s for r in ('kyo','par','omba')):
...         print s, 'is a valid city name'
... 
>>> matchCity('tokyo')
tokyo is a valid city name
>>> matchCity('paris')
paris is a valid city name
>>> matchCity('bombay')
bombay is a valid city name
>>> matchCity('chicago')  # doesn't print anything
>>> 
Sign up to request clarification or add additional context in comments.

3 Comments

Don't all of them return None?
if i replace the print with a simple statement (i used x=1) to get valid timings, this appears to be about twice fast as the re method (1.15 uS vs. 2.01uS on my machine).
@CorleyBrigman I'm not at all surprised. The re approach does a lot of work behind the scenes (parsing the regular expression etc.).
2
import re

def matchCity(city_name):
    if re.search('kyo|par|omba', city_name):
        print "{} is a valid city name".format(city_name)

matchCity('tokyo') # tokyo is a valid city name
matchCity('paris') # paris is a valid city name
matchCity('bombay') # bombay is a valid city name
matchCity('chicago') # Doesn't print anything

6 Comments

Don't all of them return None?
The function returns None. Is that what you mean?
Yes. In the comments you added, you annotate that when 'chicago' is passed, it returns undef. I was saying that a) shouldn't that be None, and b) since all of them return None, the return value can't be checked to see if the city matches or not (since I assume that's why it was mentioned that it returns undef).
Seth is right here: With Perl, a subroutine returns the value of the last calculation unless there is an explicit return value. Python returns None unless there is an explicit return value.
Ah, ok, I copied those lines verbatim from the question
|
0

With regular expressions:

import re
city = re.compile('kyo|par|omba', re.I)
matchCity = lambda x: city.search(x)

And you can use matchCity("example_city") as condition:

if matchCity("tokyo"):   print "Valid city!" # Valid city!
if matchCity("paris"):   print "Valid city!" # Valid city!
if matchCity("bombay"):  print "Valid city!" # Valid city!
if matchCity("chicago"): print "Valid city!" #

Comments

0

Python has regular expressions. So you could do this:

import re

city_names = re.compile(r'kyo|par|omba')

def match_city(city_name):
    if city_names.search(city_name):
        print city_name, "is a valid city name"

Normally I would tell you to avoid regular expressions if you can. But matching one of multiple sub-strings is actually a case where they likely perform better than a loop. This is especially true as the number of sub-strings grows.

2 Comments

According to the comments on @arshajii's post, re is slower than string methods even in this case.
but he might be right... the test i did includes the compile of the regex, and if you have more substrings to check, the timing advantage might shift. will try it later.

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.