python3 cookbook 学习笔记-数据结构和算法(未完待续)

数据结构和算法

  1. 将序列分解成单独的变量
can_iter_list = [1,2,3,4,5]
can_iter_str = "hansz"

can_iter = can_iter_str
a,b,c,d,e = can_iter
a,_,b,_,c = can_iter
# 只要是可迭代的对象都可以分解,包括生成器和迭代器等
  1. 从任意长度的可迭代对象中分解元素
can_iter_list = [1,2,3,4,5]
can_iter_str = "hansz"

can_iter = can_iter_str
a,*b = can_iter

record = ("acme",50,11,22,(12,11,2012))
name,*_,(*_,year) = record
  1. 保存最后N个元素
from collections import deque
my_deque = deque(maxlen=5)
my_deque.append(1)
# my_deque 最多只能存储5个元素
  1. 找到最大或最小的N各元素

import heapq

a = [1,4,5,542,342,423,1,653,4]
heapq.nlargest(3,a)
heapq.nsmallest(4,a)

你可能感兴趣的:(python)