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]