python 常用库

tech2025-07-02  2

1. random 产生随机数

import random random.choice(list1) 从list中随机返回一个数 random.random() 产生0 - 1 的随机数 random.uniform(a, b) 产生 a- b 之间的随机数

 

2. Deque

import collections dq = collections.deque() dq.append() dq.appendleft() dq.pop() dq.popleft()

 

3.Heap

import heapq heapq.heappush(queue, item) heapq.heappop(queue) heapq.heapift(list)

 

4. Comparator

import functools #define the node class class Node: def __init__(self, value): self.val = value #define the comparator def cmp(A, B): return A.val - B.val #使用库函数 cmp_to_key list1.sort(key = functools.cmp_to_key(cmp))

 

最新回复(0)