python六剑客

切片
序列:字符串,列表,元组
#开始位置<终止位置
终止位置为空:表示从开始位置取到最后一个位置(步长为正:正着取  步长为负:倒着取)

l[start:end:span]
遍历 [start,end),间隔为 span,当 span>0 时顺序遍历, 当 span<0 时,逆着遍历。
start 不输入则默认为 0,end 不输入默认为长度。

>>> a=list(range(10))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[:]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[::]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[::2]
[0, 2, 4, 6, 8]
>>> a[::-2]
[9, 7, 5, 3, 1]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[0::-1]
[0]
>>> a[0:9:-1]
[]
>>> a[-1:0:-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1]

map(func, *iterables) --> map object

 Make an iterator that computes the function using arguments from each of the iterables.  Stops when the shortest iterable is exhausted.

#1.与lambda函数配合使用

>>> map(lambda x:x+1,[1,2,3])

#返回值类型:map object

<map object at 0x0000016D261CD400>
>>> list(map(lambda x:x+1,[1,2,3]))
[2, 3, 4]

#2.与自定义函数,配合使用

>>> def func(x):
...     x=x+1
...     return x
...
>>> list(map(func,[1,2,3]))
[2, 3, 4]

filter(function or None, iterable) --> filter object

Return an iterator yielding those items of iterable for which function(item) is true. If function is None, return the items that are true.

Filter()函数可以对序列做过滤处理,就是说可以使用一个自定义的函数过滤一个序列,把序列的每一项传到自定义的过滤函数里处理,并返回结果做过滤。最后一次性返回过滤后的结果。(只保存返回为True的部分,返回为False的部分会被过滤掉)

Filter()函数有两个参数:

第一个,函数名或None,必须

第二个,需要过滤的列表,必须

#1.与lambda函数配合使用

>>> filter(lambda x:x>3,[1,2,4,5])

#返回值类型:filter object
<filter object at 0x0000016D25EC3438>
>>> list(filter(lambda x:x>3,[1,2,4,5]))
[4, 5]

#2.与自定义函数,配合使用

>>> def func(x):
...     if x>3:
...         return x
...
>>> list(filter(func,[1,2,4,5]))
[4, 5]

#3.function is None

>>> list(filter(None,['',(),{},True,False,0,1]))
[True, 1]

推导列表---包含表达式,for 循环,if判断句

>>> [x for x in range(5)]
[0, 1, 2, 3, 4]
>>> [x*2 for x in range(1,11)]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
>>> [x*2 for x in range(5) if x%2==0]
[0, 4, 8]
>>> [int(x+y) for x in ["1","2","3"] for y in map(str,range(10)) if int(x+y)<=30
... ]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]

>>> d = {'x': 'A', 'y': 'B', 'z': 'C' }
>>> print ([k + '=' + v for k, v in d.items()])
['x=A', 'y=B', 'z=C']

#计算key的和
>>> sum([key  for key in {1:'a',2:"b","a":3} if isinstance(key,int)])
3

#计算key和value的和

>>> sum([key  for key in {1:'a',2:"b","a":3} if isinstance(key,int)]+[value  for value in {1:'a',2:"b","a":3}.values() if isinstance(value,int)])

6

#大写字母转小写字母

>>> L = ['Hello', 'World', 'IBM', 'Apple']
>>> print ([s.lower() for s in L])
['hello', 'world', 'ibm', 'apple']

#数组转化1----[[1,2,3],[4,5,6],[7,8,9],[10,11,12]] → [[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]

for i in range(3):因为有3列,所以遍历3次
for j in a:遍历每一行
当i =0时:[j[i] for j in a] 表示获取每行的第1列,放到列表中
当i =1时:[j[i] for j in a] 表示获取每行的第2列,放到列表中
当i =2时:[j[i] for j in a] 表示获取每行的第3列,放到列表中

>>> a=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
>>> print ([ [j[i] for j in a] for i in range(3)])
[[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]

#数组转化2----[[1,2,3],[4,5,6],[7,8,9],[10,11,12]] → [[2, 3], [5, 6], [8, 9], [11, 12]]

for i in a:遍历列表的每一行
for j in [1,2]:遍历列表的第二列和第三列
j分别取1,2代表列表的第二列和第三列
[i[j] for j in [1,2]]:
表示每一行的第二列和第三列元素,存到子列表中

>>> a=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
>>> print ([[i[j] for j in [1,2]] for i in a])
[[2, 3], [5, 6], [8, 9], [11, 12]]

reduce

reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence,from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates    ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.

reduce内建函数是一个二元操作函数,他用来将一个数据集合(列表,元组等)中的所有数据进行下列操作:用传给reduce中的函数func()(必须是一个二元操作函数)先对集合中的第1,2数据进行操作,得到的结果再与第三个数据用func()函数运算,最后得到一个结果。

#使用前,必须先引入reduce

>>> from functools import reduce

例:累加

>>> lst = [1,2,3,4,5]

>>> print reduce(lambda x,y:x+y,lst)

15

>>> reduce(lambda x,y:x+y,range(1,101))

5050

例:累乘

>>> reduce(lambda x,y:x*y,range(1,5))

24

猜你喜欢

转载自blog.csdn.net/wqq5321/article/details/82899056