# by default, only the result of the last expression in a cell is displayed after evaluation.
# the following forces display of *all* self-standing expressions in a cell.
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
This list supports the mutable sequence operations in addition to the common sequence operations.
l = [1, 2, 1, 1, 2, 3, 3, 1]
l
[1, 2, 1, 1, 2, 3, 3, 1]
len(l)
8
l[5]
3
m=l[1:-1] #slices do not change the list, they make a new list
m
[2, 1, 1, 2, 3, 3]
l + ['hello', 'world']
_
l
[1, 2, 1, 1, 2, 3, 3, 1, 'hello', 'world']
[1, 2, 1, 1, 2, 3, 3, 1, 'hello', 'world']
[1, 2, 1, 1, 2, 3, 3, 1]
l # `+` does *not* mutate the list!
l * 3
l
[1, 2, 1, 1, 2, 3, 3, 1, 1, 2, 1, 1, 2, 3, 3, 1, 1, 2, 1, 1, 2, 3, 3, 1]
[1, 2, 1, 1, 2, 3, 3, 1]
sum = 0
for x in l:
sum += x
sum
14
l = list('hell')
l
['h', 'e', 'l', 'l']
l.append('o')
l
['h', 'e', 'l', 'l', 'o']
l[1]
l[5]
'e'
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-18-f21976a050d6> in <module> 1 l[1] ----> 2 l[5] IndexError: list index out of range
l[5]='matt'
l
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-19-66dec5101980> in <module> ----> 1 l[5]='matt' 2 l IndexError: list assignment index out of range
l.append(' there')
l
['h', 'e', 'l', 'l', 'o', ' there']
m=list(range(5))
m
[0, 1, 2, 3, 4]
l
['h', 'e', 'l', 'l', 'o', ' there']
del l[-1]
l
['h', 'e', 'l', 'l', 'o']
l.extend(' there')
l
['h', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e']
l[2:7] # does not change the list
del l[2:7]
l
['h', 'e', 'h', 'e', 'r', 'e']
l=[]
m=list('matt')
m
['m', 'a', 't', 't']
m.insert(0, 'Bauer')
m
['Bauer', 'm', 'a', 't', 't']
# find a run of an exact length
import random
flips=''
for j in range(100):
flips=flips+str(random.randint(0, 1))
flips
flips.index('000') # not correct!
flips.index('0001') # not correct!
'1110101101110010000111001000001100010100100111100000110011101100111010011000010110101011101110011010'
15
16
#attempt1
key='000'
#for i in flips:
for counti in range(len(flips)):
if flips[counti]==key[0]:
for j in range(len(key)): # look at following letter
if flips[counti+j]!=key[j]:
break
counti+=1
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-39-7ec89e87f377> in <module> 5 if flips[counti]==key[0]: 6 for j in range(len(key)): # look at following letter ----> 7 if flips[counti+j]!=key[j]: 8 break 9 counti+=1 IndexError: string index out of range
#attempt2
flips
runs=[]
prev=flips[0]
runCount=1
for c in flips[1:]:
if c==prev:
runCount+=1
else:
if prev=='0':
runs.append(runCount)
else:
runs.append(-1*runCount)
runCount=1
prev=c
runs
d=runs.index(3)
d
sum=0
for x in range(d):
sum=sum+abs(runs[x])
sum
'1110101101110010000111001000001100010100100111100000110011101100111010011000010110101011101110011010'
[-3, 1, -1, 1, -2, 1, -3, 2, -1, 4, -3, 2, -1, 5, -2, 3, -1, 1, -1, 2, -1, 2, -4, 5, -2, 2, -3, 1, -2, 2, -3, 1, -1, 2, -2, 4, -1, 1, -2, 1, -1, 1, -1, 1, -3, 1, -3, 2, -2, 1, -1]
15
32
[x for x in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
j=[]
for i in range(10):
j.append(i)
j
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[2*x+1 for x in range(10)] # odd numbers
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
[2*x+1 for x in range(10) if (2*x+1)%3==0] # first 9 odd numbers divisible by 3
[3, 9, 15]
# What circles of radius 1 to 5 have more area than squares with sides 1 to 5?
import math
[(radius,side) for radius in range(1,6) for side in range(1,6) if (math.pi*radius*radius > side*side) ]
[(1, 1), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5)]
# list comprehension [result_expression iteration(s) condition(s)]
adjs = ('hot', 'blue', 'quick')
nouns = ('table', 'fox', 'sky')
phrases = []
for adj in adjs:
for noun in nouns:
phrases.append(adj + ' ' + noun)
phrases
['hot table', 'hot fox', 'hot sky', 'blue table', 'blue fox', 'blue sky', 'quick table', 'quick fox', 'quick sky']
[adj + ' ' + noun for adj in adjs for noun in nouns]
['hot table', 'hot fox', 'hot sky', 'blue table', 'blue fox', 'blue sky', 'quick table', 'quick fox', 'quick sky']
l=[[]]*5
m=[[],[],[],[],[]]
l
m
l[0].append('matt')
l
m[0].append('matt')
m
[[], [], [], [], []]
[[], [], [], [], []]
[['matt'], ['matt'], ['matt'], ['matt'], ['matt']]
[['matt'], [], [], [], []]
A set is a data structure that represents an unordered collection of unique objects (like the mathematical set).
s = {1, 2, 1, 1, 2, 3, 3, 1}
s
{1, 2, 3}
t = {2, 3, 4, 5}
s.union(t)
s
t
{1, 2, 3, 4, 5}
{1, 2, 3}
{2, 3, 4, 5}
s.difference(t)
{1}
s.intersection(t)
{2, 3}
for x in s:
print(x)
1 2 3
s[1]
s[2]
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-9-37ce3d26dd00> in <module> ----> 1 s[1] 2 s[2] TypeError: 'set' object is not subscriptable
1 in s
5 in t
True
True
s - 1
s
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-11-8f2eb23b153b> in <module> ----> 1 s - 1 2 s TypeError: unsupported operand type(s) for -: 'set' and 'int'
A dictionary is a data structure that contains a set of unique key → value mappings.
d = {
'Superman': 'Clark Kent',
'Batman': 'Bruce Wayne',
'Spiderman': 'Peter Parker',
'Ironman': 'Tony Stark'
}
d['Ironman']
'Tony Stark'
d['Ironman'] = 'James Rhodes'
d
{'Superman': 'Clark Kent', 'Batman': 'Bruce Wayne', 'Spiderman': 'Peter Parker', 'Ironman': 'James Rhodes'}
d['pythonguy']="Michael Lee"
d
{'Superman': 'Clark Kent', 'Batman': 'Bruce Wayne', 'Spiderman': 'Peter Parker', 'Ironman': 'James Rhodes', 'pythonguy': 'Michael Lee'}
for x in d:
print(x)
Superman Batman Spiderman Ironman pythonguy
for x in d.values():
print(x)
Clark Kent Bruce Wayne Peter Parker James Rhodes Michael Lee
for k,v in d.items():
print(k,v)
Superman Clark Kent Batman Bruce Wayne Spiderman Peter Parker Ironman James Rhodes pythonguy Michael Lee
'pythonguy' in d
True
'Pythonguy' in d
False
d['Ironman']
d['me']
'James Rhodes'
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-25-e245055d2b39> in <module> 1 d['Ironman'] ----> 2 d['me'] KeyError: 'me'
d.get('Ironman')
d.get('me') # returning none
d.get('me', 'no one')
'James Rhodes'
'no one'
type(d)
dict
d
del d['pythonguy']
d
{'Superman': 'Clark Kent', 'Batman': 'Bruce Wayne', 'Spiderman': 'Peter Parker', 'Ironman': 'James Rhodes', 'pythonguy': 'Michael Lee'}
{'Superman': 'Clark Kent', 'Batman': 'Bruce Wayne', 'Spiderman': 'Peter Parker', 'Ironman': 'James Rhodes'}
d
{'Superman': 'Clark Kent', 'Spiderman': 'Peter Parker', 'Ironman': 'James Rhodes'}
for k,v in d.items():
if v=='Bruce Wayne':
del d[k]
d
--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-31-51bbb1efd4ae> in <module> ----> 1 for k,v in d.items(): 2 if v=='Bruce Wayne': 3 del d[k] 4 d RuntimeError: dictionary changed size during iteration
for k,v in d.copy().items():
if v=='James Rhodes':
del d[k]
d
{'Superman': 'Clark Kent', 'Spiderman': 'Peter Parker'}
{e:2**e for e in range(0,100,10)}
{0: 1, 10: 1024, 20: 1048576, 30: 1073741824, 40: 1099511627776, 50: 1125899906842624, 60: 1152921504606846976, 70: 1180591620717411303424, 80: 1208925819614629174706176, 90: 1237940039285380274899124224}
{x:y for x in range(3) for y in range(10)}
{0: 9, 1: 9, 2: 9}
sentence = 'a man a plan a canal panama'
{w:w[::-1] for w in sentence.split()}
{'a': 'a', 'man': 'nam', 'plan': 'nalp', 'canal': 'lanac', 'panama': 'amanap'}
[(w,w[::-1]) for w in sentence.split()]
[('a', 'a'), ('man', 'nam'), ('a', 'a'), ('plan', 'nalp'), ('a', 'a'), ('canal', 'lanac'), ('panama', 'amanap')]
import urllib.request
peter_pan_text = urllib.request.urlopen('https://www.gutenberg.org/files/16/16-0.txt').read().decode()
peter_pan_text[:100]
'\ufeffThe Project Gutenberg eBook of Peter Pan, by James M. Barrie\r\n\r\nThis eBook is for the use of anyone'
peter_pan_words = peter_pan_text.split() #list of strings
peter_pan_words[:10]
['\ufeffThe', 'Project', 'Gutenberg', 'eBook', 'of', 'Peter', 'Pan,', 'by', 'James', 'M.']
peter_pan_words.count("Peter")
peter_pan_words.count("peter")
238
0
peter_pan_words.index("Hook")
12453
peter_pan_words.index("Hook",12454)
15387
d={}
for word in peter_pan_words:
if word not in d:
d[word]=peter_pan_words.count(word)
d
{'\ufeffThe': 1, 'Project': 79, 'Gutenberg': 22, 'eBook': 6, 'of': 919, 'Peter': 238, 'Pan,': 3, 'by': 180, 'James': 7, 'M.': 3, 'Barrie': 3, 'This': 31, 'is': 340, 'for': 377, 'the': 2318, 'use': 19, 'anyone': 4, 'anywhere': 2, 'in': 681, 'United': 15, 'States': 7, 'and': 1388, 'most': 42, 'other': 72, 'parts': 4, 'world': 5, 'at': 318, 'no': 135, 'cost': 2, 'with': 358, 'almost': 36, 'restrictions': 2, 'whatsoever.': 2, 'You': 36, 'may': 48, 'copy': 8, 'it,': 63, 'give': 26, 'it': 473, 'away': 29, 'or': 130, 're-use': 2, 'under': 28, 'terms': 21, 'License': 8, 'included': 2, 'this': 192, 'online': 4, 'www.gutenberg.org.': 2, 'If': 34, 'you': 406, 'are': 188, 'not': 383, 'located': 7, 'States,': 4, 'will': 86, 'have': 249, 'to': 1206, 'check': 4, 'laws': 11, 'country': 5, 'where': 44, 'before': 42, 'using': 8, 'eBook.': 2, 'Title:': 1, 'Pan': 14, 'Wendy': 200, 'Author:': 1, 'Release': 1, 'Date:': 1, 'July,': 1, '1991': 2, '[eBook': 1, '#16]': 1, '[Most': 1, 'recently': 1, 'updated:': 1, 'February': 1, '15,': 1, '2021]': 1, 'Language:': 1, 'English': 3, 'Character': 1, 'set': 15, 'encoding:': 1, 'UTF-8': 1, 'Produced': 1, 'by:': 1, 'Duncan': 2, 'Research': 1, '***': 4, 'START': 1, 'OF': 10, 'THE': 31, 'PROJECT': 4, 'GUTENBERG': 3, 'EBOOK': 2, 'PETER': 4, 'PAN': 2, '[PETER': 1, 'AND': 2, 'WENDY]': 1, 'J.': 1, '[James': 1, 'Matthew': 1, 'Barrie]': 1, 'A': 19, 'Millennium': 1, 'Fulcrum': 1, 'Edition': 1, 'produced': 2, 'Research.': 1, 'Note': 1, 'that': 565, 'while': 26, 'a': 956, 'copyright': 21, 'was': 899, 'initially': 1, 'claimed': 2, 'labor': 1, 'involved': 1, 'digitization,': 1, 'claim': 2, 'consistent': 2, 'current': 2, 'requirements.': 2, 'text,': 1, 'which': 123, 'matches': 1, '1911': 1, 'original': 2, 'publication,': 1, 'public': 7, 'domain': 3, 'US.': 1, 'Contents': 1, 'Chapter': 34, 'I.': 3, 'BREAKS': 2, 'THROUGH': 2, 'II.': 2, 'SHADOW': 2, 'III.': 2, 'COME': 6, 'AWAY,': 2, 'AWAY!': 2, 'IV.': 2, 'FLIGHT': 2, 'V.': 3, 'ISLAND': 2, 'TRUE': 2, 'VI.': 2, 'LITTLE': 2, 'HOUSE': 2, 'VII.': 2, 'HOME': 6, 'UNDER': 3, 'GROUND': 2, 'VIII.': 2, 'MERMAIDS’': 2, 'LAGOON': 2, 'IX.': 2, 'NEVER': 2, 'BIRD': 2, 'X.': 2, 'HAPPY': 2, 'XI.': 2, 'WENDY’S': 2, 'STORY': 2, 'XII.': 2, 'CHILDREN': 2, 'ARE': 2, 'CARRIED': 2, 'OFF': 2, 'XIII.': 2, 'DO': 2, 'YOU': 8, 'BELIEVE': 2, 'IN': 3, 'FAIRIES?': 2, 'XIV.': 2, 'PIRATE': 2, 'SHIP': 2, 'XV.': 2, '“HOOK': 2, 'OR': 8, 'ME': 2, 'THIS': 5, 'TIME”': 2, 'XVI.': 2, 'RETURN': 2, 'XVII.': 2, 'WHEN': 2, 'WENDY': 2, 'GREW': 2, 'UP': 2, 'All': 17, 'children,': 6, 'except': 20, 'one,': 15, 'grow': 8, 'up.': 14, 'They': 101, 'soon': 26, 'know': 65, 'they': 464, 'up,': 18, 'way': 60, 'knew': 63, 'this.': 6, 'One': 9, 'day': 11, 'when': 152, 'she': 465, 'two': 36, 'years': 3, 'old': 23, 'playing': 10, 'garden,': 1, 'plucked': 2, 'another': 24, 'flower': 2, 'ran': 17, 'her': 360, 'mother.': 9, 'I': 253, 'suppose': 7, 'must': 68, 'looked': 33, 'rather': 40, 'delightful,': 1, 'Mrs.': 72, 'Darling': 93, 'put': 40, 'hand': 32, 'heart': 13, 'cried,': 42, '“Oh,': 24, 'why': 13, 'can’t': 24, 'remain': 5, 'like': 86, 'ever!”': 1, 'all': 220, 'passed': 12, 'between': 13, 'them': 165, 'on': 323, 'subject,': 2, 'but': 376, 'henceforth': 2, 'always': 50, 'after': 44, 'two.': 3, 'Two': 2, 'beginning': 7, 'end.': 3, 'Of': 37, 'course': 55, 'lived': 6, '14,': 1, 'until': 21, 'came': 71, 'mother': 33, 'chief': 3, 'one.': 11, 'She': 109, 'lovely': 14, 'lady,': 7, 'romantic': 4, 'mind': 11, 'such': 60, 'sweet': 7, 'mocking': 4, 'mouth.': 5, 'Her': 10, 'tiny': 5, 'boxes,': 1, 'one': 169, 'within': 11, 'other,': 5, 'come': 50, 'from': 145, 'puzzling': 2, 'East,': 1, 'however': 3, 'many': 25, 'discover': 4, 'there': 111, 'more;': 2, 'mouth': 15, 'had': 497, 'kiss': 8, 'could': 139, 'never': 65, 'get,': 1, 'though': 46, 'was,': 14, 'perfectly': 6, 'conspicuous': 1, 'right-hand': 2, 'corner.': 2, 'The': 146, 'Mr.': 46, 'won': 1, 'this:': 2, 'gentlemen': 3, 'who': 133, 'been': 135, 'boys': 61, 'girl': 10, 'discovered': 7, 'simultaneously': 2, 'loved': 13, 'her,': 37, 'house': 24, 'propose': 1, 'Darling,': 10, 'took': 24, 'cab': 2, 'nipped': 2, 'first,': 7, 'so': 197, 'he': 866, 'got': 33, 'her.': 41, 'He': 163, 'innermost': 1, 'box': 2, 'kiss.': 3, 'about': 106, 'box,': 2, 'time': 81, 'gave': 36, 'up': 112, 'trying': 10, 'thought': 69, 'Napoleon': 1, 'can': 50, 'picture': 1, 'him': 186, 'trying,': 2, 'then': 67, 'going': 31, 'off': 31, 'passion,': 1, 'slamming': 1, 'door.': 4, 'used': 17, 'boast': 2, 'only': 84, 'respected': 1, 'him.': 66, 'those': 17, 'deep': 2, 'ones': 13, 'stocks': 3, 'shares.': 1, 'really': 44, 'knows,': 1, 'quite': 62, 'seemed': 23, 'know,': 11, 'often': 17, 'said': 218, 'were': 243, 'shares': 1, 'down': 50, 'would': 209, 'made': 48, 'any': 67, 'woman': 7, 'respect': 3, 'married': 6, 'white,': 3, 'first': 64, 'kept': 12, 'books': 1, 'perfectly,': 1, 'gleefully,': 3, 'as': 312, 'if': 125, 'game,': 3, 'much': 30, 'Brussels': 1, 'sprout': 1, 'missing;': 1, 'whole': 14, 'cauliflowers': 1, 'dropped': 12, 'out,': 15, 'instead': 13, 'pictures': 1, 'babies': 4, 'without': 41, 'faces.': 1, 'drew': 11, 'should': 47, 'totting': 2, 'Darling’s': 7, 'guesses.': 2, 'John,': 21, 'Michael.': 6, 'For': 26, 'week': 6, 'doubtful': 2, 'whether': 18, 'be': 247, 'able': 10, 'keep': 14, 'feed.': 1, 'frightfully': 10, 'proud': 7, 'very': 64, 'honourable,': 1, 'sat': 34, 'edge': 3, 'bed,': 11, 'holding': 5, 'calculating': 1, 'expenses,': 3, 'imploringly.': 2, 'wanted': 14, 'risk': 1, 'what': 105, 'might,': 1, 'his': 455, 'way;': 1, 'pencil': 1, 'piece': 4, 'paper,': 2, 'confused': 2, 'suggestions': 1, 'begin': 1, 'again.': 22, '“Now': 8, 'don’t': 54, 'interrupt,”': 1, 'beg': 2, '“I': 159, 'pound': 2, 'seventeen': 1, 'here,': 6, 'six': 9, 'office;': 1, 'cut': 13, 'my': 69, 'coffee': 1, 'office,': 3, 'say': 36, 'ten': 8, 'shillings,': 1, 'making': 10, 'nine': 12, 'six,': 2, 'your': 80, 'eighteen': 1, 'three': 24, 'makes': 7, 'seven,': 2, 'five': 3, 'naught': 2, 'cheque-book': 3, 'eight': 3, 'seven—who': 1, 'moving?—eight': 1, 'dot': 1, 'carry': 10, 'seven—don’t': 1, 'speak,': 2, 'own—and': 1, 'lent': 1, 'man': 33, 'door—quiet,': 1, 'child—dot': 1, 'child—there,': 1, 'you’ve': 1, 'done': 10, 'it!—did': 1, 'seven?': 1, 'yes,': 5, 'seven;': 1, 'question': 7, 'is,': 6, 'we': 138, 'try': 2, 'year': 5, 'seven?”': 1, '“Of': 8, 'can,': 1, 'George,”': 5, 'cried.': 18, 'But': 65, 'prejudiced': 2, 'Wendy’s': 26, 'favour,': 1, 'grander': 1, 'character': 2, '“Remember': 1, 'mumps,”': 1, 'warned': 3, 'threateningly,': 1, 'went': 48, '“Mumps': 1, 'pound,': 1, 'down,': 7, 'daresay': 2, 'more': 62, 'thirty': 2, 'shillings—don’t': 1, 'speak—measles': 1, 'five,': 1, 'German': 1, 'measles': 2, 'half': 8, 'guinea,': 1, 'fifteen': 2, 'six—don’t': 1, 'waggle': 1, 'finger—whooping-cough,': 1, 'shillings”—and': 1, 'went,': 5, 'added': 5, 'differently': 1, 'each': 31, 'time;': 6, 'last': 48, 'just': 73, 'through,': 3, 'mumps': 1, 'reduced': 1, 'twelve': 1, 'kinds': 1, 'treated': 6, 'There': 43, 'same': 21, 'excitement': 1, 'over': 56, 'Michael': 68, 'even': 42, 'narrower': 1, 'squeak;': 1, 'both': 16, 'kept,': 1, 'soon,': 2, 'might': 28, 'seen': 20, 'row': 4, 'Miss': 2, 'Fulsom’s': 2, 'Kindergarten': 1, 'school,': 3, 'accompanied': 4, 'their': 213, 'nurse.': 4, 'everything': 6, 'so,': 9, 'passion': 2, 'being': 26, 'exactly': 13, 'neighbours;': 1, 'course,': 12, 'As': 24, 'poor,': 1, 'owing': 4, 'amount': 1, 'milk': 2, 'children': 75, 'drank,': 1, 'nurse': 5, 'prim': 1, 'Newfoundland': 1, 'dog,': 2, 'called': 30, 'Nana,': 9, 'belonged': 1, 'particular': 4, 'Darlings': 2, 'engaged': 1, 'important,': 1, 'however,': 17, 'become': 9, 'acquainted': 1, 'Kensington': 3, 'Gardens,': 1, 'spent': 3, 'spare': 1, 'peeping': 6, 'into': 100, 'perambulators,': 1, 'hated': 5, 'careless': 5, 'nursemaids,': 1, 'whom': 11, 'followed': 14, 'homes': 1, 'complained': 1, 'mistresses.': 1, 'proved': 2, 'treasure': 1, 'How': 10, 'thorough': 1, 'bath-time,': 2, 'moment': 48, 'night': 37, 'charges': 2, 'slightest': 3, 'cry.': 5, 'kennel': 6, 'nursery.': 4, 'genius': 3, 'knowing': 7, 'cough': 1, 'thing': 33, 'patience': 1, 'needs': 1, 'stocking': 2, 'around': 17, 'throat.': 1, 'believed': 7, 'old-fashioned': 1, 'remedies': 1, 'rhubarb': 1, 'leaf,': 2, 'sounds': 3, 'contempt': 3, 'new-fangled': 1, 'talk': 6, 'germs,': 1, 'on.': 11, 'It': 121, 'lesson': 2, 'propriety': 1, 'see': 85, 'escorting': 1, 'walking': 3, 'sedately': 1, 'side': 12, 'well': 24, 'behaved,': 1, 'butting': 1, 'back': 31, 'line': 4, 'strayed.': 1, 'On': 12, 'John’s': 5, 'footer': 1, 'days': 18, 'once': 40, 'forgot': 9, 'sweater,': 1, 'usually': 3, 'carried': 11, 'an': 102, 'umbrella': 1, 'case': 1, 'rain.': 1, 'room': 20, 'basement': 1, 'school': 5, 'nurses': 1, 'wait.': 3, 'forms,': 1, 'Nana': 34, 'lay': 17, 'floor,': 7, 'difference.': 1, 'affected': 2, 'ignore': 1, 'inferior': 3, 'social': 1, 'status': 5, 'themselves,': 7, 'despised': 3, 'light': 26, 'talk.': 1, 'resented': 2, 'visits': 1, 'nursery': 23, 'friends,': 3, 'did': 115, 'whipped': 4, 'Michael’s': 7, 'pinafore': 1, 'blue': 4, 'braiding,': 1, 'smoothed': 1, 'out': 115, 'dash': 1, 'hair.': 2, 'No': 12, 'possibly': 2, 'conducted': 2, 'correctly,': 1, 'yet': 14, 'sometimes': 22, 'wondered': 2, 'uneasily': 1, 'neighbours': 1, 'talked.': 1, 'position': 3, 'city': 1, 'consider.': 1, 'also': 18, 'troubled': 4, 'way.': 9, 'feeling': 15, 'admire': 2, 'admires': 1, 'tremendously,': 2, 'assure': 1, 'him,': 53, 'sign': 3, 'specially': 4, 'nice': 12, 'father.': 2, 'Lovely': 1, 'dances': 2, 'followed,': 1, 'servant,': 1, 'Liza,': 2, 'allowed': 5, 'join.': 1, 'Such': 4, 'midget': 1, 'long': 50, 'skirt': 1, 'maid’s': 1, 'cap,': 1, 'sworn,': 1, 'engaged,': 1, 'gaiety': 2, 'romps!': 1, 'And': 27, 'gayest': 1, 'pirouette': 1, 'wildly': 3, 'kiss,': 3, 'dashed': 2, 'it.': 56, 'simpler': 1, 'happier': 1, 'family': 3, 'coming': 9, 'Pan.': 5, 'heard': 43, 'tidying': 2, 'children’s': 4, 'minds.': 1, 'nightly': 1, 'custom': 3, 'every': 39, 'good': 25, 'asleep': 9, 'rummage': 1, 'minds': 4, 'things': 29, 'straight': 7, 'next': 10, 'morning,': 3, 'repacking': 1, 'proper': 4, 'places': 2, 'articles': 1, 'wandered': 2, 'during': 2, 'day.': 1, 'awake': 4, '(but': 2, 'can’t)': 1, 'own': 19, 'doing': 10, 'this,': 18, 'find': 15, 'interesting': 2, 'watch': 8, 'drawers.': 1, 'knees,': 4, 'expect,': 1, 'lingering': 2, 'humorously': 1, 'some': 30, 'contents,': 1, 'wondering': 3, 'earth': 3, 'picked': 2, 'discoveries': 1, 'sweet,': 4, 'pressing': 3, 'cheek': 2, 'kitten,': 1, 'hurriedly': 1, 'stowing': 1, 'sight.': 5, 'When': 18, 'wake': 5, 'naughtiness': 1, 'evil': 5, 'passions': 1, 'bed': 25, 'folded': 2, 'small': 11, 'placed': 2, 'bottom': 1, 'top,': 2, 'beautifully': 2, 'aired,': 3, 'spread': 1, 'prettier': 1, 'thoughts,': 1, 'ready': 3, 'ever': 37, 'map': 5, 'person’s': 1, 'mind.': 5, 'Doctors': 1, 'draw': 9, 'maps': 2, 'you,': 24, 'intensely': 1, 'interesting,': 2, 'catch': 4, 'child’s': 1, 'mind,': 1, 'confused,': 1, 'keeps': 2, 'round': 43, 'time.': 9, 'zigzag': 1, 'lines': 2, 'temperature': 1, 'card,': 1, 'these': 25, 'probably': 8, 'roads': 1, 'island,': 9, 'Neverland': 11, 'less': 9, 'astonishing': 2, 'splashes': 1, 'colour': 5, 'here': 16, 'there,': 9, 'coral': 2, 'reefs': 1, 'rakish-looking': 2, 'craft': 3, 'offing,': 1, 'savages': 2, 'lonely': 4, 'lairs,': 1, 'gnomes': 1, 'mostly': 3, 'tailors,': 1, 'caves': 2, 'through': 46, 'river': 1, 'runs,': 2, 'princes': 1, 'elder': 1, 'brothers,': 1, 'hut': 1, 'fast': 4, 'decay,': 1, 'lady': 15, 'hooked': 1, 'nose.': 1, 'easy': 5, 'all,': 11, 'religion,': 1, 'fathers,': 1, 'pond,': 1, 'needle-work,': 1, 'murders,': 1, 'hangings,': 1, 'verbs': 1, 'take': 30, 'dative,': 1, 'chocolate': 2, 'pudding': 1, 'day,': 6, 'getting': 11, 'braces,': 1, 'ninety-nine,': 1, 'three-pence': 1, 'pulling': 5, 'tooth': 1, 'yourself,': 1, 'on,': 10, 'either': 4, 'part': 20, 'island': 18, 'showing': 9, 'confusing,': 1, 'especially': 7, 'nothing': 15, 'stand': 6, 'still.': 3, 'Neverlands': 2, 'vary': 1, 'deal.': 1, 'John’s,': 1, 'instance,': 5, 'lagoon': 13, 'flamingoes': 1, 'flying': 14, 'John': 76, 'shooting,': 1, 'Michael,': 17, 'small,': 1, 'flamingo': 2, 'lagoons': 1, 'boat': 3, 'turned': 12, 'upside': 1, 'sands,': 1, 'wigwam,': 1, 'leaves': 9, 'deftly': 2, 'sewn': 2, 'together.': 3, 'friends': 1, 'night,': 10, 'pet': 3, 'wolf': 2, 'forsaken': 1, 'its': 27, 'parents,': 2, 'resemblance,': 1, 'stood': 22, 'still': 51, 'other’s': 6, 'nose,': 1, 'forth.': 3, 'magic': 2, 'shores': 1, 'play': 8, 'beaching': 1, 'coracles.': 1, 'We': 16, 'too': 34, 'there;': 3, 'hear': 32, 'sound': 28, 'surf,': 1, 'shall': 25, 'land': 5, 'more.': 8, 'delectable': 1, 'islands': 1, 'snuggest': 1, 'compact,': 1, 'large': 13, 'sprawly,': 1, 'tedious': 1, 'distances': 1, 'adventure': 10, 'another,': 7, 'nicely': 2, 'crammed.': 1, 'chairs': 2, 'table-cloth,': 1, 'least': 11, 'alarming,': 1, 'minutes': 5, 'go': 56, 'sleep': 13, 'becomes': 1, 'real.': 1, 'That': 14, 'night-lights.': 2, 'Occasionally': 1, 'travels': 1, 'found': 38, 'understand,': 3, 'perplexing': 1, 'word': 8, 'Peter.': 27, 'Peter,': 50, 'minds,': 1, 'began': 17, 'scrawled': 1, 'name': 8, 'bolder': 1, 'letters': 2, 'than': 60, 'words,': 8, 'gazed': 5, 'felt': 22, 'oddly': 2, 'cocky': 1, 'appearance.': 1, '“Yes,': 20, 'cocky,”': 1, 'admitted': 3, 'regret.': 1, 'questioning': 1, '“But': 17, ...}
len(d)
9091
maxCount=0;
maxKey=""
for k,v in d.items():
if v > maxCount:
maxCount=v
maxKey=k
maxCount
maxKey
2318
'the'
sort(d)
d
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-51-d566e75bbe4f> in <module> ----> 1 sort(d) 2 d NameError: name 'sort' is not defined
?sorted #gets help file
sorted(['i', 'like', 'cakes'])
sorted(['i', 'like', 'cakes'], reverse=True)
sorted(['i', 'like', 'cakes'], key=len, reverse=True)
Object `sorted #gets help file` not found.
['cakes', 'i', 'like']
['like', 'i', 'cakes']
['cakes', 'like', 'i']
coords = [(0, 0), (-5, 0), (1, -100), (2, 2), (50, 50), (-5, 5)]
coords
[(0, 0), (-5, 0), (1, -100), (2, 2), (50, 50), (-5, 5)]
sorted(coords)
coords
[(-5, 0), (-5, 5), (0, 0), (1, -100), (2, 2), (50, 50)]
[(0, 0), (-5, 0), (1, -100), (2, 2), (50, 50), (-5, 5)]
import math
def dist(coord):
return math.sqrt(coord[0]**2 + coord[1]**2)
for x in coords:
print(dist(x))
0.0 5.0 100.00499987500625 2.8284271247461903 70.71067811865476 7.0710678118654755
sorted(coords, key=dist)
[(0, 0), (2, 2), (-5, 0), (-5, 5), (50, 50), (1, -100)]
sorted(coords, key=dist, reverse=True)
[(1, -100), (50, 50), (-5, 5), (-5, 0), (2, 2), (0, 0)]
coords
[(0, 0), (-5, 0), (1, -100), (2, 2), (50, 50), (-5, 5)]
number_list = range(-5, 5)
less_than_zero = list(filter(lambda x: x < 0, number_list))
print(less_than_zero)
[-5, -4, -3, -2, -1]