質問するログイン新規登録

回答編集履歴

1

誤り訂正

2019/09/21 00:36

投稿

nomuken
nomuken

スコア1627

answer CHANGED
@@ -1,16 +1,30 @@
1
1
  numpyのvectorizeかfrompyfuncを使う方法があると思います。
2
+ が、型が混在する配列の場合はfrompyfunc一択のようです。
2
3
 
3
- とりあえずvectorizeを使う場合
4
4
  ```Python
5
5
  import numpy as np
6
6
 
7
- f = np.vectorize(lambda _a, _b: _a if _a is not None else _b)
7
+ f = np.vectorize(lambda _a, _b: _a or _b)
8
8
 
9
9
  a = np.array([None, None, 1, 3])
10
10
  b = np.array([True, 'hoge', 2, 0])
11
11
 
12
- print(f(a,b))
12
+ r = f(a,b)
13
+ print(r)
14
+ print([type(_r) for _r in r ])
15
+
16
+ f = np.frompyfunc(lambda _a, _b: _a or _b, 2, 1)
17
+ r = f(a,b)
18
+
19
+ print(r)
20
+ print([type(_r) for _r in r])
13
21
  ```
14
22
 
23
+ ```result
24
+ ['True' 'hoge' '1' '3']
25
+ [<class 'numpy.str_'>, <class 'numpy.str_'>, <class 'numpy.str_'>, <class 'numpy.str_'>]
15
- 性能はfrompyfuncのほうが早いという記事もあります。
26
+ ['True' 'hoge' 1 3]
27
+ [<class 'str'>, <class 'str'>, <class 'int'>, <class 'int'>]
28
+ ```
29
+
16
- [Python高速化実験~map関数とか~](https://takala.tokyo/takala_wp/2018/11/28/736/)
30
+ 参考記事:[Python高速化実験~map関数とか~](https://takala.tokyo/takala_wp/2018/11/28/736/)