5. Data Structure
5.1 More on Lists
fruits =['orange','apple','pear','banana','kiwi','apple','banana']
fruits.count('apple')
2
print(fruits)
['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
fruits.index('orange')
0
fruits.index('banana',4)
6
fruits.reverse()
fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
fruits.append('grepe')
fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grepe']
fruits.sort()
fruits
['apple', 'apple', 'banana', 'banana', 'grepe', 'kiwi', 'orange', 'pear']
fruits.pop()
'pear'
fruits
['apple', 'apple', 'banana', 'banana', 'grepe', 'kiwi', 'orange']
5.1.1. Using Lists as Stacks
stack = [3,4,5]
stack.append(6)
stack.append(7)
stack
[3, 4, 5, 6, 7]
stack.pop()
7
stack
[3, 4, 5, 6]
5.1.2. Using Lists as Queues
from collections import deque
queue = deque(["Eric","John","Michael"])
queue.append("Terry")
queue.append("Graham")
queue.popleft()
'Eric'
queue
deque(['John', 'Michael', 'Terry', 'Graham'])
5.1.3. List Comprehensions
squares = []
for x in range(10):
squares.append(x**2)
squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
squares = list(map(lambda x : x**2, range(10)))
squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
squares = list(map(lambda x : x**2, range(11)))
squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
squares = [ x**2 for x in range(11)]
squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[(x,y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
combs = []
for x in [1,2,3]:
for y in [3,1,4]:
if x != y:
combs.append((x,y))
combs
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
from math import pi
[str(round(pi,i)) for i in range(1,6)]
['3.1', '3.14', '3.142', '3.1416', '3.14159']
5.1.4. Nested List Comprehensions
matrix2 = [ [1,2,3,4],
[5,6,7,8],
[9,10,11,12],
]
[[row[i] for row in matrix2] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
transposed = []
for i in range(4):
transposed.append([row[i] for row in matrix2])
transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
5.3 Tuples and Sequences
t = 12344, 543,'hello'
t[0]
12344
t
(12344, 543, 'hello')
# Tuples may be nested:
u = t, (1, 2,3,5)
u
((12344, 543, 'hello'), (1, 2, 3, 5))
# tuples are immutable:
t[0] = 888
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
# but they can contain mutable objects:
v = ([1,2,4],[4,2,1])
v
([1, 2, 4], [4, 2, 1])
6.Modules
# Fiboonaccci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0,1
while b < n:
print(b, end =' ')
a, b= b, a + b
print()
def fib2(n): # return Fibonacci serries up to n
result = []
a, b = 0,1
while b < n:
result.append(b)
a, b = b, a+b
return result
import fibo
fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
fibo.fib2(1000)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
fibo.__name__
'fibo'
6.1. More on Modules
fibo.__name__
'fibo'
fibo.fib2(100)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
from fibo import fib,fib2
fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377
import fibo import *
File "<input>", line 1
import fibo import *
^
SyntaxError: invalid syntax
from fibo import *
fib(20)
1 1 2 3 5 8 13
6.1.1 Executing modules as scripts
if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))
6.2 Standard Modules
import sys
sys.path.append('/ufs/guido/lib/python')
网友评论