数据集 AC错误 BD正确
# -*- coding: utf-8 -*-chrome://flags/#extensions-on-chrome-urls
问题: Initializing libiomp5.dylib, but found libomp.dylib already initialized.
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
带颜色的字体
from colorama import Fore, Back, Style, init
init(autoreset=True)
print(Fore.RED + '打印红色文字'
Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
时间:10% dh
##### txt 文本执行代码
eval("getsizeof({})".format(i))
###### 多类型参数
token_ids: Union[int, List[int], "np.ndarray", "torch.Tensor", "tf.Tensor"],
def to_py_obj(obj):
###Convert a TensorFlow tensor, PyTorch tensor, Numpy array or python list to a python list.
if isinstance(obj, (dict, UserDict)):
return {k: to_py_obj(v) for k, v in obj.items()}
elif isinstance(obj, (list, tuple)):
return [to_py_obj(o) for o in obj]
elif is_tf_available() and _is_tensorflow(obj):
return obj.numpy().tolist()
elif is_torch_available() and _is_torch(obj):
return obj.detach().cpu().tolist()
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return obj
isinstance(tensors, torch.Tensor)
#### Tqdm 进度条
##方式0
from tqdm import tqdm
for i in tqdm(range(1000)):
pass
##方式1
bar = tqdm(["a", "b", "c", "d"])
for char in pbar:
pbar.set_description("Processing %s" % char)
##pickle
import pickle
with open('a.bin','wb') as f:
pickle.dump(w,f,0)
with open('a.bin','rb') as f:
bb=pickle.load(f)
print("bb-----",bb)
##是否多一个s
import difflib
diff = list(difflib.Differ().compare(position_v, raw_v))
cout_sig = len([1 for f in diff if ('-' or '+') in f])
if (diff[-1] == '+ s' or diff[-1] == '- s') and (cout_sig == 1):
print(“1111”)
#替换多个replace
intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab, outtab)
#去掉某个字符 字符转化为列表
str.strip([chars]);
如果只是去掉空格,使用str.strip();
去掉首部指定字符 str.lstrip([chars]);
去掉尾部指定字符 str.rstrip([chars]);
#查看数组维度数组拼接,统计,排序,去重不改变排序、 列表转化为字典 、找到数组中出现最多次的元素 字典排序再变成字典
np.ndim(hh), np.size(hh), np.array(hh).shape
快速生成数组
a = [[0 for j in range(m)] for i in range(n)]
b = [[0]*2 for i in range(3)]
a=a+b
a.extend(b) ##两个都可以
tensor.tolist() tensor转化为数组
#统计
counter = Counter("bracadabra")
couter_sort = counter.most_common()
couter_sort_dict = dict(couter_sort)
#排序
single_word_dict_direct_sort = sorted(counter.items() , key=lambda x : x[1] ,reverse = True)
[('ss', 4), ('hh', 2), ('gg', 1)]
single_word_dict_direct_sort = sorted(counter, key=lambda x : x[0] ,reverse = True) ##区别 按照第几列排序
['ss', 'hh', 'gg']
List=[1,2,4,6,7,5,4,6,1,7,5,6,7,6,7,9,4,4,8,9,5,8,9,8,8]
a=set(List)
# 去重但不改变排序
addr_to = list(set(mailto))
addr_to.sort(key=mailto.index)
## 二维数组去重
query_list_len4 = list(set([tuple(t) for t in query_list_len4]))
# 随机生成不重复的几个数
lengths = random.sample(range(0,9),9)
randint(1,100)
找到数组中出现最多次的元素
a = [1, 1, 2]
arr = np.array(sentence_result) # 先转化为numpy.ndarray
print((arr == 0).all()) # 是否全部为1,False
print((arr == 1).any()) # 是否含有1,True
#### 数组转为字典
data = [[123, 4], [123, 5], [345, 29]]
dict((k, [x[1] for x in v]) for k, v in itertools.groupby(data, operator.itemgetter(0)))
{345: [29], 123: [4, 5]}
######### 字典排序再变成字典
dict = (sorted(dict.items() , key=lambda x : x[1] ,reverse = True))
dict2 = {raw[0]:raw[1] for raw in dict}
######### 字典寻找最大值
HHH = {'submarino vainilla': 1, 'submarinos vainilla': 54835, 'submarinosf vainilla': 56, 'submarinosaak vainilla': 54834}
HH2 = {'submarino vainilla': 0, 'submarinos vainilla': 54835, 'submarinosf vainilla': 56, 'submarinosaak vainilla': 54834}
gg = {'submarino vainilla':HHH,'submarino vainilla4':HH2}
hh0 = max(HHH.values())
hh1 = sum(HHH.values())
hh4 = max(HHH,key=HHH.get)
hh2 = max(HHH.keys()) ##### 无用 安装字母顺序排列
hh3 = max(HHH.items()) ##### 无用 安装字母顺序排列
######### 字典复合排序
dict = (sorted(KK.items() , key=lambda x : (x[1][0],x[1][1],x[1][2],x[1][3]) ,reverse = False))
dict = (sorted(KK.items() , key=lambda x : ([x[1][i] for i in range(len(list(KK.values())[0]))]) ,reverse = False))
########## 只取第几 少文本
h1_ = [x for idx,x in enumerate(result_checkout_in_piece) if idx<=10 ] ##### 字典取前10
h1_ = [x for idx,x in enumerate(result_checkout_in_piece) if idx==10 ] ##### 字典只取第10
######## 读取数组
我们需要用列表解析的方法读取一列:
>>> b=[x[0] for x in a]
>>> print(b)
[1, 4]
而对于数组,可以直接读取:
>>> import numpy as np
>>> a=np.array([[1,2,3],[4,5,6]])
>>> a[:,0]
#数组进行打乱
import random
list = [20, 16, 10, 5]
random.shuffle(list)
字符串转化为数组 a = list(eval(b))
数组转化为字符串 text = ' '.join(text)
数据转化为json json.loads(result.content)
数组转化为字典
dict1 = dict(zip(list1,list2))
dict2 = {key:value for key,value in zip(list1,list2)}
#字典
dict.items() 以列表返回可遍历的(键, 值) 元组数组
dict.keys() 以列表返回一个字典所有的键
dict.values() 以列表返回字典中的所有值
max(a,key=a.get) 取value最大的
A = {…} B={…} Z = {**A,**B} 合并字典A B
对数组内元素值和索引同时进行迭代
#字典 合并
dict1,dict2
finaldict = {**dict1,**dict2} ## 方法一
finaldict = dict1.copy()
finaldict.update(dict2) ### 方法二
链式函数调用
def add(x,y):
return x+y
def sub(x,y):
return x-y
x,y = 2,3
print((sub if x > y else add)(x,y))
Lambda
y = lambda symbol: 'X' if symbol==True else 'O' if symbol==False else ' ' 执行效率并不高相比较for 循环
- def add(x, y):
- return x+ y
- print(add(3,4))
- 等价于
- add = lambda x,y:x+y
- print(add(3,4))
for 循环
result_t = [1 if 'hook' in raw[1:] else 2 if 'hook wo' in raw[0] else 0 for raw in item_shop_list]
for i,element in enumerate(list1):
else:
python的else 子句不仅能在 if 语句中使用,还能在 for、while 和 try 等语句中使用,这个语言特性不是什么秘密,但却没有得到重视。
一行for if else
max((len(tmp),tmp) for tmp in tmp_start) for循环返回多个数组
str = 'asdfg'
for i in range(len(str)):
# x = [1 for k_c in str[i] if (k_c == 'a') else 0 ] #带有else 返回值时只能把if放前面
x = [1 if k_c == 'a' else 0 for k_c in str[i]]
print(x)
###
lis_s = [word for word in lis if is_endwith_s(word)]
lis_s = [word if is_endwith_s(word) else '666' for word in lis]
#for 循环 并行迭代
# 并行
chinese = [randint(60, 100) for _ in range(40)]
math = [randint(60, 100) for _ in range(40)]
english = [randint(60, 100) for _ in range(40)]
total = []
for x, y, z in zip(chinese, math, english):
total.append(x + y + z)
str.isnumeric(): True if 只包含数字;otherwise False。注意:此函数只能用于unicode string
str.isdigit(): True if 只包含数字;otherwise False。
str.isalpha():True if 只包含字母;otherwise False。
str.isalnum():True if 只包含字母或者数字;otherwise False。
Python自省
这个也是python彪悍的特性.自省就是面向对象的语言所写的程序在运行时,所能知道对象的类型.简单一句就是运行时能够获得对象的类型.比如type(),dir(),getattr(),hasattr(),isinstance().
#copy
b = a: 赋值引用,a 和 b 都指向同一个对象。
b = a.copy(): 浅拷贝, a 和 b 是一个独立的对象,但他们的子对象还是指向统一对象(是引用)。
b = copy.deepcopy(a): 深度拷贝, a 和 b 完全拷贝了父对象及其子对象,两者是完全独立的。
#时间目录
import os
import time
time_dir = time.strftime("%m%d_%H_%M_%S")
backdir = "batch_dir/"
mysqlbkdir = backdir + time_dir + "/"
if not os.path.exists(mysqlbkdir):
os.makedirs(mysqlbkdir)
else:
pass
类定义:
class Solution:
def profitableSchemes(self, n: int, minProfit: int, group: List[int], profit: List[int]) -> int:
hh = Solution()
s = hh.profitableSchemes(n,minProfit,group,profit)
def ensure_unicode(_str, encoding="utf-8”):##@@@@@@@@@@@@@@@@@@@@@有啥用
if isinstance(_str, bytes):
return _str.decode(encoding)
return _str
#位运算
1 & 2 # 输出为 0,
1 | 2 # 输出为3
# and中含0,返回0; 均为非0时,返回后一个值,
# or中, 至少有一个非0时,返回第一个非0,
and or
当前面有一个满足或者有一个不满足条件时 就直接结束校验 直接宣判结果 后面的条件就不需要再进行判断
& : 按位与操作, 只有 1&1 为 1, 其它情况为 0. 可用于进位运算
| : 按位或操作 , 只有 0|0 为0 ,其他情况为1
~ : 逐位取反
^ : 异或, 相同为 0 ,相异为 1 . 可用于加操作 (不包括进位项 )
a^= b 等价 a = a^b
<< : 左移操作, 2的幂有关 print(3<<2) 12 0011 -> 1100 三向左移动两位
>> : 右移操作, 2的幂有关
比较字符串差异:
import difflib
a = "dddds"
b = "ddddG"
diff = difflib.Differ().compare(b, a)
print(list(diff))
print ('\n'.join(list(diff)))
print 函数
print('Iteration: {}. Loss: {}. Accuracy: {}'.format(iter, loss.item(), accuracy))
Iteration: 2000. Loss: 0.174654021859169. Accuracy: 93.6500015258789
日志装饰器
import pysnooper
@pysnooper.snoop('./a.log') ## 方法体前面
加快Python执行速度
from numba import jit
@jit
colab
4/1AY0e-g4J9KFalb8jX4x-Uhw02HNjmIz8ilwfl1N_9zc0qufEVsbWDL1iPkY
4/1AY0e-g6ZEczf6adlM19bxMOj59kf-WjJRSbXYCkiptUWehABYsKhyoOYePg
############## 链接 Colab
from google.colab import drive
drive.mount('/content/drive')
############# 加载使用 数据集
!pip install datasets
from datasets import load_dataset
datasets = load_dataset('wikitext', 'wikitext-2-raw-v1')
datasets.save_to_disk('/content/drive/MyDrive/数据集')
Python将二维数组/多维数组转换为一维
方法6:itertools 效率高 0.013226985931396484
方法0:extend 效率最高 0.016206026077270508
方法4:列表推导式 0.01738429069519043
方法1:flatten 效率高 0.3760957717895508
方法0:extend
arr = []
for row in mulArrays:
arr.extend(row)
方法1:flatten
import numpy as np
mulArrays = [[1,2,3],[4,5,6],[7,8,9]]
print(list(np.array(mulArrays).flatten()))
方法2:reshape+concatenate
import numpy as np
mulArrays = [[1,2,3],[4,5,6],[7,8,9]]
print(list(np.concatenate(array.reshape((-1,1),order="F"))))
方法3:sum()
mulArrays = [[1,2,3],[4,5,6],[7,8,9]]
print(sum(mulArrays,[])) #[1, 2, 3, 4, 5, 6, 7, 8, 9]
方法4:列表推导式
mulArrays = [[1,2,3],[4,5,6],[7,8,9]]
print([i for arr in mulArrays for i in arr]) #[1, 2, 3, 4, 5, 6, 7, 8, 9]
方法5:operator
import operator
from functools import reduce
mulArrays = [[1,2,3],[4,5,6],[7,8,9]]
print(reduce(operator.add, mulArrays))
方法6:itertools
from itertools import chain
mulArrays = [[1,2,3],[4,5,6],[7,8,9]]
print(list(chain.from_iterable(mulArrays))) #[1, 2, 3, 4, 5, 6, 7, 8, 9]
logging日志
import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
logging.debug('Start of factorial(%s%%)' % (n))
可以将 basicConfig() 的 level 参数设置为 logging.ERROR,这将只显示 ERROR 和 CRITICAL 消息,跳过 DEBUG、INFO 和 WARNING 消息。
logging.disable() 函数禁用这些日志消息
logging.disable(logging.CRITICAL)
将日志消息输出到文件中
import logging
logging.basicConfig(filename='demo.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
#hist 画图
hist(lengths, bins=len(set(lengths)));
#git 上传大文件
(1)、在项目目录git lfs install。
(2)、git lfs track '.文件后缀'。
(3)、git add .gitattributes。
(4)、配置完成后就可按照平时git方式使用。
多线程
thread1.join(1) ## 相当于等thread1 执行了1s之后在执行后面的程序,如果没有参数就说明等thread1执行完毕之后再执行
检查对象的内存使用量
import sys
x = 1
print(sys.getsizeof(x))
python容器
列表:元素可变(任何数据类型),有序(可索引),append/insert/pop;
元组:元素不可变,但元素中的可变元素是可变的;有序(可索引);而且元组可以被散列,例如作为字典的键。
集合:无序(不可被索引)、互异
字典:无序,键值对(key:value),key唯一不可重复\
map()
函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回。(重点理解)
>>> def f(x):
... return x * x
>>> r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> list(r)
[1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> list(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9]))
['1', '2', '3', '4', '5', '6', '7', '8', '9']
MySQL数据库
create user 'lqt' identified by 'LQTlqt_123456';
time1 = time.process_time()
pip install googletrans==4.0.0-rc1
python 数据库
brew install mysql
mysql.server start
mysql -u root -p
创建数据库:create database testmysql;
查询数据库: show databases;
删除数据库: drop database testmysql;
检查mysql服务状态
systemctl status mysql.service
将字典转化为XML
hist parameter
matplotlib.pyplot.hist(x, bins=None, range=None, normed=False, weights=None, cumulative=False, bottom=None, histtype=’bar’, align=’mid’, orientation=’vertical’, rwidth=None, log=False, color=None, label=None, stacked=False, hold=None, data=None, **kwargs)
Parameters:
- x : (n,) array or sequence of (n,) arrays(可以是一个array也可以是多个array)
- integer or array_like or ‘auto’, optional(可以是整型来设置箱子的宽度,也可以是array,指定每个箱子的宽度)
- range : tuple or None, optional(设置显示的范围,范围之外的将被舍弃)
- normed : boolean, optional(?)
- density的类型是 bool型,指定为True,则为频率直方图,反之,频数直方图
- weights : (n, ) array_like or None, optional(?)
- cumulative : boolean, optional(?)
- bottom : array_like, scalar, or None(?)
- histtype : {‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’}, optional(选择展示的类型,默认为bar)
- align : {‘left’, ‘mid’, ‘right’}, optional(对齐方式)
- orientation : {‘horizontal’, ‘vertical’}, optional(箱子方向)
- log : boolean, optional(log刻度)
- color : color or array_like of colors or None, optional(颜色设置)
- label : string or None, optional(刻度标签)
- stacked : boolean, optional(?)
return
- n : array or list of arrays(箱子的值)
- bins : array(箱子的边界)
- patches : list or list of lists
python DEBUG 调试
import pdb
a = "aaa"
pdb.set_trace()
b = "bbb"
c = "ccc"
final = a + b + c
print final
命令 | 解释 |
break 或 b 设置断点 | 设置断点 |
continue 或 c | 继续执行程序 |
list 或 l | 查看当前行的代码段 |
step 或 s | 进入函数 |
return 或 r | 执行代码直到从当前函数返回 |
exit 或 q | 中止并退出 |
next 或 n | 执行下一行 |
pp | 打印变量的值 |
help | 帮助 |
!b=“ggggg” | 修改参数值 |
从文件读取代码并且执行
# gb2312 UTF-8 ,encoding=""
with open('t_code.txt','r',errors='ignore',encoding="UTF-8") as f:
exec(f.read())
定时执行代码:
import schedule
import time
def job():
print("I'm working...")
schedule.every(10).seconds.do(job) # 每10秒执行一次
schedule.every(10).minutes.do(job) # 每10分钟执行一次
schedule.every().hour.do(job) # 每小时执行一次
schedule.every().day.at("10:30").do(job) # 每天十点半执行
schedule.every(5).to(10).minutes.do(job) # 不理解
schedule.every().monday.do(job) # 每周一执行
schedule.every().wednesday.at("13:15").do(job) # 每周三13点15执行
schedule.every().minute.at(":17").do(job) # 每分钟的第17秒钟
while True:
schedule.run_pending() # 运行所有可运行的任务
time.sleep(1)
执行CMD命令
import os
os.system('pwd && mkdir test') #执行多条命令可以使用&&连接
os.system('ls')
1、os.system(cmd)
在子终端运行系统命令,不能获取命令执行后的返回信息以及执行返回的状态
import os
os.system('date') # 2016年 06月 30日 星期四 19:26:21 CST
2、os.popen(cmd) close() 等待上一条执行完成
不仅执行命令而且返回执行后的信息对象(常用于需要获取执行命令后的返回信息)
import os
nowtime = os.popen('date')
print nowtime.read()
二、commands模块 python 3.0 没有之前 只有之后的了 subprocess
getoutput 获取执行命令后的返回信息
getstatus 获取执行命令的状态值(执行命令成功返回数值0,否则返回非0)
getstatus output 获取执行命令的状态值以及返回信息
import subprocess
status, output = subprocess.getstatusoutput('date')
print status # 0
print output # 2016年 06月 30日 星期四 19:26:21 CST
三、subprocess模块 waite() 等待上一条执行完成
运用对线程的控制和监控,将返回的结果赋于一变量,便于程序的处理。官方文档:http://python.usyiyi.cn/python_278/library/subprocess.html
import subprocess
nowtime = subprocess.Popen('date', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print nowtime.stdout.read() # 2016年 06月 30日 星期四 19:26:21 CST
首先比较下return 与 yield的区别:
return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。
yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行, 直到程序结束
ipynb转为其他格式:
- ipynb转换为python
jupyter nbconvert --to python my_file.ipynb
- ipynb转换为md
jupyter nbconvert --to md my_file.ipynb
- ipynb转为html
jupyter nbconvert --to html my_file.ipynb
- ipython转换为pdf
jupyter nbconvert --to pdf my_file.ipynb
- 其他格式转换请参考
jupyter nbconvert --help
1、字符串转bytes
'''
string to bytes
eg:
'0123456789ABCDEF0123456789ABCDEF'
b’0123456789ABCDEF0123456789ABCDEF'
'''
def stringTobytes(str):
return bytes(str,encoding='utf8')
2、bytes转字符串
'''
bytes to string
eg:
b'0123456789ABCDEF0123456789ABCDEF'
'0123456789ABCDEF0123456789ABCDEF'
'''
def bytesToString(bs):
return bytes.decode(bs,encoding='utf8')
3、十六进制字符串转bytes
'''
hex string to bytes
eg:'01 23 45 67 89 AB CD EF 01 23 45 67 89 AB CD EF'
b'\x01#Eg\x89\xab\xcd\xef\x01#Eg\x89\xab\xcd\xef'
'''
def hexStringTobytes(str):
str = str.replace(" ", "")
return bytes.fromhex(str)
# return a2b_hex(str)
4、bytes转十六进制字符串
'''
bytes to hex string
eg:b'\x01#Eg\x89\xab\xcd\xef\x01#Eg\x89\xab\xcd\xef'
'01 23 45 67 89 AB CD EF 01 23 45 67 89 AB CD EF'
'''
def bytesToHexString(bs):
# hex_str = ''
# for item in bs:
# hex_str += str(hex(item))[2:].zfill(2).upper() + " "
# return hex_str
return ''.join(['%02X ' % b for b in bs])