首页 > 代码库 > Python 简单的数据结构(一)
Python 简单的数据结构(一)
学习笔记
#python 3.5.2
解压序列赋值给多个变量
>>> x=(1,2,3) >>> a,b,c=x >>> a 1 >>> b 2 >>> c 3 >>> data=http://www.mamicode.com/[‘1‘,‘2‘,‘3‘,‘4‘,‘5‘]"<pyshell#14>", line 1, in <module> a,b,c=y ValueError: not enough values to unpack (expected 3, got 2) >>> >>> data=http://www.mamicode.com/[‘1‘,2,(3,4),5]>解压可迭代对象赋值给多个变量
>>> L=[1,2,3,4,5,6] >>> head,*tail=L >>> head 1 >>> tail [2, 3, 4, 5, 6] >>> head,*middle,tail=L >>> head 1 >>> middle [2, 3, 4, 5] >>> tail 6 >>> L=[‘aaa‘,10,(1,2,3),‘dddd‘] >>> head,*middle,tail=L >>> head ‘aaa‘ >>> middle [10, (1, 2, 3)] >>> tail ‘dddd‘ >>> name,age,(a,b,c),addr=L >>> a 1 >>> b 2 >>> c 3 >>> addr ‘dddd‘ >>> age 10 >>>
保留最后N个元素
>>> from collections import deque >>> q=deque(maxlen=4) >>> q.append(1) >>> q.append(2) >>> q.append(3) >>> q deque([1, 2, 3], maxlen=4) >>> q.append(4) >>> q deque([1, 2, 3, 4], maxlen=4) >>> q.append(5) >>> q deque([2, 3, 4, 5], maxlen=4) >>> q.append(6) >>> q deque([3, 4, 5, 6], maxlen=4) >>> q.appendleft(0) >>> q deque([0, 3, 4, 5], maxlen=4) >>> q.appendleft(-1) >>> q deque([-1, 0, 3, 4], maxlen=4) >>>
查找最大或最小的N个元素
>>> import heapq >>> nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2] >>> heapq.nlargest(3,nums) [42, 37, 23] >>> heapq.nlargest(4,nums) [42, 37, 23, 23] >>> heapq.nsmallest(2,nums) [-4, 1] >>> heapq.nsmallest(4,nums) [-4, 1, 2, 2] >>>
实现一个优先级队列
>>> import heapq >>> queue=[] >>> heapq.heappush(queue,(3,‘three‘)) >>> heapq.heappush(queue,(2,‘two‘)) >>> heapq.heappush(queue,(5,‘five‘)) >>> heapq.heappush(queue,(1,‘one‘)) >>> heapq.heappush(queue,(4,‘four‘)) >>> heapq.heappop(queue) (1, ‘one‘) >>> heapq.heappop(queue) (2, ‘two‘) >>> heapq.heappop(queue) (3, ‘three‘) >>> heapq.heappop(queue) (4, ‘four‘) >>> heapq.heappop(queue) (5, ‘five‘) >>>
字典中键映射多个值
>>> from collections import defaultdict >>> d=defaultdict(list) >>> d[‘a‘].append(1) >>> d[‘a‘].append(2) >>> d[‘a‘].append(3) >>> d[‘b‘].append(-1) >>> d[‘b‘].append(-2) >>> d[‘c‘].append(‘C‘) >>> d defaultdict(<class ‘list‘>, {‘b‘: [-1, -2], ‘c‘: [‘C‘], ‘a‘: [1, 2, 3]}) >>>
字典保持顺序
>>> from collections import OrderedDict >>> d=OrderedDict() >>> d[‘B‘]=2 >>> d[‘A‘]=1 >>> d[‘C‘]=3 >>> d[‘F‘]=6 >>> d[‘E‘]=5 >>> d OrderedDict([(‘B‘, 2), (‘A‘, 1), (‘C‘, 3), (‘F‘, 6), (‘E‘, 5)]) >>> for i in d: print(i,d[i]) B 2 A 1 C 3 F 6 E 5 >>> D=dict() >>> D[‘B‘]=2 >>> D[‘A‘]=1 >>> D[‘C‘]=3 >>> D[‘F‘]=6 >>> D[‘E‘]=5 >>> for i in D: print(i,D[i]) A 1 F 6 C 3 B 2 E 5 >>>
字典的运算
>>> prices = { ‘ACME‘: 45.23, ‘AAPL‘: 612.78, ‘IBM‘: 205.55, ‘HPQ‘: 37.20, ‘FB‘: 10.75 } >>> min_price=min(zip(prices.values(),prices.keys())) >>> min_price (10.75, ‘FB‘) >>> max_price=max(zip(prices.values(),prices.keys())) >>> max_price (612.78, ‘AAPL‘) >>> prices_sorted=sorted(zip(prices.values(),prices.keys())) >>> prices_sorted [(10.75, ‘FB‘), (37.2, ‘HPQ‘), (45.23, ‘ACME‘), (205.55, ‘IBM‘), (612.78, ‘AAPL‘)] >>> min(prices) ‘AAPL‘ >>> max(prices) ‘IBM‘ >>> min(prices,key=lambda x:prices[x]) ‘FB‘ >>> max(prices,key=lambda x:prices[x]) ‘AAPL‘ >>> prices[min(prices,key=lambda x:prices[x])] 10.75 >>> prices[max(prices,key=lambda x:prices[x])] 612.78 >>>
查找两个字典的相同点
>>> a = { ‘x‘ : 1, ‘y‘ : 2, ‘z‘ : 3 } >>> b = { ‘w‘ : 10, ‘x‘ : 11, ‘y‘ : 2 } >>> a.keys() & b.keys() {‘x‘, ‘y‘} >>> a.keys() - b.keys() {‘z‘} >>> a.items() & b.items() {(‘y‘, 2)} >>>
删除序列相同元素并保持顺序
>>> def dedupe(items): seen =set() for item in items: if item not in seen: yield item seen.add(item) >>> a = [1, 5, 2, 1, 9, 1, 5, 10] >>> list(dedupe(a)) [1, 5, 2, 9, 10]>>>
命名切片
>>> record = ‘....................100 .......513.25 ..........‘ >>> shares=slice(20,23) >>> price=slice(31,37) >>> cost=int(record[shares])*float(record[price]) >>> cost 51325.0 >>>
本文出自 “Chauncey” 博客,请务必保留此出处http://cqwujiang.blog.51cto.com/10808946/1948892
Python 简单的数据结构(一)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。