0%

Python 中常用的代码段

记录一些我经常查找的 python 方法作为备忘

Generate random int list, or just a requirement of loop N times

it’s a common requirement and some guys achieve this goal by using Numpy lib, but it’s too heavy. you can do in this way:

1
2
3
4
5
6
import random

for _ in range(10)
print(random.randint(0, 100))

# the _ is from 0 - 9

Get index and val at the same time

1
2
3
4
5
6
7
8
9
10
a = ['a','b','c','d']
for idx, val in enumerate(a):
print(f'idx = {idx}, val = {val}')


# Output:
# idx = 0, val = a
# idx = 1, val = b
# idx = 2, val = c
# idx = 3, val = d

if you want to specify the start index, you can add a second parameter to enumerate func

1
2
3
4
5
6
7
8
9
10
11

# in this case, idx would start from 3
a = ['a','b','c','d']
for idx, val in enumerate(a, 3):
print(f'idx = {idx}, val = {val}')

# Output:
# idx = 3, val = a
# idx = 4, val = b
# idx = 5, val = c
# idx = 6, val = d

Ipython 交互界面重新引入修改后的包

1
2
import importlib
importlib.reload(some_module)

for loop one line mode

1
2
3
4
5
user_ids = [record['login'] for record in resp]

# if you need if condition
list = [1,2,3,4,5,6]
filter = [str(sub + "tt") for sub in list if sub >= 3]

repr Vs str

  • 只重写 str 只定制在 print() 时的输出
  • 只重写 repr print() 和 调用都输出定制内容
  • 重写 str + repr print() 输出 str 定制内容,调用输出 repr 内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class N1:
def __init__(self, data):
self.data = data

def __str__(self):
return 'N1: data=%s' % self.data

class N2:
def __init__(self, data):
self.data = data

def __repr__(self):
return 'N2: data=%s' % self.data

class N3:
def __init__(self, data):
self.data = data

def __repr__(self):
return 'N3 repr: data=%s' % self.data

def __str__(self):
return 'N3 str: data=%s' % self.data

'''output
n1 = N1(1)
# In [30]: n1
# Out[30]: <BinaryTree.N1 at 0x10853fd30>

print(n1)
# N1: data=1

n2 = N2(2)
# In [33]: n2
# Out[33]: N2: data=2

print(n2)
# N2: data=2

n3 = N3(3)
# Out[36]: N3 repr: data=3

print(n3)
# N3 str: data=3
'''

How to print in string formant

1
2
3
'{{ Test-{} }}'.format('output')

# output: { Test-output }

遍历子目录

1
2
3
4
5
6
7
8
9
10
import os

for root, dirs, files in os.walk('.'):
for sub in files:
print('name: %s' %(os.path.join(root, sub)))

# 或者也可以使用 glob
import glob

glob.glob('./**/*.png', recursive=True)