1

I want to test if certain characters are in a line of text. The condition is simple but characters to be tested are many.

Currently I am using \ for easy viewing, but it feels clumsy. What's the way to make the lines look nicer?

text = "Tel+971-2526-821     Fax:+971-2526-821"

if "971" in text or \
   "(84)" in text or \
   "+66" in text or \
   "(452)" in text or \
   "19 " in text:
    print "foreign"
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Mark K
  • 8,767
  • 14
  • 58
  • 118
  • 1
    @GarbageCollector: Minor note: A `tuple` of `str` literals would be more efficient; the byte code compiler can store the whole `tuple` as a constant and reuse it, while a `list` would need to be rebuilt each time. – ShadowRanger Jan 04 '18 at 03:11
  • @GarbageCollector, Patrick Haugh, ShadowRanger, marvelous! could any you please post an answer so that I can close this question? – Mark K Jan 04 '18 at 03:14
  • @ShadowRanger agreed that would surely be an advantage to use `tuple` instead of `list` – Sohaib Farooqi Jan 04 '18 at 03:18

5 Answers5

3

Why don't extract the phone numbers from the string and do your tests

text = "Tel:+971-2526-821     Fax:+971-2526-821"

tel, fax = text.split()
tel_prefix, *_ = tel.split(':')[-1].split('-')
fax_prefix, *_ = fax.split(':')[-1].split('-')

if tel_prefix in ("971", "(84)"):
    print("Foreigner")

for python 2.x

tel_prefix = tel.split(':')[-1].split('-')[0]
fax_prefix = fax.split(':')[-1].split('-')[0]
taoufik A
  • 1,439
  • 11
  • 20
2

Enlightened by @Patrick Haugh in the comment. We can do:

text = "Tel+971-2526-821     Fax:+971-2526-821"

if any(x in text for x in ("971", "(84)", "+66", "(452)", "19 ")):
    print "foreign"
Mark K
  • 8,767
  • 14
  • 58
  • 118
1

You can use any builtin function to check if any one of the token exists in the text. If you would like to check if all the token exists in the string you can replace the below any with all function. Cheers!

text = 'Hello your number is 19 '
tokens = ('971', '(84)', '+66', '(452)', '19 ')

if any(token in text for token in tokens):
    print('Foriegn')

Output: Foriegn

Swadhikar
  • 2,152
  • 1
  • 19
  • 32
1

Existing comments mention that you can't really have multiple or statements like you intend, but using generators/comprehensions and the any() function you are able to come up with a serviceable option, such as the snippet if any(x in text for x in ('971', '(84)', '+66', '(452)', '19 ')): that @Patrick Haugh recommended.

I would recommend using regular expressions instead as a more versatile and efficient way of solving the problem. You could either generate the pattern dynamically, or for the purpose of this problem, the following snippet would work (don't forget to escape parentheses):

import re

text = 'Tel:+971-2526-821     Fax:+971-2526-821'

pattern = u'(971|\(84\)|66|\(452\)|19)'
prog = re.compile(pattern)

if prog.search(text):
    print 'foreign'

If you are searching many lines of text or large bodies of text for multiple possible substrings, this approach will be faster and more reusable. You only have to compile prog once, and then you can use it as often as you'd like.

As far as dynamic generation of a pattern is concerned, a naive implementation might do something like this:

match_list = ['971', '(84)', '66', '(452)', '19']
pattern = '|'.join(map(lambda s: s.replace('(', '\(').replace(')', '\)'), match_list)).join(['(', ')'])

The variable match_list could then be updated and modified as needed. There is a slight inefficiency in running two passes of replace(), and @Andrew Clark has a good trick for fixing that here, but I don't want this answer to be too long and cumbersome.

Hans Musgrave
  • 6,613
  • 1
  • 18
  • 37
  • thank you for you help. it's a very good collection and reference for learning. a part of it is too advanced for beginners. would you mind I choose the most voted for the answer? – Mark K Jan 04 '18 at 09:20
  • 1
    I wouldn't mind in the slightest. I know these things frequently turn up in Google searches, so I try to fill in a few details where I can. – Hans Musgrave Jan 04 '18 at 09:35
1

You can construct a lambda function that checks if a value is in the text, and then map this function to all of the values:

text = "Tel:+971-2526-821     Fax:+971-2526-821"
print any(map((lambda x: x in text), ["971", "(84)", "+66", "(452)", "19 "]))

The result is True, which means at least one of the values is in text.

FatihAkici
  • 4,679
  • 2
  • 31
  • 48
  • @Faith Akici, thank you for your answer. it's a very good one and probably a little too advanced for beginners. hope you don't mind I choose the most voted for the answer. – Mark K Jan 04 '18 at 09:15
  • 1
    Absolutely not, @MarkK. It is so nice of you to say that! My answer is in essence a compact version of Patrick Haugh's. Thanks! – FatihAkici Jan 04 '18 at 16:56