Sequence
I’m learning the CS_61A course since January 2024, but there was a long time I suspended the learning of this course to learn some other subjects like Algorithm and Data Structure, and pwn in CTF.
And now it’s time to continue the learning.
Sequences are a list of Data in a specific order such as queues, arrays and linked lists.
List
We should know that Python does not have a built-in array type (like C arrays) that’s available without importing a module. But there is a list type in Python similar to array in C.
Using of Lists
Use the index to visit the elements:
1
2
3
4
5
6
7odds = [41, 43, 47, 49]
odds[0]
43
odds[3]
42
odds[odds[3] - odds[2]]
47Get the number of elements:
1
2len(odds)
4Use the getitem function:
1
2
3from operator import getitem
getitem(odds, 3)
49Concatenation and repetition:
1
2
3
4
5
6
7[2, 7] + odds
[2, 7, 41, 43, 47, 49]
2 * odds
[41, 43, 47, 49, 41, 43, 47, 49]
from operator import add, mul
>> add([2, 7], mul(odds, 2))
[2, 7, 41, 43, 47, 49, 41, 43, 47, 49]nested lists
1
2
3
4
5pairs = [[10, 20], [30, 40]]
pairs[1]
[30, 40]
pairs[1][0]
30
Containers
In Python, containers are objects that hold other objects, providing a way to organize and manage collections of data.
Some typical containers:
- list
- tuple
- str
- dict
- set
But here what we want to say about is not Container itself, but some using of containers.
1 | digits = [1, 8, 2, 8] |
The emphasis is in, an operator between containers and elements. If the element is “in” the container, then this operation returns True, otherwise it’ll return False.
For Statements – the Sequence Iteration
In Python the For statement is a way of iterating over sequences.
Instance
1 | def count(s, value): |
1 | python3 -m doctest -v ex.py |
Principle
- How does
for statementworks?- Evaluate the header
expressionwhich must yield an iterable value (a sequence) - For each element in that sequence, in order:
- a. Bind
nameto that element in the current frame - b. Execute the
suit
- Evaluate the header
Extensional Instance
When we process with a sequence of fixed-length sequences, there could be several names of the fixed-length sequences.
- Unpack means reading the containers as several elements in it.
1 | pairs = [[1, 2], [2, 2], [2, 3], [3, 3]] |
1 | python ~/Downloads/test.py |
- Notice: the number of name in
for statementcould only be one or equals to the length of the fixed-length sequences.
Range
A range is a sequence of consecutive integers.*
..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5,...
- the length of range:
ending value-starting value
1 | range(-2, 2) |
Like what you see above, list is a built-in function of Python that turns a sequence into the form of list. So it could turns the range into a list and print it out.
It’s easy to find that:
- a range include it’s first element, but exclude it’s last element.
- if there is only one param for range, the range start from 0, and it’s length would be the param.
Using
With Range types, we could do like this:
1 | def sum_below(n): |
1 | python -i ex.py |
And this:
1 | def cheer(): |
1 | python -i ex.py |
List Comprehension
A List Comprehension, from my perspective, is just an special expression values to a list.
In Chinese, List Comprehension is usually translated to:
- 列表推导式(最常用)
- 列表解析式
- 列表生成式
The Grammar of List Comprehension
[expression for item in iterable if condition]
expression: just an expression of item.for item in iterable: Iterates over the iterable.if condition(optional): Filters items base on a condition.
Example
1 | [x+1 for x in [1, 2, 3]] |
Using
With List Comprehension, now we could do like this:
1 | def divisors(n): |
1 | python -i ex.py |
Lists, Slices & Recursion
For any list s, the expression s[1:] is called a slice from index 1 to the end (ro 1 onward).
- The value of
s[1:]is a list whose length is one less thant the length of s - Slicing s doesn’t affect s
Recursion Example
1 | # Compute the sum of elements in a list. |
Here is a more complex example, it’s a function which takes a list of positive numbers s and a non-negative number n. It returns the sublist of s with the largest sum that is less than or equal to n.
1 | def large(s, n): |
