回答編集履歴
1
combinationsとかありえない
answer
CHANGED
@@ -8,4 +8,22 @@
|
|
8
8
|
```Python
|
9
9
|
sentence = ["I","can","not","understand","deep","learning"]
|
10
10
|
result = [sentence[i] for i in sorted(random.sample(range(len(sentence)), k=3))]
|
11
|
+
```
|
12
|
+
|
13
|
+
----
|
14
|
+
|
15
|
+
```
|
16
|
+
In [1]: import itertools, random
|
17
|
+
|
18
|
+
In [2]: sentence = sorted(list(map(str, range(1000))))
|
19
|
+
|
20
|
+
In [3]: %timeit [sentence[i] for i in sorted(random.sample(range(len(sentence)), k=3))]
|
21
|
+
6.7 µs ± 99.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
|
22
|
+
|
23
|
+
In [4]: %timeit [k for i, k in sorted(random.sample(list(enumerate(sentence)), k=3))]
|
24
|
+
49.4 µs ± 604 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
|
25
|
+
|
26
|
+
In [5]: %time random.choice(list(itertools.combinations(sentence, 3)))
|
27
|
+
CPU times: user 23.3 s, sys: 15 s, total: 38.2 s
|
28
|
+
Wall time: 41.6 s
|
11
29
|
```
|