python常用的一些方法

简介: python常用的一些方法

列表分组


根据每个列表的最大数进行分组,返回多个列
def list_of_groups(init_list,childern_list_len):
    list_of_groups = zip(*(iter(init_list),)* childern_list_len)
    end_list = [list(i)for our here_of_groups] 
    count = len(init_list)%childern_list_len 
    end_list.append(ini​​ t_list [-count:])if count!= 0 else end_list 
    return end_list 
list_of_groups(列表,10)

生成时间列表

import datetime,time
##生成时间列表
def create_assist_date(self, datestart=None, dateend=None):
    # 创建日期辅助表
    if dateend is None:
        dateend = datetime.datetime.now().strftime('%Y%m%d')
    # 转为日期格式`
    datestart = datetime.datetime.strptime(datestart, '%Y%m%d')
    dateend = datetime.datetime.strptime(dateend, '%Y%m%d')
    date_list = []
    date_list.append(datestart.strftime('%Y%m%d'))
    while datestart < dateend:
        # 日期叠加一天
        datestart += datetime.timedelta(days=+1)
        # 日期转字符串存入列表
        date_list.append(datestart.strftime('%Y%m%d'))
    return date_list
##时间转换  + -
datestart = datetime.datetime.strptime('20170715', '%Y%m%d')
datestart += datetime.timedelta(days=-36)
start = datestart.strftime('%Y%m%d')
date_list = create_assist_date(start, '20170715')

###获取列表的值的索引

知道列表的元素但不知道索引,可以用索引方法来进行查取,不过最好先设定一下,避免列表里有重复数据

list.index( ‘想要查索引的元素’)


获取当前机器的cpu和内存使用率

import psutil
memory_now = psutil.virtual_memory().percent
cpu_now = psutil.cpu_percent(interval=1)


###获取当前的Python脚本运行的PID和机器IP

import platform
import socket
pids = os.getpid()
addrs = socket.getaddrinfo(socket.gethostname(), None)
ip = [item[4][0] for item in addrs if ':' not in item[4][0]][0]
pid = re.findall("\d+", str(pids))[0]

列表排序

按照a倒叙排列, 当a相同时, 按b倒叙排列
all_list = [{"a": 10, "b": 22}, {"a": 11, "b": 23}, {"a": 11, "b": 12}]
all_list.sort(key=lambda x: (-x["a"], -x["b"]))


目录
相关文章
|
8天前
|
测试技术 API Python
【10月更文挑战第1天】python知识点100篇系列(13)-几种方法让你的电脑一直在工作
【10月更文挑战第1天】 本文介绍了如何通过Python自动操作鼠标或键盘使电脑保持活跃状态,避免自动息屏。提供了三种方法:1) 使用PyAutoGUI,通过安装pip工具并执行`pip install pyautogui`安装,利用`moveRel()`方法定时移动鼠标;2) 使用Pymouse,通过`pip install pyuserinput`安装,采用`move()`方法移动鼠标绝对位置;3) 使用PyKeyboard,同样需安装pyuserinput,模拟键盘操作。文中推荐使用PyAutoGUI,因其功能丰富且文档详尽。
|
5天前
|
机器学习/深度学习 数据采集 数据挖掘
11种经典时间序列预测方法:理论、Python实现与应用
本文将总结11种经典的时间序列预测方法,并提供它们在Python中的实现示例。
29 2
11种经典时间序列预测方法:理论、Python实现与应用
|
1天前
|
开发者 Python
Python中的魔法方法与运算符重载
在Python的奇妙世界里,魔法方法(Magic Methods)和运算符重载(Operator Overloading)是两个强大的特性,它们允许开发者以更自然、更直观的方式操作对象。本文将深入探讨这些概念,并通过实例展示如何利用它们来增强代码的可读性和表达力。
|
22天前
|
数据处理 Python
Python 高级技巧:深入解析读取 Excel 文件的多种方法
在数据分析中,从 Excel 文件读取数据是常见需求。本文介绍了使用 Python 的三个库:`pandas`、`openpyxl` 和 `xlrd` 来高效处理 Excel 文件的方法。`pandas` 提供了简洁的接口,而 `openpyxl` 和 `xlrd` 则针对不同版本的 Excel 文件格式提供了详细的数据读取和处理功能。此外,还介绍了如何处理复杂格式(如合并单元格)和进行性能优化(如分块读取)。通过这些技巧,可以轻松应对各种 Excel 数据处理任务。
55 16
|
14天前
|
Python
Python中的push方法详解与实例
Python中的push方法详解与实例
15 3
|
14天前
|
存储 Python
python列表操作和方法
python列表操作和方法
15 1
|
17天前
|
存储 索引 Python
反转Python列表的4种方法
反转Python列表的4种方法
19 2
|
18天前
|
Python
深入解析 Python 中的对象创建与初始化:__new__ 与 __init__ 方法
深入解析 Python 中的对象创建与初始化:__new__ 与 __init__ 方法
15 1
|
9天前
|
Linux Python
Python获得本机本地ip地址的方法
【10月更文挑战第8天】 socket模块包含了丰富的函数和方法,可以获取主机的ip地址,例如gethostbyname方法可以根据主机名获取ip地址,gethostbyname_ex方法可以获得本机所有ip地址列表,也可以使用netifaces模块获取网卡信息。
9 0
|
10天前
|
SQL 安全 数据库
Python防止SQL注入攻击的方法
Python防止SQL注入攻击的方法
20 0