Computer Science 15-110, Fall 2010
Class Notes: One-Dimensional Lists
a = [1, 2, 3, 4] b = [5, 6, 7, 8] c = zip(a,b) print c # [(1, 5), (2, 6), (3, 7), (4, 8)] (aa,bb) = zip(*c) # strange syntax, but this unzips c print aa # (1, 2, 3, 4) print bb # (5, 6, 7, 8) print ((aa == a) and (bb == b)) # False print ((aa == tuple(a)) and (bb == tuple(b))) # True
# Examples from http://www.python.org/dev/peps/pep-0202/ # Note: just because you can do some of these more exotic things, # does not mean you necessarily should! Clarity above all! print [i for i in range(10)] # prints: # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print [i for i in range(20) if i%2 == 0] # prints: # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] nums = [1,2,3,4] fruit = ["Apples", "Peaches", "Pears", "Bananas"] print [(i,f) for i in nums for f in fruit] # prints: # [(1, 'Apples'), (1, 'Peaches'), (1, 'Pears'), (1, 'Bananas'), # (2, 'Apples'), (2, 'Peaches'), (2, 'Pears'), (2, 'Bananas'), # (3, 'Apples'), (3, 'Peaches'), (3, 'Pears'), (3, 'Bananas'), # (4, 'Apples'), (4, 'Peaches'), (4, 'Pears'), (4, 'Bananas')] print [(i,f) for i in nums for f in fruit if f[0] == "P"] # prints: # [(1, 'Peaches'), (1, 'Pears'), # (2, 'Peaches'), (2, 'Pears'), # (3, 'Peaches'), (3, 'Pears'), # (4, 'Peaches'), (4, 'Pears')] print [(i,f) for i in nums for f in fruit if f[0] == "P" if i%2 == 1] # prints: # [(1, 'Peaches'), (1, 'Pears'), (3, 'Peaches'), (3, 'Pears')] print [i for i in zip(nums,fruit) if i[0]%2==0] # prints: # [(2, 'Peaches'), (4, 'Bananas')]
def cmp(s1, s2): # compare s1 and s2 # result > 0 ==> s1 > s2 # result == 0 ==> s1 == s2 # result < 0 ==> s1 < s2 return int(s1) - int(s2) s = ["1", "2", "12", "23", "123"] s.sort() print s # prints ['1', '12', '123', '2', '23'] s.sort(cmp) print s # ['1', '2', '12', '23', '123']
# use list(s) to convert a string to a list of characters a = list("wahoo!") print a # prints: ['w', 'a', 'h', 'o', 'o', '!'] # use "".join(a) to convert a list of characters to a single string s = "".join(a) print s # prints: wahoo! # "".join(a) also works on a list of strings (not just single characters) a = ["parsley", " ", "is", " ", "gharsley"] # by Ogden Nash! s = "".join(a) print s # prints: parsley is gharsley
# If you are going to repeatedly "change" a string (in quotes, # because you cannot change strings, so instead you'll create lots # of new strings, which immediately become garbage), # it can be faster to first convert the string to a list, # change the list (which is mutable), and then convert back # to a string at the end. Check this out: import time print "*****************" print "First, change the string the slow way..." n = 12345678 start0 = time.time() s0 = "abcdefg" for count in xrange(n): c = "z" if s0[3] == "d" else "d" s0 = s0[0:3] + c + s0[4:] elapsed0 = time.time() - start0 print "Total time:", elapsed0 print "*****************" print "Next, the faster way..." start1 = time.time() s1 = "abcdefg" a = list(s1) for count in xrange(n): c = "z" if a[3] == "d" else "d" a[3] = c # this is the magic line we could not do above! s1 = "".join(a) elapsed1 = time.time() - start1 print "Total time:", elapsed1 print "*****************" print "Analysis..." print "Two approaches work the same:", (s0 == s1) print "But the second approach runs %0.1f times faster!" % (elapsed0/elapsed1)
# It can be much faster to join strings in a list than to concatenate # them one-at-a-time. Again, though, it is version- and platform-dependent. import time print "*****************" print "First, concatenate numbers (as strings) the slow way..." n = 1234567 start0 = time.time() s0 = "" for i in xrange(n): s0 += str(i) elapsed0 = time.time() - start0 print "Total time:", elapsed0 print "*****************" print "Next, concatenate numbers (as strings) the faster way..." start1 = time.time() listOfStrings = [ ] for i in xrange(n): listOfStrings += [str(i)] s1 = "".join(listOfStrings) elapsed1 = time.time() - start1 print "Total time:", elapsed1 print "*****************" print "Analysis..." print "Two approaches work the same:", (s0 == s1) print "But the second approach runs %0.1f times faster!" % (elapsed0/elapsed1)
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem