CSCE 110 — Programming IList Comprehension, Writing to a File, andDictionariesDr. Tiffani L. WilliamsDepartment of Computer Science and EngineeringTexas A&M UniversityFall 2011List comprehension and other list operationsListing 1: Interactive Python shell1 >>> list = [ ’ apple ’, ’ banana ’, ’ carrot ’ , ’ date ’]2 >>> [x + ’s ’ for x in list ] # list com p reh e nsi o n3 [ ’ apples ’ , ’ bananas ’, ’ carrots ’, ’ dates ’]4 >>> list2 = [1 , 5 , 10 , 3, 17 , 101 , 99]5 >>> [x * 2 for x in list2 ] # li st com p reh e nsi o n6 [2 , 10 , 20 , 6, 34 , 202 , 198]7 >>> list2 . sort () # sort list in in crea s ing order8 >>> list29 [1 , 3, 5, 10 , 17 , 99 , 101]10 >>> list2 . reverse () # rev e r s e the list11 >>> list212 [101 , 99 , 17 , 10 , 5 , 3, 1] Writing to a fileListing 2: write-file-random-numbers.py1 ’’’ Writ e 100 random nu m be rs to a file ’’ ’23 import random45 # Be careful ! You will o v erwri t e a file nam e d6 # random - n u m be rs . txt if it e x i s t s in y our c u r re nt7 # d i recto r y .8 output_fi le = open ( ’ random - nu mb ers . txt ’ , ’w ’)9 for i in range (100):10 r = random . randin t (1 ,10000)11 outpu t_fil e . write ( str ( r) + ’\ n ’)12 output_fi le . close ()13 print ’ Done ’ DictionariesListing 3: Interactive Python shell1 >>> birds = {’ canada goose ’ : 3, ’ n orth ern ful mar ’ : 1}2 >>> birds3 { ’ ca nada goose ’ : 3, ’ n orthe rn fulmar ’ : 1}4 >>> birds [ ’ northe rn fulmar ’]5 16 >>> birds = {’ canada goose ’ : 3, ’ n orth ern ful mar ’ : 1}7 >>> birds [ ’ canada goose ’]8 39 >>> birds [ ’long - tailed jaeger ’]10 Traceba ck ( most recent call last ):11 File " <stdin >" , line 1 , in < fragment >12 KeyErr or : ’long - tailed jaeg er ’13 >>> birds = {’ eagle ’ : 999 , ’ snow goose ’ : 33}14 >>> ’eagle ’ in birds15 True16 >>> del birds [ ’ eagle ’]17 >>> birds18 { ’ snow goose ’: 33}19 >>> Dictionaries — UpdateListing 4: Interactive Python shell1 >>> birds = {}2 >>> birds [ ’ snow goose ’] = 333 >>> birds [ ’ eagle ’] = 9994 >>> birds5 { ’ eagle ’ : 999 , ’ snow goose ’ : 33}6 >>> birds [ ’ eagle ’] = 97 >>> birds8 { ’ eagle ’ : 9, ’ snow goose ’ : 33} Dictionaries — DeleteListing 5: Interactive Python shell1 >>> birds = { ’ snow goose ’ : 33 , ’ eagle ’ : 9}2 >>> del birds [ ’ snow goose ’]3 >>> birds4 { ’ eagle ’ : 9}5 >>> del birds [ ’ gannet ’]6 Trace back ( most recent call last ):7 File " < stdin > " , line 1 , in < fragment >8 KeyErro r : ’ gannet ’ Dictionaries — MethodsListing 6: dictmethod.py1 sci e n tists = { ’ N e wto n ’ : 1642 , ’ Da rwi n ’ : 1809 , ’ Tu r ing ’ : 191 2}23 pr int ’ k eys : ’ , sc i e ntis t s . ke ys ()4 pr int ’ v a lue s : ’ , sc i e n tist s . v alu es ()5 pr int ’ i tem s : ’ , sc i e ntis t s . ite ms ()6 pr int ’ get : ’ , s c i e nti s t s . get ( ’ Cur ie ’ , 186 7)78 temp = { ’ Cur ie ’ : 1867 , ’ H opp er ’ : 1906 , ’ Fr ankl i n ’ : 19 2 0}9 sci e n tists . u pda te ( t emp )10 pr int ’ a fte r u pda t e : ’ , s c i e ntis t s1112 item = s cien t i sts . po p ite m ()13 pr int ’ i tem p opp e d : ’ , ite m14 pr int ’ a fte r p o pit e m : ’ , sc i e ntis t s1516 sci e n tists . cle ar ()17 pr int ’ a fte r c lea r : ’ , s c i enti s t s Dictionaries — MethodsListing 7: Output of the dictmethod.py1 keys : [ ’ T u rin g ’ , ’ Ne wto n ’ , ’ D a rwi n ’ ]2 va lue s : [1912 , 1642 , 180 9]3 it ems : [( ’ Tu rin g ’ , 1912) , ( ’ N ewt o n ’ , 16 42) , ( ’ Darw in ’ , 180 9 )]4 get : 186 75 af ter u p dat e : { ’ C uri e ’ : 1867 , ’ Darw in ’: 1809 , ’ Fra n klin ’: 1920 , ’ T urin g ’ : 1912 ,6 ’ Newt on ’: 1642 , ’ Ho p per ’: 1 906 }7 item p oppe d : ( ’ C uri e ’ , 186 7)8 af ter p o pite m : { ’ Da rwi n ’ : 1809 , ’ F r ank l in ’ : 1920 , ’ Tur i ng ’: 1912 ,9 ’ Newt on ’: 1642 , ’ Ho p per ’: 1 906 }10 af ter c lea r : {} birdwatching.txtListing 8: birdwatching.txt1 canada goose2 canada goose3 long - tailed jaeger4 canada goose5 snow goose6 norther n fulmar7 canada goose8 norther n fulmar9 canada goose10 snow goose11 norther n fulmar Counting items in a file (Version 1)Listing 9: countbirds1.py1 filenam e = raw_inp ut ( ’ Please enter filename : ’)2 infile = open ( filename , ’r ’)34 # Count all the birds .5 count = {}6 for line in infile :7 name = line . strip ()8 if name in count :9 count [ name ] = count [ name ] + 110 else :11 count [ name ] = 11213 infile . close ()1415 # Print .16 for b in count :17 print b , count [ b] Counting items in a file (Version 2)Listing 10: countbirds2.py1 filenam e = raw_inp ut ( ’ Please enter filename : ’)2 infile = open ( filename , ’r ’)34 # Count all the birds .5 count = …
View Full Document