Python[CS61A] Sequences: Videos 정리
Lists 영상
odds = [41, 43, 45, 47]
odds[0]
odds[3] #element selection expression, evaluate the index
len(odds) #call expression
odds[3] - odds[2]
#이걸 index로도 이용 가능
odds[odds[3] - odds[2]]
#list literaLs
digits = [1, 8, 2, 8]
#elements selection syntax
digits[3]
#getitem function (operator module)
getitem(digits, 3)
#Concatenation and Repetition
[2, 7] + digits * 2
add([2, 7], mul(digits, 2))
#nested lists
pairs = [[10, 20], [30, 40]]
pairs[1]
pairs[1][0]
Containers 영상
One value that contain others, will an element appear in a list?
Container: Built in operators for testing whether an element appears in a compound value.
digits = [1, 8, 2, 8]
# in operator
1 in digits # True
8 in digits # True
5 not in digits # True
'1' in digits # False
[1, 8] in digits # False
[1, 2] in [3, [1, 2], 4] # True
[1, 2] in [3, [[1, 2]], 4] # False
For Statements 영상
def count(s, value):
"""Count the number of times that value occurs
in sequence sequence s.
"""
total, index = 0, 0
while index < len(s):
element = s[index]
if element == value:
total += 1
index += 1
return total
count([1, 2, 1, 2, 1], 1)
# output: 3
for statement을 이용하면:
def count(s, value):
"""Count the number of times that value occurs
in sequence sequence s.
"""
total = 0
for element in s:
if element == value:
total += 1
return total
count([1, 2, 1, 2, 1], 1)
# output: 3
going to execute a suite of the statement a number of times, which is the number of elements in s.
element will be bound to a different element of s each time
For Statement Execution Procedure

pairs = [[1, 2], [2, 2], [3, 2], [4, 4]]
same_count = 0
for x, y in pairs:
if x == y:
same_count = same_count + 1
Ranges 영상
Range is a sequence of consecutive integers.
Length: Ending value - starting value
Element selection: starting value + index
List constructor: convert to a list when we want to see the elements
range(n)이면 n이 ending value로 취급된다.
range(-2, 2) # -2, -1, 0, 1
list(range(-2, 2)) # [-2, -1, 0, 1]
list(range(4)) # [0, 1, 2, 3]
def sum_below(n):
total = 0
for i in range(n):
total += i
return total
sum_below(5)
# output:10
def cheer():
for _ in range(3):
print("Go Bears!")
# range(3) means 3 elements in range
# underscore _는 argument name을 신경 쓰지 않는다는 것
List Comprehensions 영상
odds = [1, 3, 5, 7, 9]
[x+1 for x in odds]
# output: [2, 4, 6, 8, 10]
[x+1 for x in odds if 25 % x == 0]
# output: [2, 6]
def divisors(n):
return [1] + [x for x in range(2, n) if n % x == 0]
divisors(1)
# output: [1]
divisors(4)
# output: [1, 2]
divisors(8)
# output: [1, 2, 4]
Lists, Slices, & Recursion 영상
slice:
s[1:]
slice from index 1 to the end.
a list whose length is one less than the length of s
it contains all of the elements of s except s[0]
Slicing s doesn't affect s
s = [2, 3, 6, 4]
s[1:]
#[3, 6, 4]
s
# [2, 3, 6, 4]
Recursion example: sum
Implement sum_list, which takes a list of numbers s and returns their sum. If a list is empty, the sum of its elements is 0.
def sum_list(s):
"""
sum([2, 4, 1, 3])
10
"""
if len(s) == 0:
return 0
else:
return s[0] + sum_list(s[1:])
Recursion Example: Large Sums
def large(s, n):
"""Return the sublist of positive numbers s with the
largest sum that is less than or equal to n.
>>> large([4, 2, 5, 6, 7], 3)
[2]
>>> large([4, 2, 5, 6, 7], 8)
[2, 6]
>>> large([4, 2, 5, 6, 7], 19)
[4, 2, 6, 7]
>>> large([4, 2, 5, 6, 7], 20)
[2, 5, 6, 7]
"""
if s == []:
return []
elif s[0] > n:
return large(s[1:], n)
else:
first = s[0]
with_s0 = [first] + large(s[1:], n - first)
without_s0 = large(s[1:], n)
if sum_list(with_s0) > sum_list(without_s0):
return with_s0
else:
return without_s0