🧑‍🏫
liualexiang
  • Introduction
  • Azure
    • AKS Basic
    • AKS Spark
    • AZ ACR SYNC
    • Azure CMI SDWAN
    • Azure LB DeepDive
      • Azure LB DeepDive
    • Azure Service Principal Basic
    • Azure Internal VM Network Connectivity
      • Azure Internal VM Network Connectivity
    • Azure Cli Build
    • Azure Vm Memory Monitor
  • Blockchain
    • BTC
  • CRISPR
    • 使用Parallel_Cluster提升CRISPA效率
  • OpenSource
    • ElasticSearch
      • ES Get Started
      • ES Search Query
      • Kibana 可视化
      • Logstash配置
    • Ansible 基础
    • Infra As Code
      • Pulumi Get Started
      • Terraform Basic
    • ZooKeeper 基础
    • RPC与REST
    • 使用Python申请大量内存测试
    • 使用TPC_DS产生压测数据
    • Superset
      • Superset部署手册
    • 代码扫描
    • Git
      • Git Basic
      • Github Action Basic
      • Gitlab与AzureAD集成
      • Gitbook 基础教程
    • K8S
      • enter_node
      • K8s X509 Client Cert
      • K8s Basic
      • K8s Oidc
      • Docker 基础
      • helm基础
      • K8S_Secrets管理
      • 深入了解K8S
      • 混沌工程
      • Istio
      • 生态
      • CRD开发
      • k8s网络
    • Cloud_Custodian
    • Jenkins Basic
    • Nginx
    • ETCD
    • 正则
    • VictoriaMetrics
    • Kafka
  • MySQL
    • MySQL 调优
  • Linux
    • SSH Tunnel 上网
    • 内存管理
    • 在Linux系统中通过LUKS加密磁盘
    • 量子计算 Basic
    • IO多路复用
    • Iptables
    • tmux和screen
    • Systemd
    • OS 基础
    • jq基础
    • yum
    • neovim
  • Web
    • Html Css
    • Web部署
    • 缓存
  • Programming
    • 算法
      • 返回list中最大生序子序列长度
    • Python技巧
      • Python的语法糖
      • Python常用装饰器
      • AsyncIO基础
      • 自动化测试pytest
      • python中的下划线
      • 面向对象
      • Python的坑
      • Python配置文件管理
      • HTTP Stream Response
      • Python项目管理
    • 设计模式
      • 设计模式
      • 面向对象的思想
      • 编程概念
    • Go
      • Go 基础
      • Go常用功能
      • 结构体入门
    • 前端
    • Vue
    • NodeJS
  • Math
    • 多项式插值法
  • Security
    • HTTP常见攻击
    • 加密与签名
    • RSA
    • ECDSA
  • Solidity
    • Solidity基础
    • Blockchain Testnet Faucet
  • Tools
    • 视频处理ffmpeg
    • IDE配置
    • iTerm2美化
    • 密码管理
    • FRP配置
    • 工具集
由 GitBook 提供支持
在本页
  • 推导式
  • 列表推导式
  • 字典推导式
  • 集合推导式
  • 三元运算
  • 海象运算符
  • assset
  • 使用 * 和 ** 进行解包
  • 使用 functiontools
  • 通过 lru_cache 提高递归效率
  1. Programming
  2. Python技巧

Python的语法糖

推导式

从效率上看,推导式生成数据的方式,要比直接写 if 要快一些

列表推导式

比如生成0 到100里的偶数

list1 = [ i for i in range(0, 101) if i %2 == 0 ]

支持推导式的还有 元组、字典和集合

字典推导式

{ k: 1 for k in range(0,10) }

集合推导式

集合和列表的区别:不重复

{ s for s in range(0,101) }

三元运算

a = 15
print("a is 15") if a == 15 else print("a is not 15")

三元运算的嵌套: 可以先将第一个 if else 摘出来,然后写成三元运算符的 else 部分,也就是下面的 成绩错误的else部分写出来,但成立的部分,是另外一个 if else,而这个if else,则放在刚写出来的if 前面作为成立的部分。

score = 85

if  score in range(101):
    if score < 60:
        print("成绩不合格")
    else:
        print("成绩合格")
else:
    print("成绩错误!")

改成三元运算符为

print("成绩不合格") if score < 60 else print("成绩合格") if score in range(101) else print("成绩错误")

三元运算符是有返回值的,示例如下

score = 85
status = "ok" if score > 90 else "no"
print(status)

海象运算符

使用海象运算符,最主要的是能将中间过程存成变量。

  1. 将numbers 这个list的每个元素求平方,产生新list

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# 海象运算符
[ ( j := i * i ) for i in numbers ]
# 旧的列表推导式
 [ ( i * i) for i in numbers ]
  
  1. 将下面的list中长度大于4的单词提取出来,并说明其长度

 words = ['apple', 'banana', 'kiwi', 'pear', 'plum']
 [ (word, length) for word in words if (length := len(word)) > 4 ]

assset

通过 asset可以对某个属性值进行约束,asset是一种保护式编程。比如下面的示例,如果年龄不在0-150范围内,就会报错

def user(age: int):
    assert age > 0 and age < 150, '年龄不能为负数,也不能大于 150'
user( 20 )

使用 * 和 ** 进行解包

当我们对list或dict进行遍历的时候,可以用 * 或者 ** 对其进行操作(list 用 *, dict 用 **)

a = {"a": 1, "b": 2}
b = { "c": 3 , "d": 4}

如果只想取 a 和 b 字典中的key,那么用一个* 号,比如

print(*a)

合并a 和b 里的key的话,则可以用

{*a, *b}

如果想要取字典的全部,包括key 和 value,注意value 也可以是一个字典,或list,那么需要用两个星号

{**a, **b}

使用 functiontools

通过 lru_cache 提高递归效率

比如在进行递归的时候,有时候会有大量的重复计算,比如像计算斐波那契数列,那么可以用 funciotntools 的 lru_cache来提高效率

from functools import lru_cache

@lru_cache()
def fib(n):
    if n <= 2:
        return 1
    return fib(n-1) + fib(n-2)

print(fib(100))

如果递归深度超出了限制,那么可以用 sys.setrecursionlimit(5000) 来设置递归深度

上一页Python技巧下一页Python常用装饰器

最后更新于11个月前