Lists Are Objects: Topics: References Alias More On Slicing
Lists Are Objects: Topics: References Alias More On Slicing
Topics:
References
Alias
More on Slicing
Comparing Lists
You can use == to compare two lists
>>> x = [10,20,30,40]
>>> y = [10,20,30,40]
>>> x==y
True
Comparing Lists
You can use == to compare two lists
>>> x = [1,2,3]
>>> y = [1.0,2.0,3.0]
>>> x==y
True
If there are ints and floats, convert everything to float then compare
Comparing Lists
Do not use <, <= , > , >= to compare two lists
>>> x = [10,20,30,40]
>>> y = [11,21,31,41]
>>> x<y
True
>>> y<x
True
Unpredictable
Aliasing
This:
x = [10,20,30,40]
y = x
Results in this:
0 ---> 10
x --> 1 ---> 20
2 ---> 30
y --> 3 ---> 40
Aliasing
0 ---> 10
x --> 1 ---> 20
y --> 2 ---> 30
3 ---> 40
Things to say:
0 ---> 10
x = [10,20,30,40] x --> 1 ---> 20
y = x
2 ---> 30
y = [1,2,3]
3 ---> 40
Tracking Changes
0 ---> 10
x = [10,20,30,40] x --> 1 ---> 20
y = x
y = [1,2,3] y --> 2 ---> 30
3 ---> 40
Tracking Changes
0 ---> 10
x = [10,20,30,40] x --> 1 ---> 20
y = x
2 ---> 30
y = [1,2,3]
3 ---> 40
0 ---> 1
y --> 1 ---> 2
2 ---> 3
The is Operator
>>> x = [10,20,30,40]
>>> y = [10,20,30,40]
>>> x is y
False
0 ---> 10 0 ---> 10
x --> 1 y --> 1 ---> 20
---> 20
2 ---> 30 2 ---> 30
3 ---> 40 3 ---> 40
0 ---> 10
x = [10,20,30,40] x --> 1 ---> 20
y = list(x)
2 ---> 30
3 ---> 40
Making a Copy of a List
0 ---> 10
x = [10,20,30,40] x --> 1 ---> 20
y = list(x)
2 ---> 30
3 ---> 40
0 ---> 10
y --> 1 ---> 20
2 ---> 30
3 ---> 40
Slices Create new Objects
0 ---> 10
x = [10,20,30,40] x --> 1 ---> 20
y = x[1:]
2 ---> 30
3 ---> 40
Slices Create New Objects
0 ---> 10
x = [10,20,30,40] x --> 1 ---> 20
y = x[1:]
2 ---> 30
3 ---> 40
0 ---> 20
y --> 1 ---> 30
2 ---> 40
Careful!
0 ---> 10
x = [10,20,30,40] x --> 1 ---> 20
y = x
2 ---> 30
y = x[1:]
3 ---> 40
Careful!
0 ---> 10
x = [10,20,30,40] x --> 1 ---> 20
y = x
y = x[1:] y --> 2 ---> 30
3 ---> 40
Careful!
0 ---> 10
x = [10,20,30,40] x --> 1 ---> 20
y = x
2 ---> 30
y = x[1:]
3 ---> 40
0 ---> 20
y --> 1 ---> 30
2 ---> 40
Void Functions
0 ---> 40
x = [40,20,10,30] x --> 1 ---> 20
y = x.sort()
2 ---> 10
3 ---> 30
y -->
Void Functions
0 ---> 10
x = [40,20,10,30] x --> 1 ---> 20
y = x.sort()
2 ---> 30
3 ---> 40
y --> None
0 ---> 40
x = [40,20,10,30] x --> 1 ---> 20
y = list(x)
2 ---> 10
y.sort()
3 ---> 30
0 ---> 40
y --> 1 ---> 20
2 ---> 10
3 ---> 30
0 ---> 40
x = [40,20,10,30] x --> 1 ---> 20
y = list(x)
2 ---> 10
y.sort()
3 ---> 30
0 ---> 10
y --> 1 ---> 20
2 ---> 30
3 ---> 40
if __name__ == '__main__':
u = [1,2,3,4]
f(u)
print u
Before: 10 20 30 40 50 60 70 80
After: 10 50 20 60 30 70 40 80
Executing the Perfect Shuffle
The given list:
10 20 30 40 50 60 70 80
Cut it in half:
10 20 30 40 50 60 70 80
10 20 30 40 50 60 70 80
Cut it in half:
10 20 30 40 50 60 70 80
10 20 30 40 50 60 70 80
Cut it in half:
10 20 30 40 50 60 70 80
10 20 30 40 50 60 70 80
Cut it in half:
10 20 30 40 50 60 70 80
10 20 30 40 50 60 70 80
Cut it in half:
10 20 30 40 50 60 70 80
10 20 30 40 50 60 70 80
Cut it in half:
10 20 30 40 50 60 70 80
10 20 30 40 50 60 70 80
Cut it in half:
10 20 30 40 50 60 70 80
10 20 30 40 50 60 70 80
Cut it in half:
10 20 30 40 50 60 70 80
10 20 30 40 50 60 70 80
Cut it in half:
10 20 30 40 50 60 70 80
Question:
n numPFs
--------------------
8 3
52 8
444 442
1000 36
10000 300
100000 540