cookbook_数据结构和算法
生活随笔
收集整理的這篇文章主要介紹了
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實現
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_linesprevious_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()
import heapqnums = [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()
import heapq#列表中實際存一個元組,(-priority,self._index,item) class PriorityQueue:def __init__(self):self._queue = []self._index = 0def push(self,item,priority):heapq.heappush(self._queue,(-priority,self._index,item))self._index += 1def pop(self):return heapq.heappop(self._queue)[-1]def get(self):return self._queueq = 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 collectionsd = 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()
import collectionsd = 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()
#字典進行大小運算時都是使用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 itemseen.add(val) View Code?
?
?
?
?
?
轉載于:https://www.cnblogs.com/jiaojianglong/p/11260093.html
總結
以上是生活随笔為你收集整理的cookbook_数据结构和算法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信用卡取现可以分期吗?信用卡取现额度用完
- 下一篇: 机器学习概览