Amazon price grabber recap

In [1]:
import requests
def getprice(pid):
         ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'
         url = 'https://www.amazon.com/dp/'+pid
         s = requests.get(url, headers={'User-Agent':ua})
         pattern = '<span id="priceblock_ourprice" class="a-size-medium a-color-price">$'
         price = float( s.text.split(pattern)[-1].split('</span>')[0] )
         return price

getprice('B0027YPQEC')
Out[1]:
29.04

Slicing

In [2]:
s ='abcdefghijklmno'
s[0]
Out[2]:
'a'
In [3]:
s[1:5]
Out[3]:
'bcde'
In [4]:
s[::2]
Out[4]:
'acegikmo'
In [5]:
s[::-1]
Out[5]:
'onmlkjihgfedcba'
In [6]:
words = open('words.txt').read().split('\n')
words[2000:2010]
Out[6]:
["Bioko's",
 'Bird',
 "Bird's",
 'Birdseye',
 "Birdseye's",
 'Birkenstock',
 'Birmingham',
 "Birmingham's",
 'Biro',
 "Biro's"]

Sorting

In [7]:
sorted(words)[:10]
Out[7]:
['',
 'A',
 "A's",
 'AOL',
 "AOL's",
 'Aachen',
 "Aachen's",
 'Aaliyah',
 "Aaliyah's",
 'Aaron']
In [8]:
sorted(words,reverse=True)[:10]
Out[8]:
['études',
 "étude's",
 'étude',
 'épées',
 'épée',
 'émigrés',
 'émigré',
 "élan's",
 'élan',
 "éclat's"]
In [9]:
otherwords = ['axe','cable','apple']
sorted(otherwords)
Out[9]:
['apple', 'axe', 'cable']
In [10]:
def second(x): return x[1]
sorted(otherwords,key=second)
Out[10]:
['cable', 'apple', 'axe']
In [11]:
def righttoleft(x): return x[::-1]
sorted(otherwords,key=righttoleft)
Out[11]:
['cable', 'apple', 'axe']
In [12]:
for word in otherwords:
    print( '{:>25}'.format(word))
                      axe
                    cable
                    apple
In [13]:
start = otherwords.index('axe')
otherwords[start:start+2]
Out[13]:
['axe', 'cable']
In [14]:
def righttoleft(x): return x[::-1]
reorderedwords = sorted( words, key=righttoleft)
start = reorderedwords.index('chuck')
for word in reorderedwords[start:start+20]:
    print('{:>25}'.format(word))
                    chuck
                woodchuck
                  upchuck
                    shuck
                     luck
                    cluck
                    pluck
                  potluck
                     muck
                    amuck
                  schmuck
                   Zanuck
                    snuck
                     puck
                    truck
                   struck
                awestruck
               moonstruck
            thunderstruck
                     suck
In [45]:
start = reorderedwords.index('bit')
for word in reorderedwords[start:start+20]:
    print('{:>25}'.format(word))
                      bit
                    habit
                  inhabit
                  cohabit
                   rabbit
               jackrabbit
                   hobbit
                   tidbit
                    debit
                  inhibit
                 prohibit
                  exhibit
                  backbit
                   gambit
                     obit
                    orbit
                 frostbit
                    cubit
                    tacit
                  deficit

Palindromes

In [15]:
def ispal(w): return w==w[::-1]
for word in words:
    if ispal(word) and len(word)>1: print(word)
MGM
aha
bib
bob
boob
civic
dad
deed
deified
did
dud
eke
ere
eve
ewe
eye
gag
gig
hah
huh
kayak
kook
level
ma'am
madam
minim
mom
mum
non
noon
nun
oho
pap
peep
pep
pip
poop
pop
pup
radar
redder
refer
rotor
sagas
sees
seres
sexes
shahs
sis
solos
stats
tat
tenet
tit
toot
tot
wow

Three useful features

List comprehensions

In [16]:
w = [l*5 for l in 'abcd' if l!='a']
w
Out[16]:
['bbbbb', 'ccccc', 'ddddd']
In [17]:
w = [word for word in words if len(word)>0 and not word[0].isupper()]
In [18]:
len(words),len(w)
Out[18]:
(98569, 80471)
In [19]:
w = [word for word in words if len(word)>0 and not word[0].isupper() and not word.endswith("'s")]
In [20]:
len(words),len(w)
Out[20]:
(98569, 64204)

Reversible words

In [21]:
swords = set(words)
reversiblewords = [(word,word[::-1]) for word in words if word[::-1] in swords and len(word)>1]
for b,d in reversiblewords:
    if b<=d and len(b)>3: print( '{:>25} {:<25}'.format(b,d) )
                     abut tuba                     
                     agar raga                     
                     ajar raja                     
                     ares sera                     
                     avid diva                     
                     bard drab                     
                     bats stab                     
                     boob boob                     
                     brag garb                     
                     buns snub                     
                     burg grub                     
                     buts stub                     
                    civic civic                    
                    debut tubed                    
                    decaf faced                    
                    decal laced                    
                     deed deed                     
                     deep peed                     
                    deeps speed                    
                     deer reed                     
                    defer refed                    
                  deified deified                  
                  deliver reviled                  
                   denier reined                   
                    denim mined                    
                    deres sered                    
                 desserts stressed                 
                    devil lived                    
                     dial laid                     
                   diaper repaid                   
                     doom mood                     
                     door rood                     
                     draw ward                     
                   drawer reward                   
                    draws sward                    
                     dray yard                     
                     dual laud                     
                     edit tide                     
                     emir rime                     
                     emit time                     
                     ergo ogre                     
                     evil live                     
                   eviler relive                   
                     flog golf                     
                     flow wolf                     
                     gals slag                     
                     girt trig                     
                     gnat tang                     
                     gnus sung                     
                     gulp plug                     
                     gums smug                     
                     guns snug                     
                     hahs shah                     
                     hoop pooh                     
                    kayak kayak                    
                     keel leek                     
                    keels sleek                    
                     keep peek                     
                    knits stink                    
                     kook kook                     
                    lager regal                    
                     leer reel                     
                    leper repel                    
                    level level                    
                    lever revel                    
                     liar rail                     
                     loop pool                     
                    loops spool                    
                     loot tool                     
                   looter retool                   
                    loots stool                    
                    ma'am ma'am                    
                    madam madam                    
                     mart tram                     
                     maws swam                     
                     meet teem                     
                     mils slim                     
                    minim minim                    
                     moor room                     
                     naps span                     
                     nips spin                     
                     noon noon                     
                     nuts stun                     
                     pals slap                     
                     pans snap                     
                     part trap                     
                    parts strap                    
                     paws swap                     
                    peels sleep                    
                     peep peep                     
                     pees seep                     
                    perts strep                    
                     pets step                     
                     pins snip                     
                     pols slop                     
                    pools sloop                    
                     poop poop                     
                    ports strop                    
                     pots stop                     
                    radar radar                    
                     raps spar                     
                     rats star                     
                    rebut tuber                    
                   redder redder                   
                   redraw warder                   
                    refer refer                    
                    remit timer                    
                    rotor rotor                    
                    sagas sagas                    
                     saps spas                     
                     sees sees                     
                    seres seres                    
                    sexes sexes                    
                    shahs shahs                    
                   sleets steels                   
                   sloops spools                   
                    smart trams                    
                    snaps spans                    
                    snips spins                    
                     snit tins                     
                   snoops spoons                   
                     snot tons                     
                    solos solos                    
                     spat taps                     
                     spay yaps                     
                     spit tips                     
                   sports strops                   
                     spot tops                     
                    spots stops                    
                    sprat tarps                    
                    stats stats                    
                     stew wets                     
                    straw warts                    
                     sway yaws                     
                    tenet tenet                    
                     toot toot                     
                     tort trot                     

Timing set vs. list lookup

In [22]:
from time import time
In [23]:
longlist = list(range(0,40000000))
In [24]:
tic = time()
-1 in longlist
toc = time()
toc-tic
Out[24]:
0.4289274215698242
In [25]:
slonglist = set(longlist)
In [26]:
tic = time()
-1 in slonglist
toc = time()
toc-tic
Out[26]:
5.054473876953125e-05

Testing for membership in the set is 10,000 times faster in this example!

Dictionaries

In [27]:
d = {'apple':'crunchy fruit','banana':'long yellow fruit','zebra':'striped animal'}
In [28]:
d['zebra']
Out[28]:
'striped animal'
In [29]:
d.keys()
Out[29]:
dict_keys(['zebra', 'banana', 'apple'])
In [30]:
for item in d:
    print(item)
zebra
banana
apple
In [31]:
'banana' in d
Out[31]:
True
In [32]:
d['duck'] = 'quacking bird'
d
Out[32]:
{'apple': 'crunchy fruit',
 'banana': 'long yellow fruit',
 'duck': 'quacking bird',
 'zebra': 'striped animal'}

str.replace() for removing punctuation

In [33]:
s = 'Hello, there!'
s.replace(',','')
Out[33]:
'Hello there!'
In [34]:
punc = ',.;!?'
for p in punc: s = s.replace(p,'')
s
Out[34]:
'Hello there'
In [35]:
for x in punc:
    print(x)
,
.
;
!
?
In [ ]: