您的位置:首页 > 编程语言 > Python开发

python cookbook 3rd

2017-04-28 14:59 232 查看
# coding=utf-8
# 去掉第一名,最后一名,求平均值
def avg(rest):
return sum(rest) / rest.__len__()
def drop_first_last(grades):
first, *middle, last = grades
return avg(middle)
print (drop_first_last((99,2,3,4,5,6,7,8,1)))

# 星号表达式在迭代元素为可变长元组的序列时是很有用的
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 tag, *args in records:
if tag == 'foo':
do_foo(*args)
elif tag == 'bar':
do_bar(*args)

# 星号解压语法在字符串操作的时候也会很有用,比如字符串的分割
uname, *fields, homedir, sh = 'nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false'.split(':')
head, *tail = [1, 10, 7, 4, 5, 9]
print(uname,homedir,sh)

# 解压一些元素后丢弃它们,你不能简单就使用 * ,但是你可以使用一个普通的废弃名称,比如 或者 ign
record = ('ACME', 50, 123.45, (12, 18, 2012))
name, *_, (*_, year) = record
print(name, year)

# 在多行上面做简单的文本匹配,并返回匹配所在行的前 N 行
# 保留有限历史记录正是 collections.deque 大显身手的时候
# deque 可以一直append, 大于5个后的append会挤出第一条
from collections import deque
def search(lines, pattern, history=5):
previous_lines = deque(maxlen=history)
for li in lines:
if pattern in li:
yield li, previous_lines
previous_lines.append(li)

with open(r'test_api.py') as f:
for line, prevlines in search(f, 'app_context', 5):
for pline in prevlines:
print(pline,)
print(line,)
print('-' * 20)

# 一个集合中获得最大或者最小的 N 个元素列表?
# heapq 模块有两个函数:nlargest() 和 nsmallest() 可以完美解决这个问题
# 两个函数都能接受一个关键字参数,用于更复杂的数据结构中:
import heapq
nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
heapq.heapify(nums) #底层实现里面,首先会先将集合数据进行堆排序后放入一个列表中
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}
]
print(heapq.nsmallest(3, portfolio, key=lambda s: s['price']))
print(heapq.nlargest(3, portfolio, key=lambda s: s['price']))

# 优先级heapq队列
import heapq
class PriorityQueue:
def __init__(self):
self._queue = []
self._index = 0

def push(self, item, priority):
heapq.heappush(self._queue, (-priority, self._index, item))
self._index += 1

def pop(self):
return heapq.heappop(self._queue)[-1]

class Item:
def __init__(self, name):
self.name = name
def __repr__(self):
return 'Item({!r})'.format(self.name)

q = PriorityQueue()
q.push(Item('foo'), 1)
q.push(Item('bar'), 5)
q.push(Item('spam'), 4)
q.push(Item('grok'), 1)
print(q.pop())
print(q.pop())
print(q.pop())
print(q.pop())

# 字典中的键映射多个值,
# 使用 collections 模块中的 defaultdict 来构造这样的字典,
# 选择使用列表还是集合取决于你的实际需求。如果你想保持元素的插入顺序就应该
# 使用列表,如果想去掉重复元素就使用集合(并且不关心元素的顺序问题)。
from collections import defaultdict
d = defaultdict(list)
d['a'].append(1)
d['a'].append(2)
d['a'].append(2)
d['b'].append(4)
dd = defaultdict(set)
dd['a'].add(1)
dd['a'].add(2)
dd['a'].add(2)
dd['b'].add(4)
print(d, dd)

# 字典的运算 (比如求最小值、最大值、排序等等)?
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()))
min(prices.values()) # Returns 10.75
max(prices.values()) # Returns 612.78

#  查找两字典的相同点
# 以现有字典构造一个排除几个指定键的新字典
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) }
# Make a new dictionary with certain keys removed
c = {key:a[key] for key in a.keys() - {'z', 'w'}} # c is {'x': 1, 'y': 2}

# 删除序列相同元素并保持顺序

def dedupe(items):
seen = set()
for item in items:
if item not in seen:
yield item
seen.add(item)
# dict 类型
def dedupefordict(items, key=None):
seen = set()
for item in items:
val = item if key is None else key(item)
if val not in seen:
yield item
seen.add(val)
a = [1, 5, 2, 1, 9, 1, 5, 10]
b = [{'x': 1, 'y': 2}, {'x': 1, 'y': 3}, {'x': 1, 'y': 2}, {'x': 2, 'y': 4}]
print(set(a)) # 生成的结果中的元素位置被打乱,以下方式可以解决
print(list(dedupe(a)))
print(list(dedupefordict(b, key=lambda d: (d['x'], d['y']))))
print(list(dedupefordict(b, key=lambda d: d['x'])))

# 1.12序列中出现次数最多的元素,Counter 对象实际就是一个字典,将元素映射到它出现的次数上
from collections import Counter
words = [
'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'
]
morewords = ['why', 'are', 'you', 'not', 'looking', 'in', 'my', 'eyes']
word_counts = Counter(words)
word_counts.update(morewords)
top_three = word_counts.most_common(3) # 出现频率最高的 3 个单词
print(top_three) # Outputs [('eyes', 8), ('the', 5), ('look', 4)]
print(word_counts['not']) # 1
print(word_counts['eyes']) # 8

# 1.13 通过某个关键字排序一个字典列表
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐