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')

#'https://www.amazon.com/dp/B0027YPQEC'
Out[1]:
19.59

Slicing

In [2]:
s ='abcdefghijklmno'
s[3]
Out[2]:
'd'
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('class2_files/words.txt').read().split('\n')
words[:10]
Out[6]:
['A',
 "A's",
 'AOL',
 "AOL's",
 'Aachen',
 "Aachen's",
 'Aaliyah',
 "Aaliyah's",
 'Aaron',
 "Aaron'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]

second('abcdf')

#print(otherwords)

#print(otherwords(otherwords[0]))
#print(otherwords(otherwords[1]))
#print(otherwords(otherwords[2]))

sorted(otherwords,key=second)
Out[10]:
['cable', 'apple', 'axe']
In [11]:
def righttoleft(x): return x[::-1]

#righttoleft('abcdef')

print(righttoleft(otherwords[0]))
print(righttoleft(otherwords[1]))
print(righttoleft(otherwords[2]))

sorted(otherwords,key=righttoleft)

#sorted(words,key=righttoleft)
exa
elbac
elppa
Out[11]:
['cable', 'apple', 'axe']
In [12]:
for word in otherwords:
    print( '{:>85}'.format(word))
                                                                                  axe
                                                                                cable
                                                                                apple
In [13]:
print(otherwords)
start = otherwords.index('apple')
print(start)
#otherwords[start:start+2]
['axe', 'cable', 'apple']
2
In [14]:
def righttoleft(x): return x[::-1]
reorderedwords = sorted( words, key=righttoleft)
#print(reorderedwords)

start = reorderedwords.index('chuck')
reorderedwords[start]
Out[14]:
'chuck'
In [15]:
start = reorderedwords.index('bit')
print(start)
for word in reorderedwords[start:start+20]:
    print('{:>25}'.format(word))
88554
                      bit
                    habit
                  inhabit
                  cohabit
                   rabbit
               jackrabbit
                   hobbit
                   tidbit
                    debit
                  inhibit
                 prohibit
                  exhibit
                  backbit
                   gambit
                     obit
                    orbit
                 frostbit
                    cubit
                    tacit
                  deficit

Palindromes

In [16]:
def ispal(w): 
    return w==w[::-1]

#s = 'loop'
#'pool' == s[::-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 [17]:
print('a'*5)

w=[]
for l in 'abcd':
    if l!='a':
        w.append(l*5)
    
print(w)

w2 = [l*5 for l in 'abcd' if l!='a']
print(w2)
aaaaa
['bbbbb', 'ccccc', 'ddddd']
['bbbbb', 'ccccc', 'ddddd']
In [40]:
w1 = []
for word in words:
    if len(word)>0 and not word[0].isupper(): # issupper() means the string contains only UPPER CASED and at least 1 
        w1.append(word)
        
#print(w1)

#w = [word for word in words if len(word)>0 and not word[0].isupper()]
#print(w)
In [19]:
len(words),len(w)
Out[19]:
(98569, 3)
In [20]:
w = [word for word in words if len(word)>0 and not word[0].isupper() and not word.endswith("'s")]
In [39]:
len(words),len(w)
#print(w)
Out[39]:
(98569, 64204)

Reversible words

In [38]:
swords = set(words)

reversiblewords1 = []
for word in words:
    if word[::-1] in swords and len(word)>1:
        reversiblewords1.append((word,word[::-1]))

reversiblewords1[:10]
#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)>4: print( '{:>25} {:<25}'.format(b,d) )
Out[38]:
[('MGM', 'MGM'),
 ('abut', 'tuba'),
 ('agar', 'raga'),
 ('ah', 'ha'),
 ('aha', 'aha'),
 ('ajar', 'raja'),
 ('am', 'ma'),
 ('are', 'era'),
 ('ares', 'sera'),
 ('ate', 'eta')]

Timing set vs. list lookup

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

Note that set-membership can be tested much faster than list membership

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

Dictionaries

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

str.replace() for removing punctuation

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