Attachment 'lec5_tuples_lists.py'

Download

   1 #########################
   2 ## EXAMPLE: returning a tuple
   3 #########################
   4 def quotient_and_remainder(x, y):
   5     q = x // y
   6     r = x % y
   7     return (q, r)
   8     
   9 (quot, rem) = quotient_and_remainder(5,3)
  10 print(quot)
  11 print(rem)
  12 
  13 
  14 #########################
  15 ## EXAMPLE: iterating over tuples
  16 #########################
  17 def get_data(aTuple):
  18     """
  19     aTuple, tuple of tuples (int, string)
  20     Extracts all integers from aTuple and sets 
  21     them as elements in a new tuple. 
  22     Extracts all unique strings from from aTuple 
  23     and sets them as elements in a new tuple.
  24     Returns a tuple of the minimum integer, the
  25     maximum integer, and the number of unique strings
  26     """
  27     nums = ()    # empty tuple
  28     words = ()
  29     for t in aTuple:
  30         # concatenating with a singleton tuple
  31         nums = nums + (t[0],)   
  32         # only add words haven't added before
  33         if t[1] not in words:   
  34             words = words + (t[1],)
  35     min_n = min(nums)
  36     max_n = max(nums)
  37     unique_words = len(words)
  38     return (min_n, max_n, unique_words)
  39 
  40 test = ((1,"a"),(2, "b"),
  41         (1,"a"),(7,"b"))
  42 (a, b, c) = get_data(test)
  43 print("a:",a,"b:",b,"c:",c)
  44 
  45 # apply to any data you want!
  46 tswift = ((2014,"Katy"),
  47           (2014, "Harry"),
  48           (2012,"Jake"), 
  49           (2010,"Taylor"), 
  50           (2008,"Joe"))    
  51 (min_year, max_year, num_people) = get_data(tswift)
  52 print("From", min_year, "to", max_year, \
  53         "Taylor Swift wrote songs about", num_people, "people!")
  54 
  55 #########################
  56 ## EXAMPLE: sum of elements in a list
  57 #########################
  58 def sum_elem_method1(L):
  59   total = 0 
  60   for i in range(len(L)): 
  61       total += L[i] 
  62   return total
  63   
  64 def sum_elem_method2(L):
  65     total = 0 
  66     for i in L: 
  67         total += i 
  68     return total
  69   
  70 print(sum_elem_method1([1,2,3,4]))
  71 print(sum_elem_method2([1,2,3,4]))
  72 
  73 
  74 #########################
  75 ## EXAMPLE: various list operations
  76 ## put print(L) at different locations to see how it gets mutated
  77 #########################
  78 L1 = [2,1,3]
  79 L2 = [4,5,6]
  80 L3 = L1 + L2
  81 L1.extend([0,6])
  82 
  83 L = [2,1,3,6,3,7,0]
  84 L.remove(2)
  85 L.remove(3)
  86 del(L[1])
  87 print(L.pop())
  88 
  89 s = "I<3 cs"
  90 print(list(s))
  91 print(s.split('<'))
  92 L = ['a', 'b', 'c']
  93 print(''.join(L))
  94 print('_'.join(L))
  95 
  96 L=[9,6,0,3]
  97 print(sorted(L))
  98 L.sort()
  99 L.reverse()
 100 
 101 
 102 #########################
 103 ## EXAMPLE: aliasing
 104 #########################
 105 a = 1
 106 b = a
 107 print(a)
 108 print(b)
 109 
 110 warm = ['red', 'yellow', 'orange']
 111 hot = warm
 112 hot.append('pink')
 113 print(hot)
 114 print(warm)
 115 
 116 #########################
 117 ## EXAMPLE: cloning
 118 #########################
 119 cool = ['blue', 'green', 'grey']
 120 chill = cool[:]
 121 chill.append('black')
 122 print(chill)
 123 print(cool)
 124 
 125 #########################
 126 ## EXAMPLE: sorting with/without mutation
 127 #########################
 128 warm = ['red', 'yellow', 'orange']
 129 sortedwarm = warm.sort()
 130 print(warm)
 131 print(sortedwarm)
 132 
 133 cool = ['grey', 'green', 'blue']
 134 sortedcool = sorted(cool)
 135 print(cool)
 136 print(sortedcool)
 137 
 138 #########################
 139 ## EXAMPLE: lists of lists of lists...
 140 #########################
 141 warm = ['yellow', 'orange']
 142 hot = ['red']
 143 brightcolors = [warm]
 144 brightcolors.append(hot)
 145 print(brightcolors)
 146 hot.append('pink')
 147 print(hot)
 148 print(brightcolors)
 149 
 150 
 151 ###############################
 152 ## EXAMPLE: mutating a list while iterating over it
 153 ###############################
 154 def remove_dups(L1, L2):
 155     for e in L1:
 156         if e in L2:
 157             L1.remove(e)
 158       
 159 def remove_dups_new(L1, L2):
 160     L1_copy = L1[:]
 161     for e in L1_copy:
 162         if e in L2:
 163             L1.remove(e)
 164 
 165 L1 = [1, 2, 3, 4]
 166 L2 = [1, 2, 5, 6]
 167 remove_dups(L1, L2)
 168 print(L1, L2)
 169 
 170 L1 = [1, 2, 3, 4]
 171 L2 = [1, 2, 5, 6]
 172 remove_dups_new(L1, L2)
 173 print(L1, L2)
 174 
 175 ###############################
 176 ## EXERCISE: Test yourself by predicting what the output is and 
 177 ##           what gets mutated then check with the Python Tutor
 178 ###############################
 179 cool = ['blue', 'green']
 180 warm = ['red', 'yellow', 'orange']
 181 print(cool)
 182 print(warm)
 183 
 184 colors1 = [cool]
 185 print(colors1)
 186 colors1.append(warm)
 187 print('colors1 = ', colors1)
 188 
 189 colors2 = [['blue', 'green'],
 190           ['red', 'yellow', 'orange']]
 191 print('colors2 =', colors2)
 192 
 193 warm.remove('red') 
 194 print('colors1 = ', colors1)
 195 print('colors2 =', colors2)
 196 
 197 for e in colors1:
 198     print('e =', e)
 199 
 200 for e in colors1:
 201     if type(e) == list:
 202         for e1 in e:
 203             print(e1)
 204     else:
 205         print(e)
 206 
 207 flat = cool + warm
 208 print('flat =', flat)
 209 
 210 print(flat.sort())
 211 print('flat =', flat)
 212 
 213 new_flat = sorted(flat, reverse = True)
 214 print('flat =', flat)
 215 print('new_flat =', new_flat)
 216 
 217 cool[1] = 'black'
 218 print(cool)
 219 print(colors1)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2017-07-27 19:18:45, 142.4 KB) [[attachment:Acetaminophen_PBPK.cps]]
  • [get | view] (2017-07-31 17:20:41, 1449.3 KB) [[attachment:Andy RK.zip]]
  • [get | view] (2017-07-27 19:18:53, 59.4 KB) [[attachment:BIOMD0000000619.xml]]
  • [get | view] (2017-08-09 16:50:30, 13731.9 KB) [[attachment:Belmonte_thesis.pdf]]
  • [get | view] (2017-07-27 19:33:15, 98.1 KB) [[attachment:Bloomington_and_WoodburnHall.pdf]]
  • [get | view] (2017-08-02 19:58:08, 69593.2 KB) [[attachment:CC3D Intro.pptx]]
  • [get | view] (2017-07-31 16:33:05, 1585.0 KB) [[attachment:CC3D-Computational-TB-Ruggiero.pptx]]
  • [get | view] (2017-07-31 13:39:48, 110507.5 KB) [[attachment:CC3D-Lecture Morning 1 August 2017v4-1.pptx]]
  • [get | view] (2017-07-27 15:39:09, 91.8 KB) [[attachment:CC3D_Quick_Reference_Guide.pdf]]
  • [get | view] (2017-07-27 16:26:46, 43.9 KB) [[attachment:CC3D_card.png]]
  • [get | view] (2017-08-04 21:45:14, 189.5 KB) [[attachment:CancerFLowchart.bmp]]
  • [get | view] (2017-08-04 21:38:26, 180.5 KB) [[attachment:CancerFlowCgart.pdf]]
  • [get | view] (2017-08-04 17:24:52, 5066.1 KB) [[attachment:CellCompartmentsAndLinks.pptx]]
  • [get | view] (2017-08-01 16:43:08, 27.8 KB) [[attachment:Chaotic Lorenz in COPASI.cps]]
  • [get | view] (2017-07-27 19:19:01, 1.1 KB) [[attachment:Critchley_human_APAP_data.csv]]
  • [get | view] (2017-08-02 14:44:17, 550.8 KB) [[attachment:Defining ODEs in COPASI--SBML.pptx]]
  • [get | view] (2017-07-27 19:33:23, 461.2 KB) [[attachment:Detailed_IU_map.png]]
  • [get | view] (2017-08-04 17:40:32, 7.2 KB) [[attachment:DirectedMovement.zip]]
  • [get | view] (2017-08-06 15:35:34, 6.4 KB) [[attachment:EpithelialSheet.zip]]
  • [get | view] (2017-08-04 20:15:18, 3.4 KB) [[attachment:HeikosModel (2).zip]]
  • [get | view] (2017-08-02 16:55:16, 1753.5 KB) [[attachment:Hirashima_et_al-2017-Development_Growt.pdf]]
  • [get | view] (2017-08-04 18:09:11, 132.3 KB) [[attachment:Josua-s Model Development Workflow Gene Expression Mammalian Blastocyst.pdf]]
  • [get | view] (2017-07-30 14:29:04, 29702.4 KB) [[attachment:Monday lectures Python ppt and code.zip]]
  • [get | view] (2017-08-02 19:58:27, 58286.7 KB) [[attachment:MultiScaleModelingWithCC3D-SBML.pptx]]
  • [get | view] (2017-08-01 18:49:13, 7433.6 KB) [[attachment:PBPK_SBML_modeling.pptx]]
  • [get | view] (2017-07-31 21:35:05, 38.2 KB) [[attachment:PK_simple_one_compartment.cps]]
  • [get | view] (2017-07-30 19:38:10, 738.3 KB) [[attachment:Priyom Verbal Model from Mark Alber Epithelial paper with annotations.docx]]
  • [get | view] (2017-07-27 15:34:10, 113.2 KB) [[attachment:PythonRefCard_1p.pdf]]
  • [get | view] (2017-07-27 15:34:51, 1634.3 KB) [[attachment:PythonRefCard_8p.pdf]]
  • [get | view] (2017-07-31 17:20:59, 19455.7 KB) [[attachment:Slides_ReactionKinetics-2.pptx]]
  • [get | view] (2017-07-30 17:31:42, 97.3 KB) [[attachment:WP_20170730_13_29_10_small.jpg]]
  • [get | view] (2017-07-31 18:01:31, 118.2 KB) [[attachment:WP_20170731_13_10_15_Pro.jpg]]
  • [get | view] (2017-07-27 16:27:11, 36.5 KB) [[attachment:beginners_python_cheat_sheet_pcc_SELECTED.png]]
  • [get | view] (2017-08-04 17:40:39, 13.1 KB) [[attachment:cellcycle_DN.zip]]
  • [get | view] (2017-08-04 17:47:09, 92.6 KB) [[attachment:class_photo.jpg]]
  • [get | view] (2017-08-02 19:59:01, 34.5 KB) [[attachment:delta-notch.cps]]
  • [get | view] (2017-08-02 20:05:06, 987.6 KB) [[attachment:delta-notch.pptx]]
  • [get | view] (2017-08-04 17:20:05, 5.8 KB) [[attachment:gradient_1.zip]]
  • [get | view] (2017-08-04 17:20:14, 6.9 KB) [[attachment:gradient_2.zip]]
  • [get | view] (2017-08-04 17:20:24, 6.6 KB) [[attachment:gradient_3.zip]]
  • [get | view] (2017-07-28 21:48:18, 3377.8 KB) [[attachment:holmes_et_al.gene_exp_enh_blas.pdf]]
  • [get | view] (2017-07-28 21:48:28, 2700.3 KB) [[attachment:holmes_sup_material.pdf]]
  • [get | view] (2017-07-31 13:25:51, 18008.4 KB) [[attachment:journal.pcbi.1005533.pdf]]
  • [get | view] (2017-07-30 14:57:48, 921.2 KB) [[attachment:lec1-intro.pptx]]
  • [get | view] (2017-07-30 14:57:40, 1.2 KB) [[attachment:lec1.py]]
  • [get | view] (2017-07-30 14:58:39, 76.8 KB) [[attachment:lec2-branching-iteration.pptx]]
  • [get | view] (2017-07-30 14:57:55, 2.8 KB) [[attachment:lec2.py]]
  • [get | view] (2017-07-30 14:58:57, 61.3 KB) [[attachment:lec3-strings-approximations.pptx]]
  • [get | view] (2017-07-30 14:58:46, 2.9 KB) [[attachment:lec3.py]]
  • [get | view] (2017-07-30 14:59:04, 1342.3 KB) [[attachment:lec4-basic-functions.pptx]]
  • [get | view] (2017-07-30 14:59:23, 2097.2 KB) [[attachment:lec5-tuple-lists.pdf]]
  • [get | view] (2017-07-30 14:59:15, 4.5 KB) [[attachment:lec5_tuples_lists.py]]
  • [get | view] (2017-07-30 14:59:47, 287.6 KB) [[attachment:lec6-dictionaries.pptx]]
  • [get | view] (2017-07-30 14:59:28, 4.6 KB) [[attachment:lec6.py]]
  • [get | view] (2017-07-30 14:59:39, 4.6 KB) [[attachment:lec6_recursion_dictionaries.py]]
  • [get | view] (2017-07-30 14:59:58, 20274.9 KB) [[attachment:lec7-random-walk.pptx]]
  • [get | view] (2017-08-09 16:50:37, 584.2 KB) [[attachment:parameter-scan.pdf]]
  • [get | view] (2017-07-27 16:26:57, 31.3 KB) [[attachment:python_refcard.png]]
  • [get | view] (2017-08-01 18:56:32, 3390.1 KB) [[attachment:s13628-015-0022-x.pdf]]
  • [get | view] (2017-07-28 18:54:30, 22.2 KB) [[attachment:schedule.docx]]
  • [get | view] (2017-07-28 18:54:20, 301.5 KB) [[attachment:schedule.pdf]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.