回答編集履歴
2
追記
test
CHANGED
@@ -43,3 +43,95 @@
|
|
43
43
|
x_list = list(chain.from_iterable(zip(*tee(x_org, 2))))
|
44
44
|
|
45
45
|
```
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
----
|
52
|
+
|
53
|
+
追記
|
54
|
+
|
55
|
+
ちなみになんですが、質問に書いてあるコードが最速だと思います。
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
```plain
|
60
|
+
|
61
|
+
In [1]: from itertools import chain, repeat, tee
|
62
|
+
|
63
|
+
...:
|
64
|
+
|
65
|
+
...: def a(x_org):
|
66
|
+
|
67
|
+
...: return [x for x in x_org for _i in range(2)]
|
68
|
+
|
69
|
+
...:
|
70
|
+
|
71
|
+
...: def aa(x_org):
|
72
|
+
|
73
|
+
...: return [y for x in ['a', 'b', 'c', 'd'] for y in [x] * 2]
|
74
|
+
|
75
|
+
...:
|
76
|
+
|
77
|
+
...: def b(x_org):
|
78
|
+
|
79
|
+
...: return list(chain.from_iterable([x] * 2 for x in x_org))
|
80
|
+
|
81
|
+
...:
|
82
|
+
|
83
|
+
...: def c(x_org):
|
84
|
+
|
85
|
+
...: return list(chain.from_iterable(zip(*tee(x_org, 2))))
|
86
|
+
|
87
|
+
...:
|
88
|
+
|
89
|
+
...: def d(x_org):
|
90
|
+
|
91
|
+
...: x_list = []
|
92
|
+
|
93
|
+
...: for x in x_org:
|
94
|
+
|
95
|
+
...: x_list.append(x)
|
96
|
+
|
97
|
+
...: x_list.append(x)
|
98
|
+
|
99
|
+
...: return x_list
|
100
|
+
|
101
|
+
...:
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
In [2]: x_org = ['a', 'b', 'c', 'd']
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
In [3]: %timeit a(x_org)
|
110
|
+
|
111
|
+
1.33 µs ± 44.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
In [4]: %timeit aa(x_org)
|
116
|
+
|
117
|
+
991 ns ± 29.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
In [5]: %timeit b(x_org)
|
122
|
+
|
123
|
+
1.25 µs ± 5.31 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
In [6]: %timeit c(x_org)
|
128
|
+
|
129
|
+
1.08 µs ± 42.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
In [7]: %timeit d(x_org)
|
134
|
+
|
135
|
+
617 ns ± 25.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
|
136
|
+
|
137
|
+
```
|
1
些細
test
CHANGED
@@ -30,7 +30,7 @@
|
|
30
30
|
|
31
31
|
|
32
32
|
|
33
|
-
(3)teeとzipで`
|
33
|
+
(3)teeとzipで`('a', 'a'), ('b', 'b'),,,` のシーケンスを作ってflatten
|
34
34
|
|
35
35
|
```python
|
36
36
|
|