Mosh的课程网址
List
1 2 3 4 5 6 7 8
| letters = ["a", "b", "c"] matrix = [[0, 1], [2, 3]]
zeros = [0] * 5 combined = zeros + letters numbers = list(range(20)) chars = list("Hello World") print(len(chars))
|
Demo 1
1 2 3 4 5 6 7
| letters = ["a", "b", "c", "d"] print(letters[0]) print(letters[-1]) letters[0] = "A" print(letters) print(letters[:3]) print(letters[::2])
|
1 2 3 4 5
| a d ['A', 'b', 'c', 'd'] ['A', 'b', 'c'] ['A', 'c']
|
Demo 2
1 2 3 4
| numbers = list(range(10)) print(numbers) print(numbers[::2]) print(numbers[::-1])
|
输出结果:
1 2 3
| [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 2, 4, 6, 8] [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
|
Unpacking Lists
Demo 1
1 2 3
| numbers = [1, 2, 3] first, second, third = numbers first, second, _ = numbers
|
Demo 2
1 2 3 4
| numbers2 = [1, 2, 3, 4, 4, 4, 4, 4, 4] first, second, *other = numbers2 print(first) print(other)
|
与此同理:
1 2 3 4 5 6 7 8 9
| def multiply(*numbers): total = 1 for number in numbers: total *= number return total
print(multiply(2, 3, 4, 5)) print(multiply(4, 5, 6, 2, 5))
|
Another demo:
1 2 3 4
| numbers2 = [1, 2, 3, 4, 4, 4, 4, 4, 9] first, *other, last = numbers2 print(first, last) print(other)
|
输出:
1 2
| {'id': 1, 'name': 'John', 'age': 22} John
|
Looping over Lists
1 2 3
| letters = ["a", "b", "c"] for letter in enumerate(letters): print(letter)
|
输出结果:
1 2 3
| (0, 'a') (1, 'b') (2, 'c')
|
unpacking tuple:
1 2 3
| letters = ["a", "b", "c"] for index, letter in enumerate(letters): print(index, letter)
|
输出结果:
Adding/Removing Items
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| letters = ["a", "b", "c", "d", "e"]
letters.append("f") letters.insert(0, "-") print(letters)
letters.pop() print(letters)
letters.remove("b") print(letters)
del letters[0:3] print(letters)
letters.clear() print(letters)
|
输出结果:
1 2 3 4 5
| ['-', 'a', 'b', 'c', 'd', 'e', 'f'] ['-', 'a', 'b', 'c', 'd', 'e'] ['-', 'a', 'c', 'd', 'e'] ['d', 'e'] []
|
Finding Items
1 2 3 4
| letters = ["a", "b", "c"] print(letters.count("d")) if "c" in letters: print(letters.index("c"))
|
Sorting Lists
Demo 1
1 2 3
| numbers = [3, 51, 2, 8, 6] numbers.sort(reverse=True) print(numbers)
|
输出结果:
Demo 2
built-in function: 不改变原数组
1 2 3
| numbers = [3, 51, 2, 8, 6] print(sorted(numbers, reverse=True)) print(numbers)
|
输出结果:
1 2
| [51, 8, 6, 3, 2] [3, 51, 2, 8, 6]
|
Demo 3
1 2 3 4 5 6 7 8 9 10 11 12 13
| items = [ ("product1", 10), ("product2", 9), ("product3", 12), ]
def sort_item(item): return item[1]
items.sort(key=sort_item) print(items)
|
输出结果:
1
| [('product2', 9), ('product1', 10), ('product3', 12)]
|
Lambdas
改造上述的例子(Demo 3):
1 2 3 4 5 6 7 8 9
| items = [ ("product1", 10), ("product2", 9), ("product3", 12), ]
items.sort(key=lambda item: item[1]) print(items)
|
语法:
1
| lambda parameters:expression
|
Map Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| items = [ ("product1", 10), ("product2", 9), ("product3", 12), ]
prices = list(map(lambda item: item[1], items)) print(prices)
|
输出:
Filter Function
1 2 3 4 5 6 7 8
| items = [ ("product1", 10), ("product2", 9), ("product3", 12), ]
filtered = list(filter(lambda item: item[1] >= 10, items)) print(filtered)
|
输出结果:
1
| [('product1', 10), ('product3', 12)]
|
List Comprehension
1
| [expression for item in items]
|
改造上面的 map function 和 filter function:
1 2 3
| prices = [item[1] for item in items]
filtered = [item for item in items if item[1] >= 10]
|
Zip Function
1 2 3 4
| list1 = [1, 2, 3] list2 = [10, 20, 30]
print(list(zip("abc", list1, list2)))
|
输出结果:
1
| [('a', 1, 10), ('b', 2, 20), ('c', 3, 30)]
|
Stacks
LIFO: Last In - First Out
1 2 3 4 5 6
| browsing_session = [] browsing_session.append(1) browsing_session.append(2)
if not not browsing_session: print(browsing_session.pop())
|
Queues
FIFO: First In - First Out
1 2 3 4 5 6 7 8 9 10 11
| from collections import deque
queue = deque([]) queue.append(1) queue.append(2) queue.append(3) queue.popleft() print(queue)
if not queue: print("Empty")
|
Tuples
1 2 3 4 5
| point = 1, 2
print(type(point))
|
Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| point = (1, 2) + (3, 4) print(point)
point = (1, 2) * 3 print(point)
point = tuple([1, 2]) print(point)
point = tuple("hello") print(point)
point = (1, 2, 3) print(point[0:2])
x, y, z = point if 10 in point: print("exists")
|
Swapping Variables
1 2 3 4 5 6 7
| x = 10 y = 11
z = x x = y y = z print(x, y)
|
simpler way:
实际上是先定义了一个tuple,然后给x, y赋值,即 x, y = (11, 10)
Arrays
1 2 3 4 5 6 7 8
| from array import array
numbers = array("i", [1, 2, 3])
list1 = [1.0, 1, 2, 3] print(type(list1[0])) print(type(list1[1]))
|
arrays 可以调用 append, pop, index 等与 lists 相同的 built-in function,但是只能有一种数据类型(由参数 typecode 如 ”i“ 指定)
而一个 list 里面可以有多种数据类型:
1 2 3
| list1 = [1.0, 1, 2, 3] list1.append("hello") print(list1)
|
Sets
一个 set 内不能有重复项
1 2 3
| numbers = [1, 1, 2, 3, 4] first = set(numbers) print(first)
|
1 2 3 4 5 6 7
| second = {1, 4} second.add(5) print(second)
second.remove(5) len(second) print(second)
|
Demo
1 2 3 4 5 6 7 8 9
| numbers = [1, 1, 2, 3, 4] first = set(numbers)
second = {1, 5}
print(first | second) print(first & second) print(first - second) print(first ^ second)
|
输出结果:
1 2 3 4
| {1, 2, 3, 4, 5} {1} {2, 3, 4} {2, 3, 4, 5}
|
set是无序的,不能用index,如first[0]是不合法的
1 2
| if 1 in first: print("yes")
|
Dictionary
创建
1 2 3
| point = {"x": 1, "y": 2} point = dict(x=1, y=2)
|
增改
1 2 3 4
| point["x"] = 10 print(point) point["z"] = 20 print(point)
|
获取
1 2
| print(point.get("a")) print(point.get("a", 0))
|
删除
1 2
| del point["x"] print(point)
|
for loop
1 2 3 4 5 6 7 8
| for key in point: print(key, point[key])
for item in point.items(): print(item)
for key, value in point.items(): print(key, value)
|
返回:
1 2 3 4 5 6
| y 2 z 20 ('y', 2) ('z', 20) y 2 z 20
|
Dictionary Comprehensions
1 2 3 4 5
|
values = [x * 2 for x in range(5)]
|
set、dictionary 的:
1 2 3 4 5
| values = {x * 2 for x in range(5)} print(values)
values = {x: x * 2 for x in range(5)} print(values)
|
Generators
用于有大量(无限)元素,要节省内存空间的情形
1 2 3 4 5 6 7
| from sys import getsizeof
values = (x * 2 for x in range(100000)) print("gen:", getsizeof(values))
values = [x * 2 for x in range(100000)] print("list:", getsizeof(values))
|
输出:
对于 generator object, 无论有多少元素,都只占112的位置(就算只有一个元素也是)。generator object 在迭代时才生成元素,而不是将所有的元素都存在内存中。
Unpacking Operator
Demo 1
1 2 3
| numbers = [1, 2, 3] print(numbers) print(*numbers)
|
numbers 是一个list, 而 *numbers 是 unpack 之后的3个独立的数
Demo 2
1 2 3
| values = list(range(5)) values = [*range(5), *"Hello"] print(values)
|
Demo 3
组合两个lists:
1 2 3 4
| first = [1, 2] second = [3] values = [*first, "a", *second, *"Hello"] print(values)
|
Demo 4
组合两个字典:
1 2 3 4
| first = {"x": 1} second = {"x": 10, "y": 2} combined = {**first, **second, "z": 1} print(combined)
|
相同 key 值的元素 (“x”) 取最后一个元素的 value (10)
Exercise
得出一个句子中出现频率最高的字母:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| from pprint import pprint
sentence = "This is a common interview question"
char_frequency = {} for char in sentence: if char in char_frequency: char_frequency[char] += 1 else: char_frequency[char] = 1 pprint(char_frequency, width=1)
char_frequency_sorted = sorted( char_frequency.items(), key=lambda kv: kv[1], reverse=True)
print(char_frequency_sorted[0])
|
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| {' ': 5, 'T': 1, 'a': 1, 'c': 1, 'e': 3, 'h': 1, 'i': 5, 'm': 2, 'n': 3, 'o': 3, 'q': 1, 'r': 1, 's': 3, 't': 2, 'u': 1, 'v': 1, 'w': 1} ('i', 5)
|
我的改进:
1 2 3 4 5 6 7 8 9 10 11 12
|
char_frequency = {} for char in sentence: char_frequency[char] = char_frequency.get(char, 0) + 1 pprint(char_frequency, width=1)
|