Function Definition: Matching Area Codes to Phone Numbers

Question:

If I want to define a function called match_numbers, which would match the area code from one list to the phone number of another list, how should I fix my code? For example:

match_phone(['666', '332'], ['(443)241-1254', '(666)313-2534', '(332)123-3332'])

would give me

(666)313-2534
(332)123-3332

My code is:

def phone (nlist, nlist1):
    results = {}
for x in nlist1:
    results.setdefault(x[0:3], [])
    results[x[0:3]].append(x)
for x in nlist:
    if x in results:
        print(results[x])

The problem with this code is, however:

  1. It gives me the outputs in brackets, whereas I want it to print
    the output line by line like shown above, and
  2. it won’t work with the parantheses in the 2nd list (for example
    (666)543-2322 must be converted as 666-543-2322 for the list to
    work.
Asked By: Deer530

||

Answers:

You could do something like this:

phone_numbers = ['(443)241-1254', '(666)313-2534', '(332)123-3332']
area_codes = ['666', '332']
numbers = filter(lambda number: number[1:4] in area_codes, phone_numbers)
for number in numbers:
    print(number)

Another similar way to do this without using a filter could be something like this:

for number in phone_numbers:
    if number[1:4] in area_codes:
        print(number)

Output in either case would be:

(666)313-2534
(332)123-3332
Answered By: Roy Hvaara

If all the phone numbers will be in the format (ddd)ddd-dddd you can use

for number in (num for num in nlist1 if num[1:4] in nlist): 
   print(number)

You could use some better variable names than nlist and nlist1, in my view.

Answered By: saulspatz

Now, there are better/faster approaches to do what you are trying to do, but let us focus on fixing your code.

The first issue you have is how you are slicing your string. Remember that you start at index 0. So if you do:

x[0:3]

What you are actually getting is something like this from your string:

(12

Instead of your intended:

123

So, knowing that indexes start at 0, what you actually want to do is slice your string as such:

x[1:4]

Finally, your line here:

results[x[0:3]].append(x)

There are two problems here.

First, as mentioned above, you are still trying to slice the wrong parts of your string, so fix that.

Second, since you are trying to make a key value pair, what that above line is actually doing is making a key value pair where the value is a list. I don’t think you want to do that. You want to do something like:

{'123': '(123)5556666'}

So, you don’t want to use the append in this case. What you want to do is assign the string directly as the value for that key. You can do that as such:

results[x[1:4]] = x

Finally, another problem that was noticed, is in what you are doing here:

results.setdefault(x[1:4], [])

Based on the above explanation on how you want to store a string as your value in your dictionary instead of a list, so you don’t need to be doing this. Therefore, you should simply be removing that line, it does not serve any purpose for what you are trying to do. You have already initialized your dictionary as results = {}

When you put it all together, your code will look like this:

def match_phone(nlist, nlist1):
    results = {}
    for x in nlist1:
        results[x[1:4]] = x
    for x in nlist:
        if x in results:
            print(results[x])

match_phone(['666', '332'], ['(443)241-1254', '(666)313-2534', '(332)123-3332'])

And will provide the following output:

(666)313-2534
(332)123-3332
Answered By: idjaw
def match_phone(area_codes, numbers):
    area_codes = set(area_codes)
    for num in numbers:
        if num in area_codes:
            print num
Answered By: dursk

No one with regex solution! This may be an option too[in python 3+].

import re
def my_formatter(l1,l2):
    mydic = {re.match(r'([(])([0-9]+)([)])([0-9]+[-][0-9]+)',i).group(2):re.match(r'([(])([0-9]+)([)])([0-9]+[-][0-9]+)',i).group(4) for i in l2}
    for i in l1:
        print ("({0}){1}".format(str(i),str(mydic.get(i))))

my_formatter(['666', '332'], ['(443)241-1254', '(666)313-2534', '(332)123-3332'])

It prints-

(666)313-2534
(332)123-3332
Answered By: SIslam