FAQ
I've been beating my head against the desk trying to figure out a method
to accomplish this:

Take a list (this example is 5 items, It could be 150 or more - i.e.
it's variable length depending on the city/local calling zones)
The first 6 digits of phone numbers(NPA/NXX) in a local calling area. I
want to concatenate the last digit for insertion into a call routing
pattern.

I tried this and failed miserably:

list1=['252205','252246','252206','252247','252248']
for item in list1:
try:
item1=list1[0]
item2=list1[1]
if item1[0:5] == item2[0:5]:
print item1[0:5] + '[' + item1[5:6] + item2[5:6]
+ ']'
list1.pop(0)
else:
print item1
list1.pop(0)
except:
try:
print item1
list1.pop(0)
except:
pass

#-----------------------------------------------------------------
My intent is to have the end data come out (from the example list above)
in the format of
25220[56]
25224[678]

I tried putting together a variable inserted into a regular expression,
and it doesn't seem to like:
Item1=list1[0]
Itemreg = re.compile(Item1[0:5])
For stuff in itemreg.list1:
#do something

Can somebody throw me a bone, code example or module to read on
python.org? I'm a n00b, so I'm still trying to understand functions and
classes.

I thought the experts on this list might take pity on my pathetic code
skillz!

Thanks so much :)

Ed Ellerbee

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110713/a69f7c0f/attachment-0001.html>

Search Discussions

  • Emile van Sebille at Jul 13, 2011 at 9:51 pm
    On 7/13/2011 2:13 PM Ellerbee, Edward said...
    I've been beating my head against the desk trying to figure out a method
    to accomplish this: <snip>
    #-----------------------------------------------------------------
    My intent is to have the end data come out (from the example list above)
    in the format of
    25220[56]
    25224[678]
    this should get you started...
    list1=['252205','252246','252206','252247','252248']
    D = {}
    for ii in list1: D.setdefault(ii[:5],[]).append(ii[5])
    ...
    print D
    {'25224': ['6', '7', '8'], '25220': ['5', '6']}
    >>>

    Emile
  • MRAB at Jul 13, 2011 at 9:58 pm

    I've been beating my head against the desk trying to figure out a
    method to accomplish this: >
    Take a list (this example is 5 items, It could be 150 or more - i.e.
    it's variable length depending on the city/local calling zones) >
    The first 6 digits of phone numbers(NPA/NXX) in a local calling area.
    I want to concatenate the last digit for insertion into a call
    routing pattern. >
    I tried this and failed miserably: >
    list1=['252205','252246','252206','252247','252248']
    for item in list1:
    try:
    item1=list1[0]
    item2=list1[1]
    if item1[0:5] == item2[0:5]:
    print item1[0:5] + '[' + item1[5:6] +
    item2[5:6] + ']'
    list1.pop(0)
    else:
    print item1
    list1.pop(0)
    except:
    try:
    print item1
    list1.pop(0)
    except:
    pass >
    #-----------------------------------------------------------------
    My intent is to have the end data come out (from the example list
    above) in the format of
    25220[56]
    25224[678] >
    I tried putting together a variable inserted into a regular
    expression, and it doesn't seem to like:
    Item1=list1[0]
    Itemreg = re.compile(Item1[0:5])
    For stuff in itemreg.list1:
    #do something >
    Can somebody throw me a bone, code example or module to read on
    python.org? I'm a n00b, so I'm still trying to understand functions
    and classes. >
    I thought the experts on this list might take pity on my pathetic
    code skillz!
    >
    defaultdict comes in handy:
    list1 = ['252205','252246','252206','252247','252248']
    from collections import defaultdict
    d = defaultdict(set)
    for item in list1:
    d[item[ : 5]].add(item[5 : ])

    d
    defaultdict(<class 'set'>, {'25224': {'8', '7', '6'}, '25220': {'5', '6'}})
    for k, v in d.items():
    print(k + "[" + "".join(v) + "]")


    25224[876]
    25220[56]
  • Ellerbee, Edward at Jul 14, 2011 at 1:05 pm
    Thank you all for the advice, let me spin this in a different way.

    I've built a program that goes to the NANPA website, scrapes area
    code/exchange (npa/nxx) digits for a specified area - be it carolina,
    alabama, texas, etc - drops it into a file, then massages the data and
    prints out the correct format to insert into a voice router. The code is
    ugly, but it works great.

    The thing I haven't been able to get my script to do is to reduce the
    amount of dial-peers. Hence the need to reduce the numbers to the least
    common denominator, and put it in the xxxxx[xx] format.If we had a set
    number of digits, we could build a dictionary(unless it's possible to do
    that dynamically).

    So, a couple assertions:
    1. the data will always be 6 digit numbers (in a string format)
    2. the data is in a txt file after being put there from my script
    3. the data will never be the same (I'm going to use this for site
    conversions/new site builds
    e.g. today I might be dealing with 252-, 919- and 704- area
    codes, tomorrow might be 304- and 754-
    4. I wanted a script to reduce the time taking to build the dial-peers
    manually. I'd previously spent 3-4 hours
    on gathering and processing data. The script I have so far pulls data
    and massages in about 6 seconds
    5. I'm using python 2.7 - it seems like it had more module availability
    than 3

    And a couple question:
    1. Would lists be the best way to handle this data? Would it be better
    to process this data from a file?
    2. Is there a way to determine the common (first-5 digits) denominator
    among a list (or file) of data?
    3. and... Could those common numbers be inserted in a dict for
    processing?

    Sorry for the book!

    thanks

    Edward Ellerbee


    -----Original Message-----
    From: python-list-bounces+eellerbee=bbandt.com at python.org
    [mailto:python-list-bounces+eellerbee=bbandt.com at python.org] On Behalf
    Of MRAB
    Sent: Wednesday, July 13, 2011 5:59 PM
    To: python-list at python.org
    Subject: Re: I don't know list, I not good at list.

    I've been beating my head against the desk trying to figure out a >
    method to accomplish this:
    >
    Take a list (this example is 5 items, It could be 150 or more - i.e.
    it's variable length depending on the city/local calling zones) > >
    The first 6 digits of phone numbers(NPA/NXX) in a local calling area.
    I want to concatenate the last digit for insertion into a call >
    routing pattern.
    >
    I tried this and failed miserably: >
    list1=['252205','252246','252206','252247','252248']
    for item in list1:
    try:
    item1=list1[0]
    item2=list1[1]
    if item1[0:5] == item2[0:5]:
    print item1[0:5] + '[' + item1[5:6] +
    item2[5:6] + ']'
    list1.pop(0)
    else:
    print item1
    list1.pop(0)
    except:
    try:
    print item1
    list1.pop(0)
    except:
    pass >
    #-----------------------------------------------------------------
    My intent is to have the end data come out (from the example list >
    above) in the format of > 25220[56] > 25224[678] > > I tried putting
    together a variable inserted into a regular > expression, and it
    doesn't seem to like:
    Item1=list1[0]
    Itemreg = re.compile(Item1[0:5])
    For stuff in itemreg.list1:
    #do something >
    Can somebody throw me a bone, code example or module to read on >
    python.org? I'm a n00b, so I'm still trying to understand functions >
    and classes.
    >
    I thought the experts on this list might take pity on my pathetic >
    code skillz!
    >
    defaultdict comes in handy:
    list1 = ['252205','252246','252206','252247','252248']
    from collections import defaultdict >>> d = defaultdict(set) >>>
    for item in list1:
    d[item[ : 5]].add(item[5 : ])

    d
    defaultdict(<class 'set'>, {'25224': {'8', '7', '6'}, '25220': {'5',
    '6'}}) >>> for k, v in d.items():
    print(k + "[" + "".join(v) + "]")


    25224[876]
    25220[56]

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 13, '11 at 9:13p
activeJul 14, '11 at 1:05p
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase