mirror of
https://github.com/apachecn/ailearning.git
synced 2026-04-24 10:34:08 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
import operator as op
|
||||
|
||||
```
|
||||
@@ -15,12 +15,12 @@ import operator as op
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
print reduce(op.add, range(10))
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
45
|
||||
|
||||
```
|
||||
@@ -29,12 +29,12 @@ print reduce(op.add, range(10))
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
print reduce(op.mul, range(1,10))
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
362880
|
||||
|
||||
```
|
||||
@@ -43,7 +43,7 @@ print reduce(op.mul, range(1,10))
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
my_list = [('a', 1), ('bb', 4), ('ccc', 2), ('dddd', 3)]
|
||||
|
||||
# 标准排序
|
||||
@@ -57,7 +57,7 @@ print sorted(my_list, key=lambda x: len(x[0]))
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
[('a', 1), ('bb', 4), ('ccc', 2), ('dddd', 3)]
|
||||
[('a', 1), ('ccc', 2), ('dddd', 3), ('bb', 4)]
|
||||
[('a', 1), ('bb', 4), ('ccc', 2), ('dddd', 3)]
|
||||
@@ -70,7 +70,7 @@ print sorted(my_list, key=lambda x: len(x[0]))
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
from functools import partial
|
||||
|
||||
# 将 reduce 的第一个参数指定为加法,得到的是类似求和的函数
|
||||
@@ -84,7 +84,7 @@ print prod_([1,2,3,4])
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
10
|
||||
24
|
||||
|
||||
@@ -98,7 +98,7 @@ print prod_([1,2,3,4])
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
from itertools import cycle, groupby, islice, permutations, combinations
|
||||
|
||||
```
|
||||
@@ -107,12 +107,12 @@ from itertools import cycle, groupby, islice, permutations, combinations
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
print list(islice(cycle('abcd'), 0, 10))
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b']
|
||||
|
||||
```
|
||||
@@ -121,7 +121,7 @@ print list(islice(cycle('abcd'), 0, 10))
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
animals = sorted(['pig', 'cow', 'giraffe', 'elephant',
|
||||
'dog', 'cat', 'hippo', 'lion', 'tiger'], key=len)
|
||||
|
||||
@@ -132,7 +132,7 @@ print
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
3 ['pig', 'cow', 'dog', 'cat']
|
||||
4 ['lion']
|
||||
5 ['hippo', 'tiger']
|
||||
@@ -145,12 +145,12 @@ print
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
print [''.join(p) for p in permutations('abc')]
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
|
||||
|
||||
```
|
||||
@@ -159,12 +159,12 @@ print [''.join(p) for p in permutations('abc')]
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
print [list(c) for c in combinations([1,2,3,4], r=2)]
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
|
||||
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user