Writing in the front

Today’s review of the Python Cookbook is in the form of a question-and-answer pair, a note I looked at a long time ago. The Cookbook is more like a tool than a primer. There is no need to read it one by one since you have the basics. My advice is to turn over the knowledge and skills in that section if you need to use it. Below big karma oneself check a leak to fill a vacancy!

Chap1 Data structure and algorithm

Decompose elements from an iterable of any length

* Expressions can be used to decompose a data structure type with N elements into the desired parts. For example, the grades store 100 grades and we only care about the first and last grades, we could store all middle grades in a list like this:

first, *middle, last = grades
Copy the code
Save the last N elements
  • Collection.deque (maxlen=N) creates a fixed-length queue that automatically removes the oldest record when new records are added and the queue is full.
  • If you do not specify the size of the queue, you have an unbounded queue;
  • A deque supports adding and popping elements from both ends of the queue
from collection import deque
q = deque()
q.append(1)
q.append(2)
q.append(3)
q.appendleft(4)
q.pop()
q.popleft()
Copy the code
Find the largest or smallest N elements
  • Heapq contains two functions: nlargest() and nsmallest()
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]
Copy the code
  • Both functions can also take a key argument
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}]
Copy the code

Keep the dictionary in order

  • The OrderedDict of the Collection module operates in the order in which elements were originally added;
  • It maintains a bidirectional linked list that arranges key positions according to the order in which elements are added. OrderedDict is therefore more than twice the size of a regular dictionary.

Dictionary calculation problem

  • Use zip() to reverse the keys and values of the dictionary

Find the element that occurs most often in the sequence

  • The Counter class of the Collection module
  • And the Counter class has a very handy most_common(n) method that directly gets the top few occurrences
from collections importWord_counts = Counter(words) top_3 = word_counts. Most_common (3)
Copy the code

Sort dictionary lists by public keys

  • Itermgetter function for the operator module
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]: <operator.itemgetter at 0x7f01606657d0>

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}]
Copy the code
  • The itermgetter() function can also accept multiple keys
rows_by_frame = sorted(rows, key=itemgetter('fname'.'lname'))
Copy the code

Chap3 Numbers, dates, and times

The value is rounded

  • Use the built-in round(value, ndigits) function
>>> round(1.23.1)
1.2
>>> round(1.27.1)
1.3
>>> round(162773.- 1)
162770
Copy the code
  • When a value is exactly halfway between two integers, the integer operation goes to the nearest even value. For example, 1.5 and 2.5 are rounded to 2
  • Ndigits in round() can be negative, in which case round to tens, hundreds…

Format the output of the value

  • Use the built-in format() function
>>>x = 123.456
>>>format(x, '0.2 f')
123.46
Copy the code

Binary, octal and hexadecimal conversions

  • To convert an integer to binary, use bin()
  • To convert an integer to octal, use oct()
  • To convert an integer to hexadecimal, use hex()

Random selection

  • Random.choice () can pick elements at random from a sequence
>>>import random
>>>values = [1.2.3.4.5.6]
>>>random.choice(values)
4
>>>random.choice(values)
2
Copy the code
  • Random.shuffle () can shuffle the order of elements in a sequence
>>>random.shuffle(values)
>>>values
[2.4.3.1.6.5]
Copy the code
  • Random. Sample () can sample N elements
>>>random.sample(values, 2)
[6.2]
Copy the code

Time conversion

  • The Datatime module can be used to convert between different time units. For example, to represent a time interval, you can create a Timedelta instance
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
Copy the code

Chap4 Iterators and generators

Manually access elements in iterators

with open('/etc/passwd') as f:
    try:
        while True:
            line = next(f)
            print(line, end=' ')
    except StopIteration:
        pass
Copy the code

Entrust the iteration

  • For a custom container object that holds a list, tuple, or other iterable inside it, we want our new container to be able to iterate. In general, all we need to do is define an __iter__() method that delegates iteration requests to a container held inside the object.
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)
Copy the code

In this example, the iter() method forwards the iteration request to the _children property held internally by the object.

Create new iteration patterns with generators

  • Any occurrence of a yield statement in a function turns it into a generator
def frange(start, stop, increment):
    x = start
    while x < stop:
        yield x
        x += increment
Copy the code
  • Note that the generator runs only in response to an iteration operation

Slice the iterator

  • Itertool.islice () slicing iterators and generators
In [3]: def count(n): ... : while True: ... : yield n ... : n +=1. : In [5]: c = count(0)
In [6]: c
Out[6]: <generator object count at 0x7f92899b3c80>
----> 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
Copy the code

Skip the previous part of the iterable

  • The itertools.dropwhile() function discards the first few elements of the sequence. For example, if we need to read a file that begins with a series of comment lines we don’t want
from itertools import dropwhile
with open('/etc/passwd') as f:
    for line in dropwhile(lambda line: line,startwith(The '#'), f):
        print(line, end=' ')
Copy the code

Iterate over all possible combinations

  • We want to iterate over all possible combinations of a list of elements
  • The itrtools.permutations() function takes a set of elements, rearranges all of them into all possible scenarios, and returns them as tuples.
In [11]: from itertools import permutations
In [12]: items = ['a'.'b'.'c']
In [13] :forp in permutations(items): ... :print(p) ... : ('a'.'b'.'c')
('a'.'c'.'b')
('b'.'a'.'c')
('b'.'c'.'a')
('c'.'a'.'b')
('c'.'b'.'a'If you want shorter permutations, you can specify the length In [14] :forP in permutations (items,2) :... :print(p)
('a'.'b')
('a'.'c')
('b'.'a')
('b'.'c')
('c'.'a')
('c'.'b')
Copy the code
  • Itertools.com binations takes no account of the actual order between elements, i.e. (‘a’, ‘b’) and (‘b’, ‘a’) are considered to be the same combination.
  • To remove this restriction, you can use combinationS_with_replacement.

Iterate over multiple sequences simultaneously

  • The zip() function can be used to iterate over elements in multiple sequences
>>>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
Copy the code

Iterate through different containers

  • We need to perform the same operation on many objects, but these objects are contained in different containers, and we want to avoid writing nested loop processing and keep the code readable. The itertools.chain() method can be used to simplify this task.
from itertools import chain

In [18]: a = [1.2.3.4]
In [19]: b = ['x'.'y'.'z']
In [20] :forx in chain(a, b): ... :print(x) ... :1
2
3
4
x
y
z
Copy the code

Merge multiple ordered sequences and iterate over the entire ordered sequence

  • Heapq. Merge () function
>>>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
Copy the code

Chap 5 file and I/O

Redirect output to a file

  • Simply add the file keyword argument to the print() function
with open('somefile.txt'.'rt') as f:
    print('Hello World! ', file=f)
Copy the code

Print with a different delimiter or line end character

>>>print('GKY'.1995.5.18, sep=The '-',end='!!!!! \n')
GKY- 1995.- 518!!!!!Copy the code

Performs IO operations on a string

  • Use io.stringio () and io.byteio () to create file-like objects that manipulate string data.

Read and write compressed data files

  • Gzip and BZ2 modules can do this
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)
Copy the code

Read binary data into a mutable buffer

  • We want to read binary data directly into a mutable buffer without any copying. For example, we want to modify the data in place and write it back to the file.
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')
Copy the code

Serialize Python objects

  • We need to serialize Python objects into byte streams so that we can save them to a file, store them in a database, or transfer them over a network connection.
  • The most common way to serialize data is to use the pickle module.
import pickle
data = ...  #some python object
f = open('somefile'.'wb')
pickle.dump(data,f)
Copy the code
  • To dump an object as a string, use the
import pickle
data = ...  #some python object
f = open('somefile'.'wb')
pickle.dumps(data,f)
Copy the code
  • To recreate an object from a byte stream, use pickle.load() or pickle.loads()

Chap 6 data encoding and processing

Reading and writing JSON data

  • Mainly using JSON modules
  • The two main functions are json.dumps() and json.loads()
  • Use json.dump() and json.load() for files instead of strings

Parsing simple XML documents

  • Xml.etree.ElementTree can extract data from simple XML documents
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()
   ....:
Copy the code

“`php

Highlights of past For beginners entry route of artificial intelligence and data download AI based machine learning online manual deep learning online manual download update (PDF to 25 sets) note: WeChat group or qq group to join this site, please reply “add group” to get a sale standing knowledge star coupons, please reply “planet” knowledge like articles, point in watching

Copy the code