0. lambda匿名函数
用lambda关键词能创建小型匿名函数。这种函数得名于省略了用def声明函数的标准步骤。
lambda函数的语法只包含一个语句,如下:
lambda [arg1, arg2…argn]:expression
如下实例:
>>> sum = lambda arg1, arg2: arg1+arg2
>>> print("Value of total:", sum(10, 20))
Value of total: 30
>>> print("Value of total:", sum(20, 20))
Value of total: 40
>>>
Lambda函数能接收任何数量的参数但只能返回一个表达式的值,匿名函数不能直接调用print,因为lambda需要一个表达式。
>>> infors = [{"name":"laowang", "age":10},{"name":"xiaoming", "age":20},{"name":"banzhang", "age":10}]
>>> infors.sort(key=lambda x:x['name'])
>>> infors
[{'name': 'banzhang', 'age': 10}, {'name': 'laowang', 'age': 10}, {'name': 'xiaoming', 'age': 20}]
1. init方法
2.列表转换成字符串,字符串转换成列表
列表转换成字符串:
>>> nums = [1, 2, 9]
>>> nums = [str(i) for i in nums]
>>> nums
['1', '2', '9']
>>> n_string = "".join(nums)
>>> n_string
'129'
字符串转换成列表:
>>> n = int("".join(nums))
>>> n
129
>>> rList = [int(i) for i in list(str(n))]
>>> rList
[1, 2, 9]
列表转换成字符串字符用’/'隔开:
>>> a = ['a', 'b']
>>> '/'.join(a)
'a/b'
3. try 和 except
num = [1,2,0,3,1.5,'6']
for x in num:
try: # 尝试执行下列代码
print (6/x)
except ZeroDivisionError: # 当报错信息为ZeroDivisionError,执行下面的语句。
print('0是不能做除数的!')
except TypeError: # 当报错信息为TypeError,执行下面的语句。
print('被除数必须是整值或浮点数!')
结果:
6.0
3.0
0是不能做除数的!
2.0
4.0
被除数必须是整值或浮点数!
4. __call__
方法
该方法的功能类似于在类中重载 () 运算符,使得类实例对象可以像调用普通函数那样,以“对象名()”的形式使用。
例子:
>>> class Comli_cn:
# 定义__call__方法
def __call__(self, a):
这里的print(a)
>>> comli_cn = Comli_cn()
>>> comli_cn("Hello World!")
Hello World!
也就是说调用__call__方法的时候可以像调用python内置方法一样调用,对于__call__方法以下两种表达方式是一样的:
>>> comli_cn("Hello World!")
Hello World!
>>> comli_cn.__call__("Hello World!")
Hello World!
5. assert的用法
assert:断言
格式:
assert 表达式 [, 参数]
当表达式为真时,程序继续往下执行;
当表达式为假时,抛出AssertionError错误,并将 参数 输出
举例:
def foo(s):
n = int(s)
assert n != 0, 'n is zero!'
return 10 / n
foo('0')
# 代码执行结果
# AssertionError: n is zero!
6. isinstance() 函数
描述
isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type()。
isinstance() 与 type() 区别:
type() 不会认为子类是一种父类类型,不考虑继承关系。
isinstance() 会认为子类是一种父类类型,考虑继承关系。
如果要判断两个类型是否相同推荐使用 isinstance()。
语法
以下是 isinstance() 方法的语法:
isinstance(object, classinfo)
参数
object -- 实例对象。
classinfo -- 可以是直接或间接类名、基本类型或者由它们组成的元组。
返回值
如果对象的类型与参数二的类型(classinfo)相同则返回 True,否则返回 False。。
实例
以下展示了使用 isinstance 函数的实例:
>>>a = 2
>>> isinstance (a,int)
True
>>> isinstance (a,str)
False
>>> isinstance (a,(str,int,list)) # 是元组中的一个返回 True
True
type() 与 isinstance()区别:
class A:
pass
class B(A):
pass
isinstance(A(), A) # returns True
type(A()) == A # returns True
isinstance(B(), A) # returns True
type(B()) == A # returns False
7. enumerate() 函数
描述
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
Python 2.3. 以上版本可用,2.6 添加 start 参数。
语法
以下是 enumerate() 方法的语法:
enumerate(sequence, [start=0])
参数
sequence -- 一个序列、迭代器或其他支持迭代对象。
start -- 下标起始位置。
返回值
返回 enumerate(枚举) 对象。
实例
以下展示了使用 enumerate() 方法的实例:
>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1)) # 下标从 1 开始
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
普通的 for 循环
>>>i = 0
>>> seq = ['one', 'two', 'three']
>>> for element in seq:
... print i, seq[i]
... i +=1
...
0 one
1 two
2 three
for 循环使用 enumerate
>>>seq = ['one', 'two', 'three']
>>> for i, element in enumerate(seq):
... print i, element
...
0 one
1 two
2 three
8. bin() 函数
bin() 返回一个整数 int 或者长整数 long int 的二进制表示
用法:bin(x), x为整数 int 或者长整数 long int
例子:
>>>bin(10)
'0b1010'
>>>> bin(20)
'0b10100'
9. input()函数
input() 函数接受一个标准输入数据,返回为 string 类型。
例子:
>>> a = input()
hello world!!! # 自己输入的
>>> a
'hello world!!!'
10. split()函数
split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串。
split() 方法语法:
str.split(str="", num=string.count(str))
参数:
str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
num – 分割次数。默认为 -1, 即分隔所有。
返回值:
返回分割后的字符串列表。
例子:
>>> txt = "Google#Runoob#Taobao#Facebook"
>>> x = txt.split("#", 1)
>>> x
['Google', 'Runoob#Taobao#Facebook']
>>> y = txt.split("#", 2)
>>> y
['Google', 'Runoob', 'Taobao#Facebook']
>>> z = txt.split("#")
>>> z
['Google', 'Runoob', 'Taobao', 'Facebook']
11. map()函数
描述:
map() 会根据提供的函数对指定序列做映射。
第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。
map() 函数语法:
map(function, iterable, ...)
参数:
function -- 函数
iterable -- 一个或多个序列
例子:
>>>def square(x) : # 计算平方数
... return x ** 2
...
>>> map(square, [1,2,3,4,5]) # 计算列表各个元素的平方
[1, 4, 9, 16, 25]
>>> map(lambda x: x ** 2, [1, 2, 3, 4, 5]) # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]
# 提供了两个列表,对相同位置的列表数据进行相加
>>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
[3, 7, 11, 15, 19]
例子:
>>> a = input()
1 2 3 4 5
>>> a
'1 2 3 4 5'
>>> li = map(int, a.split()) # 生成一个迭代器, 这里int相当于一个函数,可以将str转为int
>>> li
<map object at 0x03539B70>
>>> for i in li:
print(i)
1
2
3
4
5
>>> a = input()
1 2 3 4 5
>>> a
'1 2 3 4 5'
>>> li = list(map(int, a.split()))
>>> li
[1, 2, 3, 4, 5]
>>> x, y = map(int, input().split())
>? 1 2
>>> x
1
>>> y
2
12. strip()函数
描述:
Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。
strip()方法语法:
str.strip([chars])
参数:
chars – 移除字符串头尾指定的字符序列。
返回值:
返回移除字符串头尾指定的字符生成的新字符串。
13. Python 字典(Dictionary) items()方法
描述:
Python 字典(Dictionary) items() 函数以列表返回可遍历的(键, 值) 元组数组。
items()方法语法:
dict.items()
返回值:
返回可遍历的(键, 值) 元组数组。
例子:
>>> dic = {"a": 2, "d": 5, "c": 3}
>>> dic
{'a': 2, 'd': 5, 'c': 3}
>>> dic.items()
dict_items([('a', 2), ('d', 5), ('c', 3)])
>>> for key, value in dic.items():
print(key, value)
a 2
d 5
c 3
>>> dic2 = sorted(dic.items(), key=lambda x: x[0]) # 按照第0个元素排序
>>> dic2
[('a', 2), ('c', 3), ('d', 5)]
# 按照第1个元素降序排列
>>> dic3 = sorted(dic.items(), key=lambda x: x[1], reverse = True)
>>> dic3
[('d', 5), ('c', 3), ('a', 2)]
14. enumerate()
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
例子:
>>> names = ["aa", "bb", "cc"]
>>> for temp in enumerate(names):
... print(temp)
...
(0, 'aa')
(1, 'bb')
(2, 'cc')
>>> for i, name in enumerate(names): # 一个拆包的过程
... print(i, name)
...
0 aa
1 bb
2 cc
15. 结果输出保留两位小数
>>> a = 1.23456
>>> a
1.23456
>>> print("a = %.2f" % a)
a = 1.23
16. 查看字典里是否存在某个key
>>> a = {'a': 1, 'b': 2}
>>> a
{'a': 1, 'b': 2}
>>> print(a.get('a'))
1
>>> print(a.get('c'))
None
17.格式化输出
整数:
%o —— oct 八进制
%d —— dec 十进制
%x —— hex 十六进制
小数:
%f ——保留小数点后面六位有效数字
%.3f,保留3位小数位
%e ——保留小数点后面六位有效数字,指数形式输出
%.3e,保留3位小数位,使用科学计数法
%g ——在保证六位有效数字的前提下,使用小数方式,否则使用科学计数法
%.3g,保留3位有效数字,使用小数或科学计数法
字符串:
%s
%10s——右对齐,占位符10位
%-10s——左对齐,占位符10位
%.2s——截取2位字符串
%10.2s——10位占位符,截取两位字符串
>>> a = "fvlulibi"
>>> b = 7
>>> print("%s,%d" %(a, b)) # 注意这里没有逗号
fvlulibi,7
18.从一个列表里面删除元素
>>> a = [1,2,3,4]
>>> a.pop(2)
3
>>> a
[1, 2, 4]
19. Python 如何判断大小写字母,ASCII值及大小写转换
# s 代表字符串
s.isalnum() #所有字符都是数字或者字母
s.isalpha() #所有字符都是字母
s.isdigit() #所有字符都是数字
s.islower() #所有字符都是小写
s.isupper() #所有字符都是大写
s.istitle() #所有单词都是首字母大写,像标题
s.isspace() #所有字符都是空白字符、\t、\n
s.upper() #把所有字符中的小写字母转换成大写字母
s.lower() #把所有字符中的大写字母转换成小写字母
s.capitalize() #把第一个字母转化为大写字母,其余小写
s.title() #把每个单词的第一个字母转化为大写,其余小写
ord(s):获得字符 s 的 ASCII 值
chr(s):获得数字 s 对应的字母
20.ord()
语法:
ord(c)
其中c是字符
作用是返回字符c对应的ASCII 数值,或者 Unicode 数值。
例子:
>>>ord('a')
97
>>> ord('b')
98
>>> ord('c')
99
21.random模块
(1)random.random()
语法:
import random
random.random()
返回随机生成的一个实数,它在[0,1)范围内。
例子:
import random
# 生成第一个随机数
print "random() : ", random.random()
# 生成第二个随机数
print "random() : ", random.random()
结果:
random() : 0.281954791393
random() : 0.309090465205
(2)random.randrange(start,end)
作用:生成一个[start,end)之间的一个随机整数
例子:
>>> import random
>>> random.randrange(1,5)
1
>>> random.randrange(1,5)
4
>>> random.randrange(1,5)
1
>>> random.randrange(1,5)
2
>>> random.randrange(1,5)
4
22.频率计数
# 统计字符出现的次数
>>> import collections
>>> collections.Counter('hello world')
Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})
# 统计单词数
>>> collections.Counter('hello world hello world hello nihao'.split())
Counter({'hello': 3, 'world': 2, 'nihao': 1})
# 数组计数
>>> nums = [1, 1, 1, 2, 2, 3]
>>> collections.Counter(nums)
Counter({1: 3, 2: 2, 3: 1})
23.堆
- heapq.heappush(heap, item):插入元素
- heapq.heappop(heap):取出最小元素
- heapq.heappushpop(heap, item):先插入,再弹出最小值(比 heappush + heappop 更高效)
- heapq.heapify(list):将列表原地转换为堆
- heapq.nlargest(k, iterable):获取最大 k 个元素
- heapq.nsmallest(k, iterable):获取最小 k 个元素
'''
heaqp模块提供了堆队列算法的实现,也称为优先级队列算法。
要创建堆,请使用初始化为[]的列表,或者可以通过函数heapify()将填充列表转换为堆。
提供以下功能:
heapq.heappush(堆,项目)
将值项推入堆中,保持堆不变。
heapq.heapify(x)
在线性时间内将列表x转换为堆。
heapq.heappop(堆)
弹出并返回堆中的最小项,保持堆不变。如果堆是空的,则引发IndexError。
'''
import heapq
#1 heappush生成堆+ heappop把堆从小到大pop出来
heap = []
data = [1,3,5,7,9,2,4,6,8,0]
for i in data:
heapq.heappush(heap,i)
print(heap)
lis = []
while heap:
lis.append(heapq.heappop(heap))
print(lis)
#2 heapify生成堆+ heappop把堆从小到大pop出来
data2 = [1,5,3,2,9,5]
heapq.heapify(data2)
print(data2)
lis2 = []
while data2:
lis2.append(heapq.heappop(data2))
print(lis2)
#输出结果
[0, 1, 2, 6, 3, 5, 4, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 5, 9, 5]
[1, 2, 3, 5, 5, 9]
24.读文件
按行读:
line = file_name.readline()
line = line.strip('\n')
while line:
print line
line = file_name.readline()
line = line.strip('\n')
25. 使用参数传入文件名并用open读取文件
运行命令:
python test.py file1.txt file2.txt
test.py
_, file1, file2 = sys.argv
# 逐行读取
for line1 in open(file1):
xxx
for line2 in open(file2):
xxx
26. gb18030和utf-8的格式转换
(1)将原本为gb18030格式的文件转为utf-8格式的文件
content = open("gb18030.txt").read().decode("gb18030")
open("utf-8.txt", "w").write(content.encode("utf-8"))
这样原本由gb18030编码且无乱码的gb18030.txt文件就转换成了由utf-8编码的且无乱码的utf-8.txt文件。
(2)将原本为utf-8格式的文件转为gb18030格式的文件
content = open("utf-8.txt").read().decode("utf-8")
open("gb18030.txt", "w").write(content.encode("gb18030"))
(3)将utf-8编码的中文文本逐字拆出来
27. 指定python 版本安装
python3.6 -m pip install <package_name> # 可以在python3.6下安装包
python3.7 -m pip install <package_name> # 可以在python3.7下安装包
python3.8 -m pip install <package_name> # 可以在python3.8下安装包
28. 编码格式转换
(1)原本是utf-8编码,转unicode
query = "哈喽"
uni_query = query.decode('utf-8')
29. 判断字符串是否包含中文
在unicode里面中文的范围是"\u4e00"到"\u9fff",想要判断字符串里是否包含中文可以先将字符串解码为unicode,然后判断字符是否在"\u4e00"到"\u9fff"的范围内。
def is_contain_chiness(my_str):
for ch in my_str:
if u'\u4e00' <= ch <= u'\u9fff':
return True
return False
30. 队列
collections.deque()
collections是python自带的库,collections.deque()是双端队列,可以实现左右两端添加或取出元素的功能,时间复杂度均为O(1),同时具有栈和队列的性质。
例子:
q = collections.deque()
q.append('a') # 在队列右边添加元素‘a’,deque(['a'])
q.appendleft('b') # 在队列左边添加元素‘b’,deque(['b', 'a'])
q.extend('c') # 在队列右边添加序列,deque(['b', 'a', 'c])
q.extendleft('d') # 在队列左边添加序列,deque(['d', 'b', 'a', 'c'])
# 关于append和extend的区别
# append添加一整个对象,不管该对象是什么类型,都作为一个整体全部添加进来,如整型数据或者一整个列表
# extend添加一个序列,只能是相同类型的元素,如该题中的字符/字符串或者是由字符和字符串组成的列表['st','e'],其他类型的数据如整数就会报错
q.append(['e','f'] # deque(['d', 'b', 'a', 'c', ['e', 'f']])
q.append(4) # deque(['d', 'b', 'a', 'c', ['e', 'f'], 4])
q.extend([g', 'h'] # deque(['d', 'b', 'a', 'c', ['e', 'f'], 4, 'g', 'h'])
q.append(5) # 报错 TypeError: 'int' object is not iterable
# 取出元素
q.pop() # 右端取出
q.popleft() # 左端取出
31. 跨文件夹调用
A文件夹和B文件夹都在C文件夹下面,此时A文件夹里面的a.py想要调用B文件夹里面的b.py,要设置路径才可以实现。设置路径有两种方式:
- 在python代码里面设置
import sys
sys.path.append('path/of/C')
或者
cur_dir = os.path.dirname(os.path.abspath(__file__))
proj_dir = os.path.dirname(os.path.dirname(cur_dir))
sys.path.append(proj_dir)
- 在run.sh里面设置
CURDIR=$(pwd)
export PYTHONPATH="${PYTHONPATH}:${CURDIR}"
- 将搜索路径insert到想要的位置
cur_dir = os.path.dirname(os.path.abspath(__file__))
proj_dir = os.path.dirname(os.path.dirname(cur_dir))
sys.path.insert(0,proj_dir) # insert到第一个
可以用下面的语句查看所有的搜索路径:
print(sys.path)
如果其中的某两个路径里面
32. 使用tqdm生成进度条
- 如果是加载文件
from tqdm import tqdm
# 打开文件,并使用 tqdm 显示处理进度
with open("aaa.json", "r") as file:
# 使用 tqdm 包装文件迭代器,显示处理进度
for line in tqdm(file, desc="处理进度"):
# 在这里处理每一行的内容
pass # 你的处理逻辑
- 如果加载list
from tqdm import tqdm
import time
# 示例:遍历一个列表并添加进度条
my_list = [1, 2, 3, 4, 5]
for item in tqdm(my_list, desc="处理进度"):
# 在这里进行你的处理
time.sleep(0.5) # 模拟处理时间,这里是一个休眠操作
- 记载dict
from tqdm import tqdm
import time
# 示例:遍历一个列表并添加进度条
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
for key in tqdm(my_dict, desc="处理进度"):
# 在这里进行你的处理
time.sleep(0.5) # 模拟处理时间,这里是一个休眠操作
33. queue队列
队列分类
队列有很多种,但都依赖模块queue
队列方式 | 特点 |
---|---|
queue.Queue | 先进先出队列 |
queue.LifoQueue | 后进先出队列 |
queue.PriorityQueue | 优先级队列 |
queue.deque | 双线队列 |
最常用的是collections里面的deque:
>>> from collections import deque
>>> q = deque(['a', 'b', 'c'])
>>> q.append('x')
>>> q.appendleft('y')
>>> q
deque(['y', 'a', 'b', 'c', 'x'])