[Head First Python] 5. summary

1- "原地"排序-转换后替换

>>> list = [2,1,3]

>>> list.sort()

>>> list

[1, 2, 3]

降序 reverse = True

>>> list.sort(reverse = True)
>>> list
[3, 2, 1, 1]

2- "复制"排序-转换然后返回

>>> data = []

>>> list = [3,2,4,1]

>>> data = sorted(list)

>>> data

[1, 2, 3, 4]

>>> list

[3, 2, 4, 1]

>>>

降序参数 reverse = True

>>> data = sorted(list, reverse = True)
>>> data
[5, 3, 2, 1] 

3- "方法串链"-从左向右读,对数据应用一组方法

1     try:
2         with open(filename) as f:
3             data = f.readline()
4             return ( data.strip().split(,))

4- "函数串链"-从右向左读,对数据应用一组函数

print( sorted( set ([sanitize(s) for s in julie]) )[0:3] ) 

 5- "列表推导" - 在一行上指定一个转换(不是使用迭代)

>>> new_l = []
>>> for each_item in old_l:
...     new_l.append(len(each_item))

可用下面方法替换

>>> new_l = []
>>> new_l = [ len(s) for s in old_l ]

6- "分片" 从一个列表访问多个列表项 [1:2] 不包含2,只显示第1个项目, 从0开始

>>> list = [1,2,3,4]
>>> list[1:2]
[2]
>>> list[1:3]
[2, 3]
>>> 

7- "集合"- 一组无序的数据项,其中不包含重复项,使用set工厂

>>> list = [1,1,2,3]
>>> set( list )
{1, 2, 3}

 

print( sorted( set ([sanitize(s) for s in james]) )[0:3] )

 

 

[Head First Python] 5. summary,布布扣,bubuko.com

[Head First Python] 5. summary

上一篇:I2C通信--读书笔记


下一篇:Java垃圾回收机制