【python学习笔记】python if和for的多种写法
来源:互联网 发布:数据录入员压力大吗 编辑:程序博客网 时间:2024/06/05 12:08
a, b, c = 1, 2, 3
【对比Cpp里:c = a >b? a:b】这个写法,Python只能常规的空行,缩进吗?
人生苦短,我用python,下面介绍几种if的方便的方法。
1.常规
if a>b: c = aelse: c = b2.表达式
c = a if a>b else b3.二维列表
c = [b,a][a>b]
········································································
还有for 循环的 用list解析的
[对(x)的操作 for x in 集合 if 条件]
[对(x,y)的操作 for x in 集合1 for y in 集合2 if 条件]
举一个简单的例子:
x=[1,2,3,4]
y=[5,6,7,8]
我想让着两个list中的偶数分别相加,应该结果是2+6,4+6,2+8,4+8
下面用一句话来写
[a + b for a in x for b in y if a%2 == 0 and b%2 ==0]之前的博客里用到的
import os from os.path import join, getsize def getdirsize(dir): size = 0L for root, dirs, files in os.walk(dir): size += sum([getsize(join(root, name)) for name in files]) return size
[ x for x in one if x%2==0 ]
阅读全文
0 0
- 【python学习笔记】python if和for的多种写法
- Python if 和 for 的多种写法
- Python中if-else语句的多种写法
- Python中if-else语句的多种写法
- Python中if-else语句的多种写法
- Python中if-else语句的多种写法
- Python--关于for和if的一些
- 《笨办法学 Python》 学习笔记04 if-while-for
- Python学习笔记(九):if、while、for语句
- python学习笔记-3.1python运算符和if判断
- python的for if语法
- python 学习笔记 if语句
- Python学习笔记之IF
- Python 列表推导式 一些小的古怪写法 if else for in join
- Python笔记(四) if,while,for
- Python中for循环和if判断语句的使用
- Python if和for条件语句
- Python之if,for和while循环
- jmeter几种启动方式
- 【黑科技】备份github仓库
- 解决Ubuntu上ADB devices找不到设备的问题
- 关键时刻(Moment Of Truth)
- Linux统计/监控工具SAR详细介绍
- 【python学习笔记】python if和for的多种写法
- Python-Day04-装饰器(1)
- Cg Programming/Unity/Diffuse Reflection漫反射
- 第一次写博客
- 递归SQL简单学习
- 大白菜装机步骤
- Java集合之泛型
- activity启动流程记录
- AGC 018