list
Lists are Python data structures that can store multiple values.
list comprehensions
[<expression> for <element> in <sequence> if <conditional>]
The syntax is designed to read like English: “Compute the expression for each element in the sequence if the conditional is true.”
>>> [i**2 for i in [1, 2, 3, 4] if i % 2 == 0]
[4, 16]
It would be like this:
>>> lst = []
>>> for i in [1, 2, 3, 4]:
... if i % 2 == 0:
... lst += [i**2]
>>> lst
[4, 16]
Data Abstraction
An abstract data type consists of two types of functions:
Constructors: functions that build the abstract data type.
Selectors: functions that retrieve information from the data type
However, we does not need to know how constructors and selectors are implemented.
Questions
link indexs
What would Python display?
p1 = [x*x for x in range(5)]
print('p1=',p1)
p2 = [n for n in range(10) if n % 2 == 0]
print('p2=',p2)
ones = [1 for i in ["hi", "bye", "you"]]
print('ones=',ones)
p3 = ones + [str(i) for i in [6, 3, 8, 4]]
print('ones_again=',ones)
print('p3=', p3)
p4 = [i+5 for i in [n for n in range(1,4)]]
print('p4=',p4)
p5 = [i**2 for i in range(10) if i<3]
print("p5=",p5)
lst = ['hi' for i in [1,2,3]]
print(lst)
p6 = lst + [i for i in ['1', '2', '3']]
print('p6=',p6)
If This Not That
def if_this_not_that(i_list, this):
"""Define a function which takes a list of integers `i_list` and an integer
`this`. For each element in `i_list`, print the element if it is larger
than `this`; otherwise, print the word "that".
>>> original_list = [1, 2, 3, 4, 5]
>>> if_this_not_that(original_list, 3)
that
that
that
4
5
"""
for n in i_list:
if n <= this:
print("that")
else:
print(n)
from doctest import run_docstring_examples
run_docstring_examples(if_this_not_that, globals(), True)
lst = [1, 2, 3, 4, 5, 3, 6, 1, 2, 7, 9]
if_this_not_that(lst, 5)
City Data Abstraction
Q4 distance
from math import sqrt
def distance(city1, city2):
"""
>>> city1 = make_city('city1', 0, 1)
>>> city2 = make_city('city2', 0, 2)
>>> distance(city1, city2)
1.0
>>> city3 = make_city('city3', 6.5, 12)
>>> city4 = make_city('city4', 2.5, 15)
>>> distance(city3, city4)
5.0
"""
city1_lat = city1.get_lat(city1)
city1_lon = city1.get_lon(city1)
city2_lat = city2.get_lat(city2)
city2_lon = city.get_lon(city2)
return sqrt((city2_lat-city1_lat)**2+(city2_lon-city1_lon)**2)
Q5 Closer city
def closer_city(lat, lon, city1, city2):
"""
Returns the name of either city1 or city2, whichever is closest to
coordinate (lat, lon).
>>> berkeley = make_city('Berkeley', 37.87, 112.26)
>>> stanford = make_city('Stanford', 34.05, 118.25)
>>> closer_city(38.33, 121.44, berkeley, stanford)
'Stanford'
>>> bucharest = make_city('Bucharest', 44.43, 26.10)
>>> vienna = make_city('Vienna', 48.20, 16.37)
>>> closer_city(41.29, 174.78, bucharest, vienna)
'Bucharest'
"""
city3 = make_city('city3', lat,lon)
if distance(city1,city3)return "city1"
else:
return "city2"
Q7: Flatten
Write a function flatten that takes a (possibly deep) list and “flattens” it.
def flatten(lst):
"""Returns a flattened version of lst.
>>> flatten([1, 2, 3]) # normal list
[1, 2, 3]
>>> x = [1, [2, 3], 4] # deep list
>>> flatten(x)
[1, 2, 3, 4]
>>> x = [[1, [1, 1]], 1, [1, 1]] # deep list
>>> flatten(x)
[1, 1, 1, 1, 1, 1]
"""
"*** YOUR CODE HERE ***"
Reference
a = [1,[2,3],[1,[3,[5]]]]
def flatten(lst):
templst = list()
for x in lst:
if type(x) != list:
templst.append(x)
else:
lstterm = flatten(x)
templst.extend(lstterm)
return templst
b = flatten(a)
print(b)
Q8: Merge
Write a function merge that takes 2 sorted lists lst1 and lst2, and returns a new list that contains all the elements in the two lists in sorted order.
def merge(lst1, lst2):
"""Merges two sorted lists.
>>> merge([1, 3, 5], [2, 4, 6])
[1, 2, 3, 4, 5, 6]
>>> merge([], [2, 4, 6])
[2, 4, 6]
>>> merge([1, 2, 3], [])
[1, 2, 3]
>>> merge([5, 7], [2, 4, 6])
[2, 4, 5, 6, 7]
"""
"*** YOUR CODE HERE ***"
Reference
def merge(lst1, lst2):
"""Merges two sorted lists.
>>> merge([1, 3, 5], [2, 4, 6])
[1, 2, 3, 4, 5, 6]
>>> merge([], [2, 4, 6])
[2, 4, 6]
>>> merge([1, 2, 3], [])
[1, 2, 3]
>>> merge([5, 7], [2, 4, 6])
[2, 4, 5, 6, 7]
"""
lst3 = lst1 +lst2
lst3.sort()
return lst3
from doctest import run_docstring_examples
run_docstring_examples(merge, globals(), True)
Lab 4: Lists and Data Abstraction https://inst.eecs.berkeley.edu/~cs61a/sp18/lab/lab04/