python中的map
>>> seg = range(8)
>>> def square(x):return x*x
...
>>> map(None,seg,map(square,seg))
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)]
>>>
方法reduce(func,sequence)
>>> def add(x,y):return x+y
...
>>> reduce(add,range(1,11))
55
>>>
现已序列的前两个元素调用函数,然后以返回值和下一个元素,依次类推退
>>> def sum(seq):
... def add(x,y):return x+y
... return reduce(add,seq,0)
...
>>> sum(range(1,11))
55
>>>
通过第三个参数初始化
链表推导式
>>> freshfruit = ['banana','apple','orange']
>>> [weapon.strip() for weapon in freshfruit]
['banana', 'apple', 'orange']
>>> weapon
'orange'
>>> vec = [2,4,6]
>>> [3*x for x in vec]
[6, 12, 18]
>>> [3*x for x in vec]
[6, 12, 18]
>>> [3*x for x in vec if x > 3]
[12, 18]
>>> [[x,x**2] for x in vec]
[[2, 4], [4, 16], [6, 36]]
>>>
del语句从链表中删除指定元素:
>>> a = [-1,1,66.6,333,333,1234.5]
>>> del a[0]
>>> a
[1, 66.599999999999994, 333, 333, 1234.5]
>>> del a
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>>
元祖(tuples)和(sequence)
>>> t = 12,23,34,45
>>> x,y,z,a = a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> x,y,z,a = t
>>> x
12
>>> y
23
>>>