python data structure (and algorithm analysis)
1.1 decomposed into individual variable sequence:
1.1.1 Solution:
Any sequence can be decomposed into a plurality of sequences of simple assignment, the only requirement is that the structure and the total number of variables to be consistent with the sequence of
example:
tp = (1,1,2)
var1,var2,var3 = tp
print(var1,var2,var3)
data = ['hello','a','b','c','d']
var1,var2,var3,var4,var5 = data
print(var1,var2,var3,var4,var5)
#若变量个数和元素个不统一则会报错:
#ValueError:need more than 2 values to unpack / too many values to unpack
1.2 iterables elements from the decomposition of any length
1.2.1 Solution:
than the one of the most cattle x python very flexible "* expression " the
example:
num1,*kargs,num2 = [1,2,3,4,5,6]
print(num1)# 1
print(num2)# 2
print(*kargs)# [2,3,4,5]
avg = sum(*args)/len(*args)
print(avg)# 2+3+4+5 / 4 == 3.5
A more complex example:
record = [
('foo',1,2),
('bar','hello'),
('foo',2,3),
('foo',4,4)
]
def do_foo(x,y):
print('foo',x,y)
def do_bars(s):
print('bar',s)
for tag,*args in record:
if tag == 'foo':
do_foo(*args)
elif tag == 'bar':
do_bars(*args)
#有些时候想要丢弃某些值:
record = ('白piao老师', 50, 123.45, (12, 9, 2012)]
name,*_,(*_,year) = record #常常使用_或者ign来表示丢弃值
print(name,year)
#输出结果: 小白piao老师 2012
#有的时候依托于*可以做出更加精妙的算法
#例子:
def func(items):
head_var, *tail_var = items
return var_head + func(tail_var) if tail_var else head_var
>>>func([1,2,3,4,5])
>>>15
#其实python做这个真心鸡肋....
1.3 Save the last N elements:
If we want iteration or other forms of processing to do a limited historical records of the last few records:
1.3.1 Solution
from collections import deque
def search(lines,pattern,history):
previous_lines = deque(maxlen = history)
for line in lines:
if pattern == line :
yield line,privious_lines
privious_lines.append(line)
if __name__ == '__main__':
with open('test.txt') as f:
for line,prevlines in search(f,'whitepiao',5):
for line1 in prevlines:
print(line1)
print(line,end = '')
print('-'*20)
1.3.2 About deque
In fact, essentially deque a queue length may be fixed, if a fixed length, in the case where the queue is full, a new value is added thereto, the oldest value will be automatically ejected,
>>>q = deque(maxlen = 3)
>>>q
>>>deque([],maxlen=3)
>>>q.append(1)
>>>q.append(2)
>>>q.append(3)
>>>q
>>>deque([1,2,3],maxlen = 3)
>>>q.append(4)
>>>q
>>>deque([2,3,4],maxlen = 3)
>>>q.appendleft(77)
>>>q
>>>deque([77,2,3],maxlen = 3)
1.4 find the maximum or minimum of N elements
1.4.1 find the maximum or minimum of N elements in a collection
1.4.2 Solution:
import heapq as h
nums = [1,8, 2, 23, -4, 18, 23, 42, 37, 2]
print(h.nlargest(3,nums))# [42,37,23]
print(h.nsmallest(3,nums)# [-4,1,2]
More used nlargest and nsmallest can accept a parameter key:
proInfo = [
{'name':'IB','shares':100,'prices':91.1},
{'name':'APP','shares':99,'prices':107.2},
{'name':'LEN','shares':80,'prices':99.0},
{'name':'ASU','shares':80,'prices':91.0},
{'name':'DEL','shares':95,'prices':89.7},
]
cheap = h.nsmallest(3,proInfo,lambda x:x['price'])
expensive = h.nlargest(3,proInfo,lambda x:x['price'])
>>>cheap
>>>[89.7,91.0,91.1]