A list is a sequential collection of Python data values, where each value is identified by an index.
The values that make up a list are called its elements.
Lists are similar to strings, which are ordered collections of characters, except that the elements of a list can have any type and for any one list, the items can be of different types.
[10, 20, 30, 40]
["spam", "bungee", "swallow"]
nested list, sub list
["hello", 2.0, 5, [10, 20]]
empty list
[]
vocabulary = ["iteration", "selection", "control"]
numbers = [17, 123]
empty = []
mixedlist = ["hello", 2.0, 5*2, [10, 20]]
print(numbers)
print(mixedlist)
newlist = [ numbers, vocabulary ]
print(newlist)
Exercise
A list can contain only integer items.
(A) False
(B) True
function len
returns the length of a list
alist = ["hello", 2.0, 5, [10, 20]]
print(len(alist))
print(len(['spam!', 1, ['Brie', 'Roquefort',
'Pol le Veq'], [1, 2, 3]]))
Exercise
1. What is printed by the following statements?
alist = [3, 67, "cat", 3.14, False]
print(len(alist))
(A) 4
(B) 5
2. What is printed by the following statements?
alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
print(len(alist))
(A) 7
(B) 8
numbers = [17, 123, 87, 34, 66, 8398, 44]
print(numbers[2])
print(numbers[9 - 8])
print(numbers[-2])
print(numbers[len(numbers) - 1])
Exercise
1. What is printed by the following statements?
alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
print(alist[5])
(A) [ ]
(B) 3.14
(C) False
2. What is printed by the following statements?
alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
print(alist[2].upper())
(A) Error, you cannot use the upper method on a list.
(B) 2
(C) CAT
in and not in are boolean operators that test membership in a sequence.
fruit = ["apple", "orange", "banana", "cherry"]
print("apple" in fruit)
print("pear" in fruit)
print("pear" not in fruit)
Exercise
1. What is printed by the following statements?
alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
print(3.14 in alist)
(A) True
(B) False
As with strings,
+
operator: concatenates lists.
*
operator: repeats the items in a list a given number of times
fruit = ["apple", "orange", "banana", "cherry"]
print([1, 2] + [3, 4])
print(fruit + [6, 7, 8, 9])
print([0] * 4)
print([1, 2, ["hello", "goodbye"]] * 2)
from IPython.display import IFrame
IFrame("http://pythontutor.com/iframe-embed.html#code=fruit%20%3D%20%5B%22apple%22,%20%22orange%22,%20%22banana%22,%20%22cherry%22%5D%0Anumlist%20%3D%20%5B6,%207%5D%0A%0Anewlist%20%3D%20fruit%20%2B%20numlist%0A%0Azeros%20%3D%20%5B0%5D%20*%204&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=false&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false", width='100%', height=450)
Exercise
1. What is printed by the following statements?
alist = [1, 3, 5]
blist = [2, 4, 6]
print(alist + blist)
(A) 6
(B) [1, 2, 3, 4, 5, 6]
(C) [1, 3, 5, 2, 4, 6]
(D) [3, 7, 11]
2. What is printed by the following statements?
alist = [1, 3, 5]
print(alist * 3)
(A) 9
(B) [1, 1, 1, 3, 3, 3, 5, 5, 5]
(C) [1, 3, 5, 1, 3, 5, 1, 3, 5]
(D) [3, 9, 15]
a_list = ['a', 'b', 'c', 'd', 'e', 'f']
print(a_list[1:3])
print(a_list[:4])
print(a_list[3:])
print(a_list[:])
Exercise
What is printed by the following statements?
alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
print(alist[4:])
(A) [ [ ], 3.14, False]
(B) [ [ ], 3.14]
(C) [ [56, 57, "dog"], [ ], 3.14, False]
fruit = ["banana", "apple", "cherry"]
print(fruit)
fruit[0] = "pear"
fruit[-1] = "orange"
print(fruit)
from IPython.display import IFrame
IFrame("http://pythontutor.com/iframe-embed.html#code=fruit%20%3D%20%5B%22banana%22,%20%22apple%22,%20%22cherry%22%5D%0A%0Afruit%5B0%5D%20%3D%20%22pear%22%0Afruit%5B-1%5D%20%3D%20%22orange%22&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=false&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false", width='100%', height=450)
alist = ['a', 'b', 'c', 'd', 'e', 'f']
alist[1:3] = ['x', 'y']
print(alist)
alist = ['a', 'b', 'c', 'd', 'e', 'f']
alist[1:3] = []
print(alist)
alist = ['a', 'd', 'f']
alist[1:1] = ['b', 'c']
print(alist)
alist[4:4] = ['e']
print(alist)
Exercise
What is printed by the following statements?
alist = [4, 2, 8, 6, 5]
alist[2] = True
print(alist)
(A) [4, 2, True, 8, 6, 5]
(B) [4, 2, True, 6, 5]
(C) Error, it is illegal to assign
The del
statement removes an element from a list by using its position.
a = ['one', 'two', 'three']
del a[1]
print(a)
alist = ['a', 'b', 'c', 'd', 'e', 'f']
del alist[1:5]
print(alist)
a = "banana"
b = "banana"
Q: Do a
and b
point to the same string?
A: Use is
operator to test
a = "banana"
b = "banana"
print(a is b)
a = "banana"
b = "banana"
print(a is b)
print(a == b)
Python optimizes resources by making different names that refer to the same value (immutable type) refer to the same object.
Immutable types: str, int, float, bool
a = 1
b = 1
print(a is b)
print(a == b)
print(id(a), id(b))
a = [81, 82, 83]
b = [81, 82, 83]
print(a is b)
print(a == b)
print(id(a), id(b))
from IPython.display import IFrame
IFrame("http://pythontutor.com/iframe-embed.html#code=a%20%3D%20%5B81,%2082,%2083%5D%0Ab%20%3D%20%5B81,%2082,%2083%5D%0A%0Aprint(a%20is%20b%29%0A%0Aprint(a%20%3D%3D%20b%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=false&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false", width='100%', height=450)
Variables refer to objects.
If we assign one variable to another, both variables refer to the same object
a = [81, 82, 83]
b = a
print(a is b)
from IPython.display import IFrame
IFrame("http://pythontutor.com/iframe-embed.html#code=a%20%3D%20%5B81,%2082,%2083%5D%0Ab%20%3D%20%5B81,%2082,%2083%5D%0A%09%0Aprint(a%20%3D%3D%20b%29%0Aprint(a%20is%20b%29%0A%09%0Ab%20%3D%20a%0Aprint(a%20%3D%3D%20b%29%0Aprint(a%20is%20b%29%0A%0Ab%5B0%5D%20%3D%205%0Aprint(a%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=false&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false", width='100%', height=450)
Exercise
What is printed by the following statements?
alist = [4, 2, 8, 6, 5]
blist = alist
blist[3] = 999
print(alist)
(A) [4, 2, 8, 6, 5]
(B) [4, 2, 8, 999, 5]
newlist = fromlist[:]
from IPython.display import IFrame
IFrame("http://pythontutor.com/iframe-embed.html#code=a%20%3D%20%5B81,%2082,%2083%5D%0A%0Ab%20%3D%20a%5B%3A%5D%20%20%20%20%20%20%20%23%20make%20a%20clone%20using%20slice%0Aprint(a%20%3D%3D%20b%29%0Aprint(a%20is%20b%29%0A%0Ab%5B0%5D%20%3D%205%0A%0Aprint(a%29%0Aprint(b%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=false&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false", width='100%', height=450)
If the list item is a sub-list, the repetition operator creates copies of the references.
origlist = [45, 76, 34, 55]
print(origlist * 3)
origlist = [45, 76, 34, 55]
print(origlist * 3)
newlist = [origlist] * 3
print(newlist)
Reference Diagram
origlist = [45, 76, 34, 55]
newlist = [origlist] * 3
print(newlist)
origlist[1] = 99
print(newlist)
from IPython.display import IFrame
IFrame("http://pythontutor.com/iframe-embed.html#code=origlist%20%3D%20%5B45,%2076,%2034,%2055%5D%0A%0Anewlist%20%3D%20%5Boriglist%5D%20*%203%0A%0Aprint(newlist%29%0A%0Aoriglist%5B1%5D%20%3D%2099%0A%0Aprint(newlist%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=false&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false", width='100%', height=450)
Exercise
1. What is printed by the following statements?
alist = [4, 2, 8, 6, 5]
blist = alist * 2
blist[3] = 999
print(alist)
(A) [4, 2, 8, 999, 5, 4, 2, 8, 6, 5]
(B) [4, 2, 8, 999, 5]
(C) [4, 2, 8, 6, 5]
2. What is printed by the following statements?
alist = [4, 2, 8, 6, 5]
blist = [alist] * 2
alist[3] = 999
print(blist)
(A) [4, 2, 8, 999, 5, 4, 2, 8, 999, 5]
(B) [[4, 2, 8, 999, 5], [4, 2, 8, 999, 5]]
(C) [4, 2, 8, 6, 5]
(D) [[4, 2, 8, 999, 5], [4, 2, 8, 6, 5]]
mylist = []
mylist.append(5)
mylist.append(27)
mylist.append(3)
mylist.append(12)
print(mylist)
print(mylist)
mylist.insert(1, 12)
print(mylist)
print(mylist.count(12))
print(mylist.index(3))
print(mylist.count(5))
print(mylist)
mylist.reverse()
print(mylist)
mylist.sort()
print(mylist)
mylist.sort(reverse=True)
print(mylist)
print(mylist)
mylist.remove(5)
print(mylist)
lastitem = mylist.pop()
print(lastitem)
mylist.pop(1)
print(mylist)
mylist = []
mylist.append(5)
mylist.append(27)
mylist.append(3)
mylist.append(12)
print(mylist)
mylist = mylist.sort() #probably an error
print(mylist)
mylist = [1,2,3,4]
mylist.extend([9,8,7])
mylist += [5,6]
print(mylist)
Exercise
1. What is printed by the following statements?
alist = [4, 2, 8, 6, 5]
alist.append(True)
alist.append(False)
print(alist)
(A) [4, 2, 8, 6, 5, False, True]
(B) [4, 2, 8, 6, 5, True, False]
(C) [True, False, 4, 2, 8, 6, 5]
2. What is printed by the following statements?
alist = [4, 2, 8, 6, 5]
alist.insert(2, True)
alist.insert(0, False)
print(alist)
(A) [False, 4, 2, True, 8, 6, 5]
(B) [4, False, True, 2, 8, 6, 5]
(C) [False, 2, True, 6, 5]
3. What is printed by the following statements?
alist = [4, 2, 8, 6, 5]
temp = alist.pop(2)
temp = alist.pop()
print(alist)
(A) [4, 8, 6]
(B) [2, 6, 5]
(C) [4, 2, 6]
4. What is printed by the following statements?
alist = [4, 2, 8, 6, 5]
alist = alist.pop(0)
print(alist)
(A) [2, 8, 6, 5]
(B) [4, 2, 8, 6, 5]
(C) 4
(D) None
from IPython.display import IFrame
IFrame("http://pythontutor.com/iframe-embed.html#code=origlist%20%3D%20%5B45,%2032,%2088%5D%0A%0Aoriglist.append(%22cat%22%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=false&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false", width='100%', height=450)
origlist = [45, 32, 88]
origlist = origlist + ['cat']
print(origlist)
origlist = [45, 32, 88]
origlist = origlist + 'cat'
print(origlist)
origlist = [45, 32, 88]
origlist = origlist + list('cat')
print(origlist)
Exercise
What is printed by the following statements?
alist = [4, 2, 8, 6, 5]
alist = alist + 999
print(alist)
(A) [4, 2, 8, 6, 5, 999]
(B) Error, you cannot concatenate a list with an integer.
fruits = ["apple", "orange", "banana", "cherry"]
for afruit in fruits: # by item
print(afruit)
fruits = ["apple", "orange", "banana", "cherry"]
for position in range(len(fruits)): # by index
print(fruits[position])
for number in range(20):
if number % 3 == 0:
print(number)
numbers = [1, 2, 3, 4, 5]
print(numbers)
for i in range(len(numbers)):
numbers[i] = numbers[i] ** 2
print(numbers)
Sometimes we are interested in both the value of an item, (we want to square that value), and its index (so that we can assign the new value to that position).
This pattern is common enough that Python provides a nicer way to implement it: enumerate(list)
enumerate generates pairs of both (index, value) during the list traversal
numbers = [1, 2, 3, 4, 5]
for (i, val) in enumerate(numbers):
numbers[i] = val**2
print(numbers)
for (i, v) in enumerate(["banana", "apple",
"pear", "lemon"]):
print(i, v)
Exercise
What is printed by the following statements?
alist = [4, 2, 8, 6, 5]
blist = [ ]
for item in alist:
blist.append(item+5)
print(blist)
(A) [4, 2, 8, 6, 5]
(B) [4, 2, 8, 6, 5, 5]
(C) [9, 7, 13, 11, 10]
(D) Error, you cannot concatenate inside an append.
list
and range
¶xs = list("Crunchy Frog")
print(xs)
"".join(xs)
One particular feature of range
is that it doesn’t instantly compute all its values: it “puts off” the computation, and does it on demand, or “lazily”.
We’ll say that it gives a promise to produce the values when they are needed.
This is very convenient if your computation short-circuits a search and returns early
def f(n):
""" Find the first positive integer between
101 and less than n that is divisible by 21
"""
for i in range(101, n):
if (i % 21 == 0):
return i
assert(f(110) == 105)
assert(f(1000000000) == 105)
r = range(10) # Create a lazy promise
print(r)
r = list(range(10))
print(r)
Functions which take lists as arguments and change them during execution are called modifiers and the changes they make are called side effects.
Passing a list as an argument actually passes a reference to the list, not a copy of the list.
def doubleStuff(aList):
""" Overwrite each element in aList with
double its value. """
for (i, v) in enumerate(aList):
aList[i] = 2 * v
things = [2, 5, 9]
print(things)
doubleStuff(things)
print(things)
from IPython.display import IFrame
IFrame("http://pythontutor.com/iframe-embed.html#code=def%20doubleStuff(aList%29%3A%0A%20%20%20%20%22%22%22%20Overwrite%20each%20element%20in%20aList%20with%20double%20its%20value.%20%22%22%22%0A%20%20%20%20for%20position%20in%20range(len(aList%29%29%3A%0A%20%20%20%20%20%20%20%20aList%5Bposition%5D%20%3D%202%20*%20aList%5Bposition%5D%0A%0Athings%20%3D%20%5B2,%205,%209%5D%0A%0AdoubleStuff(things%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=false&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false", width='100%', height=450)
A pure function does not produce side effects.
It communicates with the calling program only through parameters (which it does not modify) and a return value.
def doubleStuff(a_list):
""" Return a new list in which contains doubles of the elements in a_list. """
new_list = []
for value in a_list:
new_elem = 2 * value
new_list.append(new_elem)
return new_list
things = [2, 5, 9]
print(things)
things = doubleStuff(things)
print(things)
from IPython.display import IFrame
IFrame("http://pythontutor.com/iframe-embed.html#code=def%20doubleStuff(a_list%29%3A%0A%20%20%20%20%22%22%22%20Return%20a%20new%20list%20in%20which%20contains%20doubles%20of%20the%20elements%20in%20a_list.%20%22%22%22%0A%20%20%20%20new_list%20%3D%20%5B%5D%0A%20%20%20%20for%20value%20in%20a_list%3A%0A%20%20%20%20%20%20%20%20new_elem%20%3D%202%20*%20value%0A%20%20%20%20%20%20%20%20new_list.append(new_elem%29%0A%20%20%20%20return%20new_list%0A%0Athings%20%3D%20%5B2,%205,%209%5D%0Athings%20%3D%20doubleStuff(things%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=false&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false", width='100%', height=450)
Anything that can be done with modifiers can also be done with pure functions.
There is some evidence that programs that use pure functions are faster to develop and less error-prone than programs that use modifiers.
In general, we recommend that you write pure functions whenever it is reasonable to do so. This approach might be called a functional programming style.
Pattern:
initialize a result variable to be an empty list
loop
create a new element
append it to result
return the result
def primes_upto(n):
""" Return a list of all prime numbers
less than n. """
result = []
for i in range(2, n):
if is_prime(i):
result.append(i)
return result
List comprehensions are concise ways to create lists.
The general syntax is:
[<expression> for <item> in <sequence> if <condition>]
mylist = [1,2,3,4,5]
yourlist = [item ** 2 for item in mylist]
print(yourlist)
def primes_upto(n):
""" Return a list of all prime numbers less than n using a list comprehension. """
result = [num for num in range(2,n)
if is_prime(num)]
return result
Exercise
Write a Python program to generate a 3*4*6 3D array whose each element is *.
nested = ["hello", 2.0, 5, [10, 20]]
innerlist = nested[3]
print(innerlist)
item = innerlist[1]
print(item)
print(nested[3][1])
Exercise
Write a Python program to generate all sublists of a list.
The split method breaks a string into a list of words. By default, any number of whitespace characters is considered a word boundary.
song = "The rain in Spain..."
wds = song.split()
print(wds)
# An optional argument called a delimiter can be used
# to specify which characters to use as word boundaries.
song = "The rain in Spain..."
wds = song.split('ai')
print(wds)
join
The inverse of the split method is join.
You choose a desired separator string, (often called the glue) and join the list with the glue between each of the elements.
wds = ["red", "blue", "green"]
glue = ';'
s = glue.join(wds)
print(s)
print(wds)
print("***".join(wds))
print("".join(wds))
Exercise
Write a Python program to convert a list of multiple integers into a single integer.
inlst = [1,2,34,5]
outi = 12345
list
Type Conversion Function¶Whereas split
will break a string into a list of “words”, list
will always break it into a list of characters.
xs = list("Crunchy Frog")
print(xs)
A tuple, like a list, is a sequence of items of any type.
Unlike lists, however, tuples are immutable.
Syntactically, a tuple is a comma-separated sequence of values.
It is conventional to enclose tuples in parentheses.
julia = ("Julia", "Roberts", 1967, "Duplicity",
2009, "Actress", "Atlanta, Georgia")
print(type(julia))
print(julia)
Tuples are useful for representing what other languages often call records — some related information that belongs together, like your student record.
A tuple lets us “chunk” together related information and use it as a single thing.
Tuples support the same sequence operations as strings and lists.
julia = ("Julia", "Roberts", 1967, "Duplicity",
2009, "Actress", "Atlanta, Georgia")
julia[0] = 'X'
julia = ("Julia", "Roberts", 1967, "Duplicity",
2009, "Actress", "Atlanta, Georgia")
print(julia[2])
print(julia[2:6])
print(len(julia))
julia = julia[:3] + ("Eat Pray Love", 2010) + \
julia[5:]
print(julia)
tup = (5,)
print(type(tup))
x = (5)
print(type(x))
Count how many students are taking CompSci
students = [
("John", ["CompSci", "Physics"]),
("Vusi", ["Maths", "CompSci", "Stats"]),
("Jess", ["CompSci", "Accounting", "Economics", "Management"]),
("Sarah", ["InfSys", "Accounting", "Economics", "CommLaw"]),
("Zuki", ["Sociology", "Economics", "Law", "Stats", "Music"])]
students = [
("John", ["CompSci", "Physics"]),
("Vusi", ["Maths", "CompSci", "Stats"]),
("Jess", ["CompSci", "Accounting", "Economics", "Management"]),
("Sarah", ["InfSys", "Accounting", "Economics", "CommLaw"]),
("Zuki", ["Sociology", "Economics", "Law", "Stats", "Music"])]
# Count how many students are taking CompSci
counter = 0
for (name, subjects) in students:
if "CompSci" in subjects:
counter += 1
print("The number of students taking CompSci is",
counter)
(name, surname, birth_year, movie, movie_year, profession, birth_place) = julia
swap the values of two variables
conventional:
temp = a
a = b
b = temp
tuple assignment:
(a, b) = (b, a)
a, b = 10, 20
print(a, b)
a, b = b, a
print(a,b)
a, b = (10, 20)
print(a, b)
(a, b) = (b, a)
print(a,b)
(a, b, c, d) = (1, 2, 3)
print((1,2,3) == (1,2,3,))
def circleInfo(r):
""" Return (circumference, area) of a
circle of radius r """
c = 2 * 3.14159 * r
a = 3.14159 * r * r
return (c, a)
print(circleInfo(10))
Exercise
Write a Python program to get a list, sorted in increasing order by the last element in each tuple from a given list of non-empty tuples.
Sample List : [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
Expected Result : [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]
A set contains an unordered collection of unique and immutable objects.
The set data type is a Python implementation of the sets as they are known from mathematics.
Set elements must be hashable objects (All of Python’s immutable built-in objects are hashable).
x = set("A Python Tutorial")
print(x)
print(type(x))
x = set(["Perl", "Python", "Java"])
print(x)
y = set(("Perl", "Python", "Java"))
print(x)
z = {"Perl", "Python", "Java"}
print(x)
x = {1,2,3}
y = {3,2,1}
x == y
x = {1,2,3,2,3}
x == {1,2,3}
len(s), x in s, x not in s
add remove discard
union difference intersection symmetric_difference
isdisjoint issubset issuperset
add(element)
colours = {"red","green"}
colours.add("yellow")
colours.add("yellow")
print(colours)
colours.add(["black","white"])
union
a = {1,2,3}
b = {3,4,5}
a.union(b)
a = {1,2,3}
b = {3,4,5}
a | b
difference
a = {1,2,3}
b = {3,4,5}
a.difference(b)
a = {1,2,3}
b = {3,4,5}
a - b
intersection
a = {1,2,3}
b = {3,4,5}
a.intersection(b)
a = {1,2,3}
b = {3,4,5}
a & b
symmetric_difference
a = {1,2,3}
b = {3,4,5}
a.symmetric_difference(b)
a = {1,2,3}
b = {3,4,5}
a ^ b
isdisjoint
a = {1,2,3}
b = {3,4,5}
c = {4,5,6}
print(a.isdisjoint(b))
print(a.isdisjoint(c))
issubset
a,b,c = {1,2,3},{2,3,4},{1,2}
print(c.issubset(a))
print(c.issubset(b))
a,b,c = {1,2,3},{2,3,4},{1,2}
print(c <= a)
print(c <= b)
issuperset
a,b,c = {1,2,3},{2,3,4},{1,2}
print(a.issuperset(c))
print(b.issuperset(c))
a,b,c = {1,2,3},{2,3,4},{1,2}
print(a >= c)
print(b >= c)
a = [1,2,3,2,3,4,5,6,7,8,2]
a = [1,2,3,2,3,4,5,6,7,8,2]
a = list(set(a))
print(a)
The bisect
module offers two main functions-- bisect
and insort
-- that use the binary search algorithm to quickly find and insert items in any sorted sequence.
import bisect
def grade(score, breakpoints=[60,70,80,90], grades='FDCBA'):
i = bisect.bisect(breakpoints, score)
return grades[i]
[grade(score) for score in [33,99,77,70,89,90,100]]
import bisect
import random
SIZE = 7
random.seed(1729)
my_list = []
for i in range(SIZE):
new_item = random.randrange(SIZE*2)
bisect.insort(my_list, new_item)
print('%2d ->' % new_item, my_list)
from array import array
memoryview
bytes bytearray
import numpy, scipy
from array import array
from random import random
floats = array('d', (random() for i in range(10**7)))
print(floats[0])
fp = open('floats.bin','wb')
floats.tofile(fp)
fp.close
floats2 = array('d')
fp = open('floats.bin', 'rb')
floats2.fromfile(fp, 10**7)
fp.close()
print(floats2[0])
print(floats == floats2)
aliases
Multiple variables that contain references to the same object.
clone
To create a new object that has the same value as an existing object. Copying a reference to an object creates an alias but doesn’t clone the object.
delimiter
A character or string used to indicate where a string should be split.
element
One of the values in a list (or other sequence). The bracket operator selects elements of a list.
index
An integer variable or value that indicates an element of a list.
list
A collection of objects, where each object is identified by an index. Like other types str, int, float, etc. there is also a list type-converter function that tries to turn its argument into a list.
list traversal
The sequential accessing of each element in a list.
modifier
A function which changes its arguments inside the function body. Only mutable types can be changed by modifiers.
mutable data type
A data type in which the elements can be modified. All mutable types are compound types. Lists are mutable data types; strings are not.
nested list
A list that is an element of another list.
object
A thing to which a variable can refer.
pattern
A sequence of statements, or a style of coding something that has general applicability in a number of different situations. Part of becoming a mature Computer Scientist is to learn and establish the patterns and algorithms that form your toolkit. Patterns often correspond to your “mental chunking”.
pure function
A function which has no side effects. Pure functions only make changes to the calling program through their return values.
sequence
Any of the data types that consist of an ordered collection of elements, with each element identified by an index.
side effect
A change in the state of a program made by calling a function that is not a result of reading the return value from the function. Side effects can only be produced by modifiers.
tuple
A sequential collection of items, similar to a list. Any python object can be an element of a tuple. However, unlike a list, tuples are immutable.
inlst = ['p', 'q']
n = 5
outlst = ['p1', 'q1', 'p2', 'q2', 'p3', 'q3', 'p4', 'q4', 'p5', 'q5']
inlst = [[1,2,3], [4,5],[6,7,8],9]
outlst = [1,2,3,4,5,6,7,8,9]
def isSubList(l, s):
'return True if s is sub list of l, otherwise return False'
# To DO
return True
assert(isSubList([1,10,3,4,6], [3,4]) == True)
assert(isSubList([1,10,3,4,6], [3,6]) == False)
Some exercise answners
def mex1(l):
imax = None
imex = None
for i in l:
if imax == None:
imax = i
else:
if imax < i :
imex = imax
imax = i
elif imax > i :
if imex == None:
imex = i
else:
if i > imex :
imex = i
return imex
def mex2(l):
a = list(set(l))
a.sort(reverse=True)
if len(a) == 1:
return None
else:
return a[1]
l0 = [3,4,4,4,3,2,1]
l1 = [4,4,4,4,4,4,4]
print(mex2(l0))
print(mex2(l1))
[[['*' for i in range(6)] for j in range(4)] for k in range(3)]
def gensubs(l):
subs = [[]]
for i in range(len(l)-1):
for j in range(i+1,len(l)):
subs.append(l[i:j])
return subs
def gensubs2(l):
return [ l[f:t] for f in range(len(l)-1) for t in range(f+1,len(l))
] + [[]]
l = [1,2,3,4]
print(gensubs2(l))
a = [1,2,34,56,9]
print(int(''.join([str(i) for i in a])))
l = [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
l.sort(key=lambda t: t[1])
print(l)
l = [1,2,4,2,6,7,3,99,8]
def mysort(l):
for i in range(len(l)):
for j in range(i+1,len(l)):
if l[j] < l[i]:
l[j],l[i] = l[i],l[j]
mysort(l)
print(l)
l = [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
def mysort(l):
for i in range(len(l)):
for j in range(i+1,len(l)):
if l[j][1] < l[i][1]: # judge criterio
l[j],l[i] = l[i],l[j]
mysort(l)
print(l)
a = (10,20)
x = 10,20
x
inlst = ['p', 'q']
n = 5
outlst = []
for i in range(1,n+1):
outlst.append(inlst[0]+str(i))
outlst.append(inlst[1]+str(i))
print(outlst)
inlst = ['p', 'q']
n = 5
outlst = [j+str(i) for i in range(1,n+1) for j in inlst]
print(outlst)
inlst = [[1,2,3], [4,5],[6,7,8],9]
#outlst = [1,2,3,4,5,6,7,8,9]
outlst = []
for i in inlst:
if type(i) == type([]):
outlst.extend(i)
else:
outlst.append(i)
print(outlst)
def isSubList(l, s):
'return True if s is sub list of l, otherwise return False'
# To DO
for i in range(len(l)):
if i + len(s) > len(l):
return False
if s == l[i:i+len(s)]:
return True
return False
assert(isSubList([1,10,3,4,6], [3,4]) == True)
assert(isSubList([1,10,3,4,6], [3,6]) == False)