Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'classes.': 0.05; 'skip:[ 40': 0.07; '"["': 0.09; 'beating': 0.09; 'collections': 0.09; 'from:addr:python': 0.09; 'python.org?': 0.09; 'subject:don': 0.09; 'throw': 0.09; 'except:': 0.16; 'expression,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'pity': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'reply-to:addr:python-list': 0.16; 'skip:# 60': 0.16; 'this:': 0.16; '>>>': 0.16; 'subject:list': 0.16; 'figure': 0.21; 'variable': 0.21; 'subject:not': 0.21; '(this': 0.22; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'stuff': 0.22; 'trying': 0.23; 'code': 0.24; 'tried': 0.27; "i'm": 0.27; 'pass': 0.28; 'somebody': 0.28; 'putting': 0.28; 'received:84': 0.28; 'import': 0.29; 'example': 0.30; 'module': 0.30; 'list1': 0.30; 'seem': 0.31; 'print': 0.32; 'list': 0.32; "i've": 0.33; 'to:addr:python-list': 0.34; 'header:User-Agent:1': 0.34; 'calling': 0.34; 'like:': 0.35; 'reply-to:addr:python.org': 0.35; 'try:': 0.35; 'regular': 0.36; 'digit': 0.37; 'could': 0.37; 'subject:: ': 0.38; 'something': 0.38; 'data': 0.39; 'i.e.': 0.39; 'to:addr:python.org': 0.39; 'might': 0.39; 'format': 0.40; 'skip:r 20': 0.40; 'phone': 0.63; 'skip:5 10': 0.63; 'skip:l 50': 0.67; 'experts': 0.69; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'concatenate': 0.84; 'items,': 0.91; '150': 0.93; 'subject:good': 0.93; 'subject:know': 0.93 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AlAGAFEUHk7Unw4S/2dsb2JhbABTmEqOeHeIegLCU4Y6BJdji08 Date: Wed, 13 Jul 2011 22:58:50 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.18) Gecko/20110616 Thunderbird/3.1.11 MIME-Version: 1.0 To: python-list@python.org Subject: Re: I don't know list, I not good at list. References: <77AE044B1BF3944FAE2435F395F11B4B018599C6@clt-exmb02.bbtnet.com> In-Reply-To: <77AE044B1BF3944FAE2435F395F11B4B018599C6@clt-exmb02.bbtnet.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1310594335 news.xs4all.nl 23904 [2001:888:2000:d::a6]:47013 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:9449 > 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(, {'25224': {'8', '7', '6'}, '25220': {'5', '6'}}) >>> for k, v in d.items(): print(k + "[" + "".join(v) + "]") 25224[876] 25220[56]