Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #9482 > unrolled thread

Re: Please critique my script

Started byMRAB <python@mrabarnett.plus.com>
First post2011-07-14 20:12 +0100
Last post2011-07-14 23:47 +0100
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Please critique my script MRAB <python@mrabarnett.plus.com> - 2011-07-14 20:12 +0100
    Re: Please critique my script Peter Otten <__peter__@web.de> - 2011-07-14 22:53 +0200
      Re: Please critique my script MRAB <python@mrabarnett.plus.com> - 2011-07-14 23:47 +0100

#9482 — Re: Please critique my script

FromMRAB <python@mrabarnett.plus.com>
Date2011-07-14 20:12 +0100
SubjectRe: Please critique my script
Message-ID<mailman.1033.1310670724.1164.python-list@python.org>
[snip]
raw_input() returns a string, so there's no need for these 3 lines:

 > y = str(y)
 > z = str(z)
 > p = str(p)

 > pagedef = ("http://www.localcallingguide.com/xmllocalprefix.php?npa=" 
+ y + "&nxx=" + z)
 > print "Querying", pagedef
 >
 > #------Get info from NANPA.com ----------
 > urllib2.install_opener(opener)
 > page = urllib2.urlopen(pagedef)
 > soup = BeautifulSoup(page)
 > soup = str(soup)
 >
 > #------Parse Gathered Data----------
 > for line in npaReg.findall(soup):
 >     npalist.insert(count, line)
 >     count = count + 1
 >
 > for line2 in nxxReg.findall(soup):
 >     nxxlist.insert(count2, line2)
 >     count2 = count2 + 1
 >
enumerate will help you here:

for count, line in enumerate(npaReg.findall(soup)):
     npalist.insert(count, line)

for count2, line2 in enumerate(nxxReg.findall(soup)):
     nxxlist.insert(count2, line2)

 > #-----Sort, remove duplicates, concatenate the last digits for 
similiar NPA/NXX ------
 > for makenewlist in range(0, count):
 >     sortlist.append(npalist.pop(0) + nxxlist.pop(0))
 >
 > sortlist.sort()
 >
 > for sortednumber in sortlist:
 >     if sortednumber not in sortedlist:
 >         sortedlist.append(sortednumber)
 >
If you're going to sort them anyway (so you don't need to preserve the 
existing order), the easiest way to remove duplicates is to use a set:

sortedlist = sorted(set(sortlist))

[snip]

[toc] | [next] | [standalone]


#9490

FromPeter Otten <__peter__@web.de>
Date2011-07-14 22:53 +0200
Message-ID<ivnkvv$jnn$1@solani.org>
In reply to#9482
MRAB wrote:

>> for line2 in nxxReg.findall(soup):
>>     nxxlist.insert(count2, line2)
>>     count2 = count2 + 1
>>
> enumerate will help you here:

> for count2, line2 in enumerate(nxxReg.findall(soup)):
>     nxxlist.insert(count2, line2)
 
An insert() at the end of a list is usually spelt append() in Python ;)
If you are looking for something less baroque

nxxlist = nxxReg.findall(soup)

will do, too.

[toc] | [prev] | [next] | [standalone]


#9493

FromMRAB <python@mrabarnett.plus.com>
Date2011-07-14 23:47 +0100
Message-ID<mailman.1040.1310683659.1164.python-list@python.org>
In reply to#9490
On 14/07/2011 21:53, Peter Otten wrote:
> MRAB wrote:
>
>>> for line2 in nxxReg.findall(soup):
>>>      nxxlist.insert(count2, line2)
>>>      count2 = count2 + 1
>>>
>> enumerate will help you here:
>
>> for count2, line2 in enumerate(nxxReg.findall(soup)):
>>      nxxlist.insert(count2, line2)
>
> An insert() at the end of a list is usually spelt append() in Python ;)
> If you are looking for something less baroque
>
> nxxlist = nxxReg.findall(soup)
>
> will do, too.
>
That's true... :-)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web