cookbook_數據結構和算法

1.1將數據分解爲單獨的變量
list_a = [1,2,3,4,5,6,7,8,9]
a,b,c,d,e,f,g,h,i = list_a
print(a,b,c,d,e,f,g,h,i)
#使用相等數量的參數來接收


_,b,c,d,e,f,g,h,_ = list_a 
print(b,c,d,e,f,g,h)
#不要的數據使用一個沒有用的變量接收
View Code

 

1.2從任意長度的可迭代對象中分解元素

使用 * XXX實現python

list_a = range(20)
first,*middle,last = list_a
print(first,middle,last)
#使用*來接收任意數量,甚至沒有,返回一個list

#當一個元祖內有一個標誌位時,一個較好的應用
records = [
    ("foo",1,2),
    ("bar","hello"),
    ("foo",3,4)
]

def do_foo(x,y):
    print("foo",x,y)

def do_bar(s):
    print("bar",s)

for tags,*args in records:
    if tags == "foo":
        do_foo(*args)
    elif tags == "bar":
        do_bar(*args)
View Code

 

1.3保存最後N個元素

collections.deque()數據結構

import collections

#使用collections.deque(maxlen=5)來定義一個固定長度的list,有新數據寫入時若是已經達到maxlen,會自動刪除最先插入的數據
def search(lines,pattern,history = 5):
    previous_lines = collections.deque(maxlen=history)
    for line in lines:
        if pattern in line:
            yield line,previous_lines
        previous_lines.append(line)

if __name__ =="__main__":
    with open("test.txt","r",encoding="utf8") as f:
        for line,previous in search(f,"python",5):
            for pline in previous:
                print(pline,end="")
            print(line,end="")
            print("-"*20)

#collections.deque使用簡介
#一個更增強大的list

queue = collections.deque(["jiao","li",'hao',"yu"])
queue.appendleft("wu")
print(queue)
queue.append("haha")
print(queue)
queue.popleft()
print(queue)
print(queue[4])
View Code
 
1.4找到最大或最小的N個元素

heapq.nlargest(),heapq.nsmallest() app

import heapq

nums = [5,56,7,6,34,2,5,7,6,89,80,-90,0,9,-67,5,45,]

print(min(nums))
print(max(nums))

print(heapq.nlargest(3,nums))
print(heapq.nsmallest(3,nums))

#可支持更加複雜的數據結構

portfolio = [
    {"name":"jiao","age":24},
    {"name":"jsdfo","age":2},
    {"name":"jisd","age":12},
    {"name":"jdo","age":36},
    {"name":"li","age":25},
    {"name":"jgd","age":50},
]

print(heapq.nlargest(3,portfolio,key=lambda s:s['age']))
print(max(portfolio,key=lambda s:s['age']))
View Code

 

 

1.5實現優先級隊列

heapq.heappush(),heapq.heappop()ide

import heapq


#列表中實際存一個元組,(-priority,self._index,item)
class PriorityQueue:
    def __init__(self):
        self._queue = []
        self._index = 0

    def push(self,item,priority):
        heapq.heappush(self._queue,(-priority,self._index,item))
        self._index += 1

    def pop(self):
        return heapq.heappop(self._queue)[-1]

    def get(self):
        return self._queue

q = PriorityQueue()
q.push("foo",2)
q.push("sdf",3)
q.push("sfasc",5)
q.push("fdsg",4)
print(q.pop())
print(q.get())
View Code
 
1.6在字典中將鍵映射到多個值上

collections.defaultdict(list),collections.defaultdict(set)大數據

import collections

d = collections.defaultdict(list)#自動初始化,不用判斷是否存在
d["a"].append(1)
d["a"].append(1)
d["a"].append(1)
d["a"].append(1)
print(d['a'])
View Code

 

1.7讓字典保持有序

collections.OrderedDict()spa

import collections

d = collections.OrderedDict()#普通字典的兩倍,大數據不該該使用
d['foo'] = 1
d["bar"] = 2
d["spam"] = 3
d["gork"] = 4
for i in d:
    print(i)
View Code

 

1.8與字典有關的計算問題

zip(),min(),sorted().max()code

#字典進行大小運算時都是使用key值進行大小比較,而咱們通常想要用value值比較,並且還想要獲得該值的key

prices = {
    "ACME":23,
    "AAPL":345,
    "IBM":34,
    "FB":24
}

#利用zip,zip返回一個迭代器,只能使用一次

min_price = min(zip(prices.values(),prices.keys()))
print(min_price)

#排序
price_sorted = sorted(zip(prices.values(),prices.keys()))
print(price_sorted)
View Code

 

 
1.9在兩個字典中尋找相同點

 

a = {
    "x":2,
    "y":5,
    "z":7
}

b = {
    "x":2,
    "y":8,
    "w":4
}

print(a.keys() & b.keys())#尋找相同的key
print(a.keys() - b.keys())#尋找a中有b中沒有的key
print(a.items() & b.items())#尋找相同項
View Code

 

 

1.10從序列中移除重複項且保持元素間順序不變
def dedupe(items,key = None):
    seen = set()
    for item in items:
        val = item if key is None else key(item)
        if val not in seen:
            yield item
            seen.add(val)
View Code
相關文章
相關標籤/搜索