数据结构和算法 解包赋值
p = [1, 2, 3]
a, b, c = p# _表示被丢弃的值
_, d, _ = p# 可变长解包
*a, b = p# 字串切割解包
line = 'nobody:hello:world:123:/dirname'
a, *_, c, d = line.split(':')
保留最后N个元素
from collections import dequep = list(range(10))
d = deque(maxlen=3)def keep_history():for i in p:yield id.append(i)
查找最大或最小的N个元素
适合在大集合中查找少量元素;若N大小与集合大小接近,直接排序后切片;若找唯一最大最小值,直接用max/min方法。
import heapqnums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
print(heapq.nlargest(3, nums)) # Prints [42, 37, 23]
print(heapq.nsmallest(3, nums)) # Prints [-4, 1, 2]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}
]
# 通过关键字参数排序然后取出N个值
cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price'])
expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])
字典键映射多个值
from collections import defaultdictd = defaultdict(list)d['a'].append(1)
d['a'].append(2)
d['b'].append(3)
字典排序
from collections import OrderedDictd = OrderedDict()
d['a'] = 1
d['b'] = 2
d['c'] = 3
字典运算
最大值、最小值、排序,用zip将键值反转。
prices = {
'ACME': 45.23,
'AAPL': 612.78,
'IBM': 205.55,
'HPQ': 37.20,
'FB': 10.75
}
min_price = min(zip(prices.values(), prices.keys()))max_price = max(zip(prices.values(), prices.keys()))prices_sorted = sorted(zip(prices.values(), prices.keys()))
zip函数创建的是只能访问一次的迭代器。
查找两字典的相同元素
交并差集运算。
a = {
'x' : 1,
'y' : 2,
'z' : 3
}
b = {
'w' : 10,
'x' : 11,
'y' : 2
}# Find keys in common
a.keys() & b.keys() # { 'x', 'y' }
# Find keys in a that are not in b
a.keys() - b.keys() # { 'z' }
# Find (key,value) pairs in common
a.items() & b.items() # { ('y', 2) }
删除相同元素并保持顺序
# 元素可哈希,如列表元组
def dedupe(items):seen = set()for item in items:if item not in seen:yield itemseen.add(item)# 元素不可哈希,如字典
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)
list(dedupe(a, key=lambda d: (d['x'],d['y'])))
命名切片
通过slice方法将切片赋值到变量,然后调用变量。
SHARES = slice(20, 23)
PRICE = slice(31, 37)
cost = int(record[SHARES]) * float(record[PRICE])
序列中出现次数最多的元素
from collections import Counterwords = [
'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
'my', 'eyes', "you're", 'under'
]
word_counts = Counter(words)
top_three = word_counts.most_common(3)
通过关键字排序一个字典列表
from operator import itemgetterrows = [
{'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
{'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
{'fname': 'John', 'lname': 'Cleese', 'uid': 1001},
{'fname': 'Big', 'lname': 'Jones', 'uid': 1004}
]rows_by_fname = sorted(rows, key=itemgetter('fname'))
rows_by_uid = sorted(rows, key=itemgetter('uid'))
# 多个关键字
rows_by_lfname = sorted(rows, key=itemgetter('lname','fname'))# 支持下标索引
itemgetter(0, 1)
排序不支持原生比较的对象
内置sorted方法的关键字参数key接受一个可调用对象,利用函数返回一个值用于比较。
class User:def __init__(self, user_id):self.user_id = user_iddef __repr__(self):return 'User({})'.format(self.user_id)def sort_notcompare():users = [User(23), User(3), User(99)]print(users)print(sorted(users, key=lambda u: u.user_id))
或通过operator的attrgetter方法替代这个函数,并且通常这个方法运行得快一点。
from operator import attrgettersorted(users, key=attrgetter('user_id'))
合并多个字典
从逻辑上合并然后执行操作,而不是物理上的合并。
from collections import ChainMapa = {'x': 1, 'z': 3 }
b = {'y': 2, 'z': 4 }c = ChainMap(a,b)
print(c['x']) # Outputs 1 (from a)
print(c['y']) # Outputs 2 (from b)
print(c['z']) # Outputs 3 (from a)
对新字典的更新或删除操作总是影响第一个字典。