今回の目的を果たすためであれば、条件を満たす最初のものを取り出すことができるはずです。
こんなやつ。
ここ を参考にしています。
python
1 from itertools import count
2 a = [ grundy_ceil [ n - i ] for i in range ( 1 , math . ceil ( n / 2 + 1 ) ) ]
3 j = next ( i for i in count ( ) if i not in a )
コードの速さが気になるのであれば、numpy.arrayへのキャストなくすべきでしょう。
また検索があるので、listやnp.arrayではなくsetを使うとだいぶ早くなります。
python
1 import math
2 import pandas as pd
3 from itertools import count
4 grundy_ceil = [ 0 ]
5
6 for n in range ( 1 , 101 ) :
7 a = set ( [ grundy_ceil [ n - i ] for i in range ( 1 , math . ceil ( n / 2 + 1 ) ) ] )
8 grundy_ceil . append ( next ( i for i in count ( ) if i not in a ) )
9
10 df = pd . DataFrame ( grundy_ceil ) . T
追記:完全に趣味ですが。ここ を参考に。
yag1kazさんのlistからの取り出し方も取り入れました。
sliceをリスト内包表記で書くと遅くなるのは盲点でした…
python
1 from timeit import timeit
2 from itertools import count
3 import math
4 import pandas as pd
5 import numpy as np
6
7 pd . options . display . max_columns = None
8 pd . options . display . notebook_repr_html = True
9
10 def f0 ( ) :
11 grundy_ceil = np . array ( [ 0 ] )
12 #grundy_ceil_1p = np.array([0])
13 for n in range ( 1 , 101 ) :
14 compare = np . array ( [ ] )
15 compare = np . append ( compare , [ grundy_ceil [ n - i ] for i in range ( 1 , math . ceil ( n / 2 + 1 ) ) ] )
16 for j in range ( compare . size + 1 ) :
17 if not j in compare :
18 grundy_ceil = np . append ( grundy_ceil , j )
19 break
20 df = pd . DataFrame ( grundy_ceil ) . T
21 return df
22
23 def f1 ( ) :
24 grundy_ceil = [ 0 ]
25 for n in range ( 1 , 101 ) :
26 a = [ grundy_ceil [ n - i ] for i in range ( 1 , math . ceil ( n / 2 + 1 ) ) ]
27 j = next ( i for i in count ( ) if i not in a )
28 grundy_ceil . append ( j )
29 df = pd . DataFrame ( grundy_ceil ) . T
30 return df
31
32 def f2 ( ) :
33 grundy_ceil = [ 0 ]
34 for n in range ( 1 , 101 ) :
35 a = np . array ( [ grundy_ceil [ n - i ] for i in range ( 1 , math . ceil ( n / 2 + 1 ) ) ] )
36 j = next ( i for i in count ( ) if i not in a )
37 grundy_ceil . append ( j )
38 df = pd . DataFrame ( grundy_ceil ) . T
39 return df
40
41 def f3 ( ) :
42 grundy_ceil = [ 0 ]
43 for n in range ( 1 , 101 ) :
44 a = set ( [ grundy_ceil [ n - i ] for i in range ( 1 , math . ceil ( n / 2 + 1 ) ) ] )
45 j = next ( i for i in count ( ) if i not in a )
46 grundy_ceil . append ( j )
47 df = pd . DataFrame ( grundy_ceil ) . T
48 return df
49
50 def f4 ( ) :
51 grundy_ceil = [ 0 ]
52 for n in range ( 1 , 101 ) :
53 a = set ( grundy_ceil [ : - math . ceil ( n / 2 + 1 ) : - 1 ] )
54 j = next ( i for i in count ( ) if i not in a )
55 grundy_ceil . append ( j )
56 df = pd . DataFrame ( grundy_ceil ) . T
57 return df
58
59 a = f0 ( )
60 b = f1 ( )
61 c = f2 ( )
62 d = f3 ( )
63 e = f4 ( )
64 print ( 'f1' , np . allclose ( a [ 0 ] . values , b [ 0 ] . values ) )
65 print ( 'f2' , np . allclose ( a [ 0 ] . values , c [ 0 ] . values ) )
66 print ( 'f3' , np . allclose ( a [ 0 ] . values , d [ 0 ] . values ) )
67 print ( 'f4' , np . allclose ( a [ 0 ] . values , e [ 0 ] . values ) )
68 '''
69 一応同じ結果になることを確認
70 f1 True
71 f2 True
72 f3 True
73 f4 True
74 '''
75 n = 1000
76 print ( 'Original {}' . format ( timeit ( f0 , number = n ) ) )
77 print ( 'list {}' . format ( timeit ( f1 , number = n ) ) )
78 print ( 'np.array {}' . format ( timeit ( f2 , number = n ) ) )
79 print ( 'set {}' . format ( timeit ( f3 , number = n ) ) )
80 print ( 'slice+set {}' . format ( timeit ( f4 , number = n ) ) )
81 '''
82 Original 9.65767323700129
83 list 1.1692692330107093
84 np.array 7.38920716199209
85 set 0.9272018460032996
86 slice+set 0.6420557640085462
87 '''
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/17 06:17
2017/11/17 09:35
2017/11/17 09:39
2017/11/17 10:03 編集
2017/11/17 10:08
2017/11/17 10:19