lhl
首页
python
leetcode
产品思想
软件测试
博客 (opens new window)
github (opens new window)
首页
python
leetcode
产品思想
软件测试
博客 (opens new window)
github (opens new window)
  • python

    • Python 基础

    • Python 数据库

    • 面向对象

    • Python Web

    • Python 进阶

      • 协程
      • 装饰器
      • decorator
      • filter
      • 列表生成式
        • 列表生成式
      • 进程&线程
      • 生成器
      • 匿名函数
      • map&&reduce
      • venv
  • leetcode

  • 软件测试

  • Git

  • linux

  • 产品

  • MySql

  • docker

  • python
  • 进阶
2023-07-14
目录

列表生成式

# 列表生成式

列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式。

# 简单使用

## 生成 [1*1,2*2,3*3,...n*n] 的列表
[n * n for n in range(1,11)]
## for 循环后 + 判断 if
## 仅偶数的平方
[n * n for n in range(1,11) if n % 2 == 0]
## 两层循环
## 99乘法
[x * y for x in range(1,10) for y in range(1,10)]
## 测试题
## 来源: https://www.liaoxuefeng.com/wiki/1016959663602400/1017317609699776
## 生成 L1 的小写 List
L1 = ['Hello', 'World', 18, 'Apple', None]
[i.lower() for i in L1 if isinstance(i, str)]
filter
进程&线程

← filter 进程&线程→

最近更新
01
lhl learn notes
02
filter
06-09
03
decorator
06-09
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式