python 标准库 数据结构

~list tuple dict set
1、collections.Counter
collections.Counter 属于dict,计算出现几次


image.png

c.update({'a':3}) 更新


image.png

对于未知的元素,counter不会产生异常,返回0
Counter转成list:list(c.elements())
c.most_common(3) 打印最常见的三个元素,即value最大的那三个
Counter支持+ - & |

2、defaultdict 支持自定义方法提供默认值的dict


image.png

3、deque 线程安全的
deque.append deque.extend deque.extendleft deque.appendleft
deque.pop deque.popleft
image.png

旋转,ratate
image.png

4、namedtuple 生成只包含属性的类
image.png

5、OrderedDict 按内容增加的顺序存储
image.png

6、import array
array.array('c',"sdfdsfsfsdfs")
array.array('i',range(4))


image.png

7、heapq 二叉树堆
heapq内置模块位于./Anaconda3/Lib/heapq.py,提供基于堆的优先排序算法
堆的逻辑结构就是完全二叉树,并且二叉树中父节点的值小于等于该节点的所有子节点的值。这种实现可以使用 heap[k] <= heap[2k+1] 并且 heap[k] <= heap[2k+2] (其中 k 为索引,从 0 开始计数)的形式体现,对于堆来说,最小元素即为根元素 heap[0]。
1.初始化
可以通过 list 对 heap 进行初始化,或者通过 api 中的 heapify 将已知的 list 转化为 heap 对象。

  1. heapq.py中提供的函数方法
    heapq.heappush(heap, item)
    heapq.heappop(heap):返回 root 节点,即 heap 中最小的元素。
    heapq.heapreplace(heap,item): python3中heappushpop的更高效版。
    heapq.heappushpop(heap, item):向 heap 中加入 item 元素,并返回 heap 中最小元素。
    heapq.heapify(x):Transform list into a heap, in-place, in O(len(x)) time
    heapq.merge(*iterables, key=None, reverse=False)
    heapq.nlargest(n, iterable, key=None):返回可枚举对象中的 n 个最大值,并返回一个结果集 list,key 为对该结果集的操作。
    heapq.nsmallest(n, iterable, key=None):同上相反
    heapq._heappop_max(heap): Maxheap version of a heappop
    heapq._heapreplace_max(heap,item):Maxheap version of a heappop followed by a heappush.
    heapq._heapify_max(x):Transform list into a maxheap, in-place, in O(len(x)) time
    heapq._siftdown(heap,startpos,pos): Follow the path to the root, moving parents down until finding a place
    heapq._siftup(heap,pos):Bubble up the smaller child until hitting a leaf
    heapq._siftdown_max(heap,startpos,pos):Maxheap variant of _siftdown
    heapq._siftup_max(heap,pos):Maxheap variant of _siftup

import heapq
def heapsort(iterable):
h = []
for i in iterable:
heapq.heappush(h, i)
return [heapq.heappop(h) for i in range(len(h))]

method 1: sort to list

s = [3, 5, 1, 2, 4, 6, 0, 1]
print(heapsort(s))
'''
[0, 1, 1, 2, 3, 4, 5, 6]
'''

method 2: use key to find price_min

portfolio = [{'name': 'IBM', 'shares': 100, 'price': 91.1},
{'name': 'AAPL', 'shares': 50, 'price': 543.22},
{'name': 'FB', 'shares': 200, 'price': 21.09},
{'name': 'HPQ', 'shares': 35, 'price': 31.75},
{'name': 'YHOO', 'shares': 45, 'price': 16.35},
{'name': 'ACME', 'shares': 75, 'price': 115.65}]
cheap = heapq.nsmallest(1, portfolio, key=lambda s:s['price'])
print(cheap)
'''
[{'name': 'YHOO', 'shares': 45, 'price': 16.35}]
'''

method 3: use while to push min element

def heapilize_list(x):
n = len(x)
# 获取存在子节点的节点 index 列表,并对每个节点单元进行最小堆处理
for i in reversed(range(n // 2)):
raiseup_node(x, i)

def put_down_node(heap, startpos, pos):
current_item = heap[pos]
# 判断单元中最小子节点与父节点的大小
while pos > startpos:
parent_pos = (pos - 1) >> 1
parent_item = heap[parent_pos]
if current_item < parent_item:
heap[pos] = parent_item
pos = parent_pos
continue
break
heap[pos] = current_item

def raiseup_node(heap, pos):
heap_len = len(heap)
start_pos = pos
current_item = heap[pos]
left_child_pos = pos * 2 + 1
while left_child_pos < heap_len:
right_child_pos = left_child_pos + 1
# 将这个单元中的最小子节点元素与父节点元素进行位置调换
if right_child_pos < heap_len and not heap[left_child_pos] < heap[right_child_pos]:
left_child_pos = right_child_pos
heap[pos] = heap[left_child_pos]
pos = left_child_pos
left_child_pos = pos * 2 + 1
heap[pos] = current_item
put_down_node(heap, start_pos, pos)

p = [4, 6, 2, 10, 1]
heapilize_list(p)
print(p)
'''
[1, 4, 2, 10, 6]
'''

8、 bisect有序列表
bisect.bisect(list,5)
bisect.insort()

image.png

9、queue
import Queue
Queue.queue()
q.put()
q.empty()
q.get()
10、LifoQueue
Queue.LifoQueue()
q.put()
q.empty()
q.get()
11、PriorityQueue() 优先级由你定
image.png

12、struct结构
image.png

image.png

缓存区:pack_into??
image.png

image.png

13、对象的非永久引用:weakref
weakref.ref 弱引用
image.png

引用回调,删除时被调用
image.png

weakref.WeakValueDictionary??
14、copy
copy.copy(obj)
copy.deepcopy() 连里面的所有元素都是新的
copy自己实现复制
image.png

15 pprint

-- coding: cp936 --

python 27

xiaodeng

python之模块pprint之常见用法

import pprint
data = [(1,{'a':'A','b':'B','c':'C','d':'D'}),(2,{'e':'E','f':'F','g':'G','h':'H','i':'I','j':'J','k':'K','l':'L'}),]
print '--'*30

1、打印效果

pprint.pprint (data)
'''


[(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),
(2,
{'e': 'E',
'f': 'F',
'g': 'G',
'h': 'H',
'i': 'I',
'j': 'J',
'k': 'K',
'l': 'L'})]
'''

2、格式化

data = [(1,{'a':'A','b':'B','c':'C','d':'D'}),(2,{'e':'E','f':'F','g':'G','h':'H','i':'I','j':'J','k':'K','l':'L'}),]
result=pprint.pformat(data)
for key in result.splitlines():
print key

'''
[(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),
(2,
{'e': 'E',
'f': 'F',
'g': 'G',
'h': 'H',
'i': 'I',
'j': 'J',
'k': 'K',
'l': 'L'})]
'''

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,163评论 0 10
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 6,164评论 0 2
  • 在C语言中,五种基本数据类型存储空间长度的排列顺序是: A)char B)char=int<=float C)ch...
    夏天再来阅读 8,786评论 0 2
  • 东平农商银行金融精准扶贫工作纪实 v.qq.com/x/page/q0509k0p4rb.html
    顽石不恭阅读 1,079评论 0 0
  • 一位白发苍苍的老大爷步履蹒跚地往这边挪过来。他右手拄着拐杖(一根带四个脚的拐杖),左手提着一个透明的塑料袋,战...
    鱼真的好笨阅读 1,124评论 0 1