python
  • 简介
  • python
  • 基本数据类型总结
  • 变量与运算符
  • 流程控制语句
  • 项目的组织结构
  • 面向对象
  • 正则表达式的学习过程
  • 初识JSON
  • 闭包
  • 枚举
  • 函数式编程
  • 装饰器
  • 用字典代替switch
  • 列表推导式
  • 有关于None
  • 补充
  • 文件操作
  • 异常
  • 网络编程
Powered by GitBook
On this page
  • 列表推导式
  • 集合字典也可
  • 元组也可
  • 当为字典时

Was this helpful?

列表推导式

列表推导式

集合字典也可

元组也可

a=[1,2,3,4,5,6,7,8,9]
b=[i**3 for i in a if i <=5]
print(b)   #b={....}为集合,b(...)为元组,与a{[(...)]},什么的无关。
#为b()时,是一个对象

也可以map filter表示

list_a=[1,2,3,4,5,6,7,8,9]
r=filter(lambda x:x if x<=5 else 0,list_a)
s=map(lambda x:x*x,r)
print(list(s))
#同上,不建议
list_a=[1,2,3,4,5,6,7,8,9]
r=filter(lambda x:x if x<=5 else 0,list_a)
s=map(lambda x:x**3,filter(lambda x:x if x<=5 else 0,list_a))
print(list(s))

当为字典时

students ={
    'wei':18,
    'lai':19,
    'wan':20
}
b = [key for key,value in students.items()]
print(b)#['wei', 'lai', 'wan']
for x in b:
    print(x)#wei#lai#wan

**交换key和value

students ={
    'wei':18,
    'lai':19,
    'wan':20
}

b ={value:key for key,value in students.items()}
print(b)#{18: 'wei', 19: 'lai', 20: 'wan'}
Previous用字典代替switchNext有关于None

Last updated 6 years ago

Was this helpful?