旅游微信网站建设网站要放备案号吗

当前位置: 首页 > news >正文

旅游微信网站建设,网站要放备案号吗,seo属于运营还是技术,个人做地方门户网站1、易混淆操作 本节对一些 Python 易混淆的操作进行对比。 1.1 有放回随机采样和无放回随机采样 import random random.choices(seq, k1) # 长度为k的list#xff0c;有放回采样 random.sample(seq, k) # 长度为k的list#xff0c;无放回采样1.2 lambda 函数的参数 …1、易混淆操作 本节对一些 Python 易混淆的操作进行对比。 1.1 有放回随机采样和无放回随机采样 import random random.choices(seq, k1)  # 长度为k的list有放回采样 random.sample(seq, k)     # 长度为k的list无放回采样1.2 lambda 函数的参数 func  lambda y: x  y          # x的值在函数运行时被绑定 func  lambda y, xx: x  y     # x的值在函数定义时被绑定1.3 copy 和 deepcopy import copy y  copy.copy(x)      # 只复制最顶层 y  copy.deepcopy(x)  # 复制所有嵌套部分复制和变量别名结合在一起时容易混淆 a  [1, 2, [3, 4]]# Alias. b_alias  a   assert b_alias  a and b_alias is a# Shallow copy. b_shallow_copy  a[:]   assert b_shallow_copy  a and b_shallow_copy is not a and b_shallow_copy[2] is a[2]# Deep copy. import copy b_deep_copy  copy.deepcopy(a)   assert b_deep_copy  a and b_deep_copy is not a and b_deep_copy[2] is not a[2]对别名的修改会影响原变量浅复制中的元素是原列表中元素的别名而深层复制是递归的进行复制对深层复制的修改不影响原变量。 2、常用工具 2.1 读写 CSV 文件 import csv # 无header的读写 with open(name, rt, encodingutf-8, newline) as f:  # newline让Python不将换行统一处理for row in csv.reader(f):print(row[0], row[1])  # CSV读到的数据都是str类型 with open(name, modewt) as f:f_csv  csv.writer(f)f_csv.writerow([symbol, change])# 有header的读写 with open(name, modert, newline) as f:for row in csv.DictReader(f):print(row[symbol], row[change]) with open(name, modewt) as f:header  [symbol, change]f_csv  csv.DictWriter(f, header)f_csv.writeheader()f_csv.writerow({symbol: xx, change: xx})注意当 CSV 文件过大时会报错_csv.Error: field larger than field limit (131072)通过修改上限解决 import sys csv.field_size_limit(sys.maxsize)csv 还可以读以 \t 分割的数据 f  csv.reader(f, delimiter\t)2.2 迭代器工具 itertools 中定义了很多迭代器工具例如子序列工具 import itertools itertools.islice(iterable, startNone, stop, stepNone) # islice(ABCDEF, 2, None) - C, D, E, Fitertools.filterfalse(predicate, iterable)         # 过滤掉predicate为False的元素 # filterfalse(lambda x: x  5, [1, 4, 6, 4, 1]) - 6itertools.takewhile(predicate, iterable)           # 当predicate为False时停止迭代 # takewhile(lambda x: x  5, [1, 4, 6, 4, 1]) - 1, 4itertools.dropwhile(predicate, iterable)           # 当predicate为False时开始迭代 # dropwhile(lambda x: x  5, [1, 4, 6, 4, 1]) - 6, 4, 1itertools.compress(iterable, selectors)            # 根据selectors每个元素是True或False进行选择 # compress(ABCDEF, [1, 0, 1, 0, 1, 1]) - A, C, E, F序列排序 sorted(iterable, keyNone, reverseFalse)itertools.groupby(iterable, keyNone)              # 按值分组iterable需要先被排序 # groupby(sorted([1, 4, 6, 4, 1])) - (1, iter1), (4, iter4), (6, iter6)itertools.permutations(iterable, rNone)           # 排列返回值是Tuple # permutations(ABCD, 2) - AB, AC, AD, BA, BC, BD, CA, CB, CD, DA, DB, DCitertools.combinations(iterable, rNone)           # 组合返回值是Tuple itertools.combinations_with_replacement(…) # combinations(ABCD, 2) - AB, AC, AD, BC, BD, CD多个序列合并 itertools.chain(*iterables)                        # 多个序列直接拼接 # chain(ABC, DEF) - A, B, C, D, E, Fimport heapq heapq.merge(*iterables, keyNone, reverseFalse)   # 多个序列按顺序拼接 # merge(ABF, CDE) - A, B, C, D, E, Fzip(*iterables)                                    # 当最短的序列耗尽时停止结果只能被消耗一次 itertools.zip_longest(*iterables, fillvalueNone)  # 当最长的序列耗尽时停止结果只能被消耗一次2.3 计数器 计数器可以统计一个可迭代对象中每个元素出现的次数。 import collections # 创建 collections.Counter(iterable)# 频次 collections.Counter[key]                 # key出现频次 # 返回n个出现频次最高的元素和其对应出现频次如果n为None返回所有元素 collections.Counter.most_common(nNone)# 插入/更新 collections.Counter.update(iterable) counter1  counter2; counter1 - counter2  # counter加减# 检查两个字符串的组成元素是否相同 collections.Counter(list1)  collections.Counter(list2)2.4 带默认值的 Dict 当访问不存在的 Key 时defaultdict 会将其设置为某个默认值。 import collections collections.defaultdict(type)  # 当第一次访问dict[key]时会无参数调用type给dict[key]提供一个初始值2.5 有序 Dict import collections collections.OrderedDict(itemsNone)  # 迭代时保留原始插入顺序3、高性能编程和调试 3.1 输出错误和警告信息 向标准错误输出信息 import sys sys.stderr.write()输出警告信息 import warnings warnings.warn(message, categoryUserWarning)   # category的取值有DeprecationWarning, SyntaxWarning, RuntimeWarning, ResourceWarning, FutureWarning控制警告消息的输出 \( python -W all     # 输出所有警告等同于设置warnings.simplefilter(always) \) python -W ignore  # 忽略所有警告等同于设置warnings.simplefilter(ignore) \( python -W error   # 将所有警告转换为异常等同于设置warnings.simplefilter(error)3.2 代码中测试 有时为了调试我们想在代码中加一些代码通常是一些 print 语句可以写为 # 在代码中的debug部分 if __debug__:pass一旦调试结束通过在命令行执行 -O 选项会忽略这部分代码 \) python -0 main.py3.3 代码风格检查 使用 pylint 可以进行不少的代码风格和语法检查能在运行之前发现一些错误 pylint main.py3.4 代码耗时 耗时测试 $ python -m cProfile main.py测试某代码块耗时 # 代码块耗时定义 from contextlib import contextmanager from time import perf_countercontextmanager def timeblock(label):tic  perf_counter()try:yieldfinally:toc  perf_counter()print(%s : %s % (label, toc - tic))# 代码块耗时测试 with timeblock(counting):pass代码耗时优化的一些原则 专注于优化产生性能瓶颈的地方而不是全部代码。 避免使用全局变量。局部变量的查找比全局变量更快将全局变量的代码定义在函数中运行通常会快 15%-30%。 避免使用.访问属性。使用 from module import name 会更快将频繁访问的类的成员变量 self.member 放入到一个局部变量中。 尽量使用内置数据结构。str, list, set, dict 等使用 C 实现运行起来很快。 避免创建没有必要的中间变量和 copy.deepcopy()。 字符串拼接例如 a : b : c 会创造大量无用的中间变量:,join([a, b, c]) 效率会高不少。另外需要考虑字符串拼接是否必要例如 print(:.join([a, b, c])) 效率比 print(a, b, c, sep:) 低。