“*表达式”可以用来将一个含有N个元素的数据结构类型分解成所需的几部分。
例如grades保存了100个成绩数据而我们只关心首末两个成绩,就可以把中间的所有成绩保存到一个列表里面,如下:
first, *middle, last = grades
from collection import deque
q = deque()
q.append(1)
q.append(2)
q.append(3)
q.appendleft(4)
q.pop()
q.popleft()
import heapq
nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
print(heapq.nlargest(3, nums))
out: [42, 37, 23]
print(heapq.nsmallest(3,nums))
out: [-4, 1, 2]
In [1]: 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(3, portfolio, key=lambda s: s['price'])
cheap
out:
[{'name': 'YHOO', 'price': 16.35, 'shares': 45},
{'name': 'FB', 'price': 21.09, 'shares': 200},
{'name': 'HPQ', 'price': 31.75, 'shares': 35}]
from collections import Counter
words = [一系列单词组成的列表]
word_counts = Counter(words)
top_3 = word_counts.most_common(3)
from operator import itemgetter
In [26]: rows = [
...: {'fname': 'Brian', 'lname': 'Jones', 'uid':1003},
...: {'fname': 'David', 'lname': 'Beazley', 'uid':1002},
...: {'fname': 'John', 'lname': 'Cleese', 'uid':1001},
...: {'fname': 'Big', 'lname': 'Jones', 'uid':1004}
...: ]
itemgetter('fname')
Out[31]:
rows_by_frame = sorted(rows, key=itemgetter('fname'))
Out[30]:
[{'fname': 'Big', 'lname': 'Jones', 'uid': 1004},
{'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
{'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
{'fname': 'John', 'lname': 'Cleese', 'uid': 1001}]
rows_by_frame = sorted(rows, key=itemgetter('fname','lname'))
>>> round(1.23, 1)
1.2
>>> round(1.27, 1)
1.3
>>> round(162773, -1)
162770
>>>x = 123.456
>>>format(x, '0.2f')
123.46
>>>import random
>>>values = [1,2,3,4,5,6]
>>>random.choice(values)
4
>>>random.choice(values)
2
>>>random.shuffle(values)
>>>values
[2,4,3,1,6,5]
>>>random.sample(values, 2)
[6, 2]
from datetime import timedelta
In [33]: a = timedelta(days=2, hours=6)
In [34]: b = timedelta(hours=4.5)
In [35]: c = a + b
In [36]: c.days
Out[36]: 2
In [37]: c.seconds
Out[37]: 37800
In [38]: c.seconds/3600
Out[38]: 10.5
In [39]: c.total_seconds() / 3600
Out[39]: 58.5
with open('/etc/passwd') as f:
try:
while True:
line = next(f)
print(line, end='')
except StopIteration:
pass
class Node:
def __init__(self, value):
Self._value = vaule
self._children = []
def __repr__(self):
return 'Node({!r})'.format(self._value)
def __iter__(self):
return iter(self._children)
在这个例子中,iter()方法将迭代请求转发给对象内部持有的_children属性上。
def frange(start, stop, increment):
x = start
while x < stop:
yield x
x += increment
In [3]: def count(n):
...: while True:
...: yield n
...: n += 1
...:
In [5]: c = count(0)
In [6]: c
Out[6]:
----> 1 c[0]
TypeError: 'generator' object has no attribute '__getitem__'
import itertools
In [10]: for x in itertools.islice(c, 10, 20):
...: print(x)
10
11
12
13
14
15
16
17
18
19
from itertools import dropwhile
with open('/etc/passwd') as f:
for line in dropwhile(lambda line: line,startwith('#'), f):
print(line, end='')
In [11]: from itertools import permutations
In [12]: items = ['a', 'b', 'c']
In [13]: for p in permutations(items):
...: print(p)
...:
('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')
#如果想得到较短的所有全排列,可以指定长度
In [14]: for p in permutations(items, 2):
...: print(p)
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'c')
('c', 'a')
('c', 'b')
>>>xvalues = [1,5,4,2,10,7]
>>> yvalues = [101,78,37,15,62,99]
>>> for x, y in zip(xvalues, yvalues):
... print(x, y)
...
1 101
5 78
4 37
2 15
10 62
7 99
from itertools import chain
In [18]: a = [1, 2, 3, 4]
In [19]: b = ['x', 'y', 'z']
In [20]: for x in chain(a, b):
...: print (x)
...:
1
2
3
4
x
y
z
>>>import heapq
>>>a = [1,4,7,10]
>>>b = [2,5,6,11]
>>>for c in heapq.merge(a,b):
... print(c)
...
1
2
4
5
6
7
10
11
with open('somefile.txt', 'rt') as f:
print('Hello World!', file=f)
>>>print('GKY',1995,5,18, sep='-',end='!!\n')
GKY-1995-5-18!!
import gzip
with open('somefile.gz', 'rt') as f:
text = f.read()
import bz2
with open('somefile.bz2', 'rt') as f:
text = f.read()
import gzip
with open('somefile.gz', 'wt') as f:
f.write(text)
import bz2
with open('somefile.bz', 'wt') as f:
f.write(text)
import os.path
def read_into_buffer(filename):
buf = bytearray(os.path.getsize(filename))
with open(filename, 'rb') as f:
f.readinto(buf)
return buf
with open('sample.bin', 'wb') as f:
f.write(b'hello world')
buf = read_into_buffer('sample.bin')
In [16]: buf
Out[16]: bytearray(b'hello world')
import pickle
data = ... #some python object
f = open('somefile', 'wb')
pickle.dump(data,f)
import pickle
data = ... #some python object
f = open('somefile', 'wb')
pickle.dumps(data,f)
from urllib.request import urlopen
from xml.etree.ElementTree import parse
u = urlopen('http://planet.python.org/rss20.xml')
doc = parse(u)
In [24]: for item in doc.iterfind('channel/item'):
....: title = item.findtext('title')
....: date = item.findtext('pubDate')
....: link = item.findtext('link')
....: print (title)
....: print(date)
....: print(link)
....: print()
....: