随笔分类 - python
list列表切片方法汇总
摘要:python为list列表提供了强大的切片功能,下面是一些常见功能的汇总 """ 使用模式: [start:end:step] 其中start表示切片开始的位置,默认是0 end表示切片截止的位置(不包含),默认是列表长度 step表示切片的步长,默认是1 当start是0时,可以省略;当end是列
阅读全文
list操作
摘要:In [7]: a= [1,2] #定义一个list In [8]: a+[3,4] # 使用+操作 Out[8]: [1, 2, 3, 4] In [9]: a Out[9]: [1, 2] In [10]: a.extend([3,4]) # 使用extend操作,其实是在修改a In [11]
阅读全文
with上下文管理协议
摘要:with open('data.txt', 'r') as f: lines = f.readlines() for line in lines: print(line.split()) 类上面这段代码在python中很常见,就是使用with去管理资源。 其原理就是使用__entry__ 和__ex
阅读全文
re正则常用示例积累
摘要:2019-12-7 import re ''' 示例1: 提取网站的网址 ''' urls = ['https://blog.csdn.net/xxcupid/article/details/51993235', 'https://mbd.baidu.com/newspage/data/landin
阅读全文
python线程池示例
摘要:使用with方式创建线程池,任务执行完毕之后,会自动关闭资源 , 否则就需要手动关闭线程池资源 import threading, time from concurrent.futures import ThreadPoolExecutor, as_completed class MyTask(th
阅读全文
正则表达式之贪婪模式
摘要:首先需要明确一点,正则默认就是贪婪模式,何为贪婪模式? 就是在满足条件的前提下,尽可能多的进行匹配。 下面先来个例子 s ='my tel number is 132-4567-1258' r = re.match(r'(.+)(\d+-\d+-\d+)',s) print(r.groups())
阅读全文
re模块的高级使用
摘要:主要有四个方法: search : 从字符串的任意位置开始检索,首次匹配即结束 findall : 全部匹配 sub : 将正则匹配结果进行替换 split : 根据正则匹配结果将字符串进行切分,然后返回一个列表 import re ''' search 从字符串的任意位置搜索匹配,检索到第一个匹配
阅读全文
正则表达式常用示例
摘要:import re """ 使用match方法进行匹配 result = re.match(正则表达式,要匹配的字符串) 如果match匹配到数据的话,就可以使用group方法提取数据 注: 若字符串匹配正则表达式,则match方法返回匹配对象(Match Object), 否则返回None(不是空
阅读全文
python类对象属性查找原理
摘要:class Foo(object): def __init__(self): # 这是一个对象属性 self.obj_pro = 12 # 这是一类属性 c_pro = 11 # 这是一个静态方法 @classmethod def c_method(self): print('c_method')
阅读全文
python对象之__call__方法
摘要:先看示例,然后啥都明白了 class Student(): def __call__(self, *args, **kwargs): print('__call__方法被调用', *args) class Person(): def mm(self): print('mm方法被调用') 测试代码 :
阅读全文
通过类路径字符串获取获取类中静态属性
摘要:一个python类,其类路径字符串是student.Student class Student: name = 'admin' age = 12 通过如下方式就能获取到类的属性及其属性值 import importlib # 类的全路径 path = 'student.Student' p,c =
阅读全文
list的过滤操作
摘要:假设 l = ['abc', 'mn', 'aq', 'liuming'] 我要过滤出以a开头的元素,方法有以下两种 方法1: l = ['abc', 'mn', 'aq', 'liuming'] list1 = [e for e in l if e.startswith('a')] print(l
阅读全文
python之字符串切分
摘要:在工作中,经常遇到字符串切分,尤其是操作linux命令,返回一段文本,如下面这种格式 Filesystem Size Used Avail Use% Mounted on /dev/vda1 40G 3.1G 35G 9% / tmpfs 939M 0 939M 0% /dev/shm 在整理数据时
阅读全文
eval函数让我忧伤
摘要:今天首次接触这个eval函数,让我忧伤了一把。我把当成字符串拼接,结果错得天远地远。大体情况是下面这句代码,一个劲的给我报NameError: name 'qinfeng' is not defined. class_obj = eval('qinfeng.zheng.ipvsadm.%s()' %
阅读全文
浙公网安备 33010602011771号