头两个用random库,后四个用OS 和datetime库,能不能帮助一下,谢谢了
1
import random
persons=[(111,'张三',False),
(222,'李四',True),
(333,'王五',False),
(444,'钱六',False),
(555,'赵大',False)]
l=len(persons)
lst=[]
max=3
kv={}
while max>0:
index=random.randint(0,l-1)
if not persons[index][2] and index not in kv:
lst.append(persons[index])
kv[index]=True
max-=1
print(lst)
2
def getPassword(n=10,numProb=.2):
chrs=list("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
weights=[]
chrProb=1-numProb
for c in chrs:
if c.isnumeric():
weights.append(numProb)
else:
weights.append(chrProb)
return ''.join(random.choices(chrs,weights=weights,k=n))
print(getPassword())
3
import os
import datetime
d=os.getenv('SystemDrive')
user=os.getlogin()
path=r'{}\users\{}\Desktop'.format(d,user)
path=os.path.join(path,datetime.datetime.now().strftime('%Y%m%d-%H%M%S.%f'))
os.mkdir(path)
from datetime import date
def lefttDays(dateFrom,dateTo):
return (dateTo-dateFrom).days
newYearDay=date(date.today().year+1,1,1)
daysLeft=lefttDays(date.today(),newYearDay)
print('距离元旦还有{}天'.format(daysLeft))
4
import os
def removeFilesByExt(dir,exts,recusion=True):
for root,dirs,files in os.walk(dir):
for file in files:
ext="."+file.lower().split('.')[-1]
if ext in exts:
os.remove(os.path.join(root,file))
if not recusion:
break
5
import os
import shutil
def archivve(dir):
for root,dirs,files in os.walk(dir):
for file in files:
d=os.path.join(dir,file.split('_')[0])
if not os.path.exists(d):
os.mkdir(d)
shutil.move(os.path.join(root,file),os.path.join(d,file))
break
帮忙看看咋做呗
函数 | 说明 |
---|---|
time() | 返回当前时间戳 |
sleep() | 线程推迟指定时间运行,单位秒 |
localtime() | 有一个时间戳转换成但钱地区的struct_time,参数未提供时,以当前时间为准 |
gmtime() | 和localtime方法类似,gmtime()将一个时间戳转换为UTC时区的struct_time |
mktime() | 将一个struct_time转化为时间戳,从(1970.1.1的0点)起点 eg:time.mktime(time.localtime()) |
asctime([t]) | 把表示时间的元组或struct_time表示为这种形式“Sun Jun 20 23:01:05 2016” 如果没有参数,将time.localtime()作为参数传入 |
strftime(format[ , t] ) | 将一个表示时间的元组和或struct_time(由gmtime()和localtime()函数返回)转化为时间字符串,如果 t 未指定,将localtime()传入 |
ctime([secs]) | 把一个时间戳(按秒计算浮点数)转化为time。asctime()的形式,如果参数未给,将time.time()传入 |
strptime(string[ , format]) | 把一个格式化时间字符串转化成为struct_time。实际上它是strftime逆过程 |
perf_counter() | 返回性能计数器的值,以秒为单位 |
process_time() | 返回当前进程使用CPU的时间,以秒为单位 |
timezone() | 返回本地时区的时间偏移,以秒为单位 |
tzname() | 返回本地时区的名字 |
import 库名
库名.方法
具体方法的调用,要根据该方法的接口来传参。如产生0-1之间的随机数:
import random
number = random.uniform(0, 1)
print("产生的随机数是:", number)