In Python,if startswith values in tuple, I also need to return which value -


i have area codes file put in tuple

for line1 in area_codes_file.readlines():     if area_code_extract.search(line1):         area_codes.append(area_code_extract.search(line1).group()) area_codes = tuple(area_codes) 

and file read python full of phone numbers. if phone number starts 1 of area codes in tuple, need things: 1 keep number 2 know area code did match, need put area codes in brackets.

so far, able 1:

for line in txt.readlines(): is_number = phonenumbers.parse(line,"gb") if phonenumbers.is_valid_number(is_number):     if line.startswith(area_codes):         print (line) 

how do second part?

the simple (if not highest performance) approach check each prefix individually, , keep first match:

for line in txt:     is_number = phonenumbers.parse(line,"gb")     if phonenumbers.is_valid_number(is_number):         if line.startswith(area_codes):             print(line, next(filter(line.startswith, area_codes))) 

since know filter(line.startswith, area_codes) 1 hit, pull hit using next.

note: on python 2, should start file from future_builtins import filter generator based filter (which save work stopping search when hit). python 3's filter behaves this.

for potentially higher performance, way both test prefixes @ once , figure out value hit use regular expressions:

import re  # function match of given prefixes returning match obj on hit area_code_matcher = re.compile(r'|'.join(map(re.escape, area_codes))).match line in txt:     is_number = phonenumbers.parse(line,"gb")     if phonenumbers.is_valid_number(is_number):         # returns none on miss, match object on hit         m = area_code_matcher(line)         if m not none:             # whatever matched in 0th grouping             print(line, m.group()) 

lastly, 1 final approach can use if area codes of fixed length. rather using startswith, can slice directly; know hit because sliced off yourself:

# if there lot of area codes, using set/frozenset allow faster lookup area_codes_set = frozenset(area_codes) line in txt:     is_number = phonenumbers.parse(line,"gb")     if phonenumbers.is_valid_number(is_number):         # assuming lines match start ###         if line[:3] in area_codes_set:             print(line, line[:3]) 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -