|
| 1 | +print('chapter 8 List') |
| 2 | +# Algorithm: "A set of rules or steps used to solve a problems" |
| 3 | +# Data Structure: "A particular way of organizing data in a computers" |
| 4 | +print('1. A list is a kind of collection ') |
| 5 | +# e.g. friends = ['Joe', 'Glenn', 'Sally'] |
| 6 | +# []: list constent, and seperate by commas "," |
| 7 | +# A list element can be any python object, even another list |
| 8 | +# list can be empty |
| 9 | +print('List contants example') |
| 10 | +print([1, 24, 9]) #one type "int" in the list |
| 11 | +print([12, 'red', [1, 5]]) # mixed three types |
| 12 | +print('#####################################################') |
| 13 | +print('2. List and definaite loops') |
| 14 | +friends = ['Joe', 'Glenn', 'Sally'] |
| 15 | +for friend in friends: |
| 16 | + print('Hello: ', friend) |
| 17 | +print('Done!') |
| 18 | +print('#####################################################') |
| 19 | +print('3. Looking inside lists') |
| 20 | +# we can get any single element in a list by using index: start from 0 |
| 21 | +friends = ['Joe', 'Glenn', 'Sally'] |
| 22 | +print(friends[2]) |
| 23 | +print('#####################################################') |
| 24 | +print('4. lists are mutable: can be change') |
| 25 | +# strings are "immutable" <-- we cannot change the content of a string, |
| 26 | +# we must makea new string to make any changes |
| 27 | +fruit = 'Banana' |
| 28 | +print(fruit) |
| 29 | +x = fruit.lower() # <--it makes a copy of the string |
| 30 | +print(x) |
| 31 | +# Lists are "mutable" <-- we can change a element of a list by its index |
| 32 | +lotto = [12, 26, 41, 28] |
| 33 | +print(lotto) |
| 34 | +lotto[2] = 28 |
| 35 | +print(lotto) |
| 36 | +print('#####################################################') |
| 37 | +print('5. The length of the list') |
| 38 | +# The "leng()" function takes a list as a parameter |
| 39 | +# and returns the numbers of elements in the list |
| 40 | +# !! len() tells the numbers of elements of any set or sequence |
| 41 | +greet ='hello Bob' |
| 42 | +print(len(greet)) |
| 43 | + |
| 44 | +x = [1, 2, 'Joe', [4, 6]] |
| 45 | +print(len(x)) |
| 46 | +print('#####################################################') |
| 47 | +print('6. Using the "Range" Function') |
| 48 | +# The range function returns a list of numbers that range |
| 49 | +# from zero to one less than the parameter: 0 to (parameter-1) |
| 50 | +# we can construct an index loop using "for and an integer iterator" |
| 51 | +num4 = range(4) |
| 52 | +for i in num4: |
| 53 | + print(i, end=', ') |
| 54 | +print('Use rang in a loop') |
| 55 | +friends = ['Joe', 'Glenn', 'Sally'] |
| 56 | +print(len(friends)) |
| 57 | +for i in range(len(friends)): |
| 58 | + friend = friends[i] |
| 59 | + print('Hello: ', friend) # <-- within for loop! |
| 60 | +print('####################part2############################') |
| 61 | +print('7. Concatenating lists using "+"') |
| 62 | +a = [1, 2, 3] |
| 63 | +b = [4, 5, 6] |
| 64 | +c = a + b |
| 65 | +print(a, '\n', b, '\n', c, sep='') |
| 66 | +#sep -- 用来间隔多个对象,默认值是一个空格。 |
| 67 | +print('####################part2############################') |
| 68 | +print('8. Slicing lists using ":"') |
| 69 | +print('Reminder: the second number is "up to but not inclusing"') |
| 70 | +t = [9, 41, 12, 3 ,74, 15] |
| 71 | +print(t) |
| 72 | +print(t[1:3]) |
| 73 | +print(t[:4]) |
| 74 | +print(t[3:]) |
| 75 | +print('####################part2############################') |
| 76 | +print('9. List Methods') |
| 77 | +# append: adds a new element to the end of a list |
| 78 | +t = ['a', 'b', 'c', 'd', 'e', 'f'] |
| 79 | +t[1:3] = ['x', 'y'] |
| 80 | +print(t) |
| 81 | +# ['a', 'x', 'y', 'd', 'e', 'f'] |
| 82 | +stuff= list() |
| 83 | +stuff.append('book') |
| 84 | +stuff.append(99) |
| 85 | +print(stuff) |
| 86 | +stuff.append([3, 4]) |
| 87 | +print(stuff) |
| 88 | + |
| 89 | +# extend: takes a list as an argument and appends all of the elements |
| 90 | +# >>> t1 = ['a', 'b', 'c'] |
| 91 | +# >>> t2 = ['d', 'e'] |
| 92 | +# >>> t1.extend(t2) |
| 93 | +# >>> print(t1) |
| 94 | +# ['a', 'b', 'c', 'd', 'e'] |
| 95 | +# sort :arranges the elements of the list from low to high: |
| 96 | +# >>> t = ['d', 'c', 'e', 'b', 'a'] |
| 97 | +# >>> t.sort() |
| 98 | +# >>> print(t) |
| 99 | +# ['a', 'b', 'c', 'd', 'e'] |
| 100 | +# pop: delete element by its index, modifies the list and returns the element that was removed |
| 101 | +# >>> t = ['a', 'b', 'c'] |
| 102 | +# >>> x = t.pop(1) |
| 103 | +# >>> print(t) |
| 104 | +# ['a', 'c'] |
| 105 | +# >>> print(x) |
| 106 | +# b |
| 107 | +# del: delete element by its index |
| 108 | +# >>> t = ['a', 'b', 'c'] |
| 109 | +# >>> del t[1] |
| 110 | +# >>> print(t) |
| 111 | +# ['a', 'c'] |
| 112 | +# remove: delete by name |
| 113 | +# >>> t = ['a', 'b', 'c'] |
| 114 | +# >>> t.remove('b') |
| 115 | +# >>> print(t) |
| 116 | +# ['a', 'c'] |
| 117 | +print('####################part2############################') |
| 118 | +print('10. Is something in the list?') |
| 119 | +# Pythonprovies two operators that check if an item is in a list |
| 120 | +# "in" or "not in" & "True" or "false" |
| 121 | +some = [1, 9, 21, 10, 16] |
| 122 | +print('9 in some: ') |
| 123 | +print(9 in some) |
| 124 | +print('15 in some: ') |
| 125 | +print(15 in some) |
| 126 | +print('sort list:') |
| 127 | +friends = ['Joe', 'Glenn', 'Sally'] |
| 128 | +print(friends) |
| 129 | +friends. sort() |
| 130 | +print(friends) |
| 131 | +# cannot use print(friends.sort())!!!! output: None |
| 132 | +print('simpler than loop') |
| 133 | +some = [1, 9, 21, 10, 16] |
| 134 | +print(len(some)) |
| 135 | +print(max(some)) |
| 136 | +# no use of some.max()!!!!!! |
| 137 | +print(min(some)) |
| 138 | +print(sum(some)) |
| 139 | +print('compare loop and list:') |
| 140 | +# ##########loop############# |
| 141 | +total = 0 |
| 142 | +count = 0 |
| 143 | +while True: |
| 144 | + inp = input('Enter a number:') |
| 145 | + if inp =='none': break |
| 146 | + val = float(inp) |
| 147 | + total = total + val |
| 148 | + count = count + 1 |
| 149 | +print('Average: ', total/count) |
| 150 | +# ##########list############# |
| 151 | +numlist = list() |
| 152 | +while True: |
| 153 | + inp = input('Enter a number:') |
| 154 | + if inp =='none': break |
| 155 | + val = float(inp) |
| 156 | + numlist.append(val) |
| 157 | +print('Ave: ', sum(numlist)/len(numlist)) |
| 158 | +# Difference: loop will keep memory of one number, |
| 159 | +# while list keep all numbers in memory before calculation |
| 160 | +print('####################part3############################') |
| 161 | +print('11. String and list') |
| 162 | +print('imortant example:') |
| 163 | +abc = 'With three words' |
| 164 | +stuff = abc.split() |
| 165 | +print(stuff) |
| 166 | +print(len(stuff)) |
| 167 | +for i in stuff: |
| 168 | + print(i) |
| 169 | +print('delimiter: specifies which characters to use as word boundaries' |
| 170 | + 'with no specify of delimiter, multiple spaces treated as one delimiter') |
| 171 | +line1 = 'A lot of spaces' |
| 172 | +etc1= line1.split() |
| 173 | +print(etc1) |
| 174 | +line2 = '1;2;3' |
| 175 | +etc2 = line2.split(';') |
| 176 | +print(etc2) |
| 177 | +print(len(etc2)) |
| 178 | +print('#####################################################') |
| 179 | +print('12. parsing lines') |
| 180 | +fhand = open('mbox_short.txt') |
| 181 | +for line in fhand: |
| 182 | + line = line.rstrip() |
| 183 | + if not line.startswith('From '): continue |
| 184 | + words = line.split() |
| 185 | + print(words[2]) |
| 186 | +print('#####################################################') |
| 187 | +print('13. double split pattern') |
| 188 | + |
| 189 | +fhand = open('mbox_short.txt') |
| 190 | +for line in fhand: |
| 191 | + line = line.rstrip() |
| 192 | + if not line.startswith('From '): continue |
| 193 | + words = line.split() |
| 194 | + email = words[1] |
| 195 | + pieces = email.split('@') |
| 196 | + print(pieces) |
| 197 | + |
| 198 | + |
0 commit comments