Python brush title Practical tips collection (continually updated)

Some common techniques python, very easy to learn some basic functions of
a strong push
9. The whole arrangement
12. The XOR
15. exchange matrix row
@ (Content Navigation :)


Hash table definition:

dict = {}, python known in the dictionary, the search key value

  1. Must correspond to the key value and, in pairs,
  2. Instead of the value of the key will be repeated subsequent occurrence

Determines whether or not in the dictionary, a keyword may be used in combination, for example:
IF A dict in:

Detailed reference https://www.cnblogs.com/scios/p/8108243.html


map function

A function used to map to a set up

def f(x):
    return x*x
print map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])

结果是:[1, 4, 9, 10, 25, 36, 49, 64, 81]

Detailed reference https://www.cnblogs.com/lincappu/p/8179475.html


ord and chr

ord for obtaining single character ASCII value of
the character chr for obtaining values corresponding to the ASCII

>>> ord('d')
100

>>> chr(100)
'd'

Detailed reference https://blog.csdn.net/hk_john/article/details/77990585


32-byte string to limit the scope of the digital code output line

return max(-2 ** 31, min(sign * ret, 2 ** 31 - 1))

Transpose (using zip line of code) two-dimensional matrix

return list(map(list,zip(*array)))

Fast initialization two-dimensional matrix

# dim1, dim2是二维矩阵的第一第二维度,初始化为0,改成其他数则全为其他数
aarray = [ [0] * dim1 for i in range(dim2)]

Quick list of two-dimensional matrix

# 第一种:导入外部的包
from itertools import chain
list(chain(*res))

# 第二种:二重循环
>>>ab = [[1,2,3], [5,8], [7,8,9]]
>>>print([i for item in ab for i in item])
[1, 2, 3, 5, 8, 7, 8, 9]

The default implementation of two-way queue

# 好处是多了leftappend这个属性,2019年腾讯提前批笔试就考察了这个数据结构
import collections
d = collections.deque()

Fast full array list

I am going to thank byte beating good buddy child to practice good stuff, very practical, put his blog address specific you can feel the full array of various companies at the time of interview questions require brute convenience, the proposed mark

# 给出list:123,返回这三个数的全排列
# list和map的组合用法是将内部的tuple变成数组形式方便使用
>>>import itertools
>>>result = list(map(list, itertools.permutations([1,2,3])))
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]

Find the maximum value of the bond dictionary

# 第一种,直接返回
res = max(d,key=d.get)
# 第二种,对字典按照键排序
# 这里的d.items()实际上是将d转换为可迭代对象
# items()方法将字典的元素 转化为了元组
# 而这里key参数对应的lambda表达式的意思则是选取元组中的第二个元素作为比较参数
# lambda x:y中x表示输出参数,y表示lambda 函数的返回值
d = sorted(d.items(), key=lambda item: item[1])
res = d[-1][0]

The number of numbers appear in the statistics list

# most_common(列出前n个元素,不指定参数,则列出所有)
>>>numbers = [1,2,3,2,4,2,5,2,3]
>>>from collections import Counter
>>>count = Counter(numbers).most_common()
[(2, 4), (3, 2), (1, 1), (4, 1), (5, 1)]

Some show XOR operations

1. Basic properties

0 ^ n = n
n ^ n = 0

2. Three XOR swap two numbers

>>>a, b = 1, 2
>>>a = a ^ b
>>>b = a ^ b
>>>a = a ^ b
>>>print a, b
2 1

Understanding of recursion (known almost reproduced)

I saw dictionary metaphor fine. Imagine using a dictionary look up the words in plain English, to a beautiful woman meaning of a word, when the word turn, see explanation, interpretation found in a word do not know, therefore, can not understand the words you want to check what is meant; this when, then this dictionary (function itself) to check that word does not know, and explain the investigation found the first two words in there did not know a word, then, and then check the dictionary first three do not know words, so one by one investigation continues until all the words to explain all know, so that in the end, the last word will understand what it means, then pour back layer by layer, I knew I initially wanted to check first What is the meaning of the words, the problem is solved.

Author: Lee oo
link: https: //www.zhihu.com/question/31412436/answer/77766824
Source: know almost
copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.


filter filters out portions of elements in the array

def is_odd(n):
    return n % 2 == 1
 
newlist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(newlist)
# 输出:1,3,5,7,9

The ranks of two-dimensional matrix of exchange, additions and deletions

Numpy arrays can be easily implemented by means of the function to avoid the complicated operation,
in order to facilitate the subsequent operations, sub-method recommended .ToList () to convert ndarray type ist

import numpy as np
array = np.array([[1,2,3], [4,5,6],[7,8,9]])
# 交换1,3列
array = array[:,[2,1,0]]
# 增加一行或一列
array = np.row_stack((array, [7,8,9]))
array = np.column_stack((array, [7,8,9]))

In addition to the relationship right one and two operations

Briefly summarize:
It should be said to be non-negative except two is rounded down, the second is the integer division rounding.
Both equivalent most cases
enlightenment address
detailed proof of address

Guess you like

Origin www.cnblogs.com/kouin/p/11455721.html