Contents

Lec12-Container

Container

box and pointers

见61B 😋

slice

传统python中,slice操作符的语法为[start:stop:step],其中startstop是切片的起止位置,step是切片的步长(左闭右开)

切片生成新的序列,对原序列的修改不会影响切片(物化view的感觉)

Process value in container

aggregate functions

  • sum(container):返回容器中所有元素的和
1
2
3
sum([1, 2, 3, 4, 5])  # 15
sum([1, 2], 9) # 12
sum([[2, 3], [4]], []) # [2, 3, 4]
  • max(container):返回容器中最大的元素
1
max(range(10), key=lambda x: 7-(x-4)*(x-2)) # 3
  • all(container):如果容器中所有元素都为真,则返回True,否则返回False
1
2
3
all([True, True, True]) # True
all([True, False, True]) # False
all([x < 5 for x in range(5)])

Strings

/lec12-container/image.png 注意事实上程序只是字符串 😋 和61B的text写java一个道理

/lec12-container/image-1.png

1
exec('some_code')

/lec12-container/image-2.png

Dict

💢 我一直觉得这个和 Map<K, V> 以及json的关系不清不楚

1
2
3
4
num = {'I': 1, 'V': 5, 'X': 10}
list(num) # ['I', 'V', 'X']
num.values() # dict_values([1, 5, 10])
empty = {}

/lec12-container/image-3.png

dict comprehension

1
{x: x**2 for x in range(5)} # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

/lec12-container/image-4.png