ã¤ãã¬ã¼ã¿ï¼ã¸ã§ãã¬ã¼ã¿ï¼ãªã¹ãå æ¬è¡¨è¨ã¨éåæ§æè¨æ³
ã¤ãã¬ã¼ã¿ï¼ã¸ã§ãã¬ã¼ã¿ï¼ãªã¹ãå æ¬è¡¨è¨ã¨éåæ§æè¨æ³
ç§ã¯ä»Think Complexityã§ã¢ã«ã´ãªãºã ã¨è¤éç³»ã«ã¤ãã¦åå¼·ãã¦ãã¾ãï¼
ä»åã¯ï¼èªåã®åå¼·ãå
¼ãã¦ThinkComplexity(chap2
ã¨chap3
)ã§åå¼·ãã
- iterator(ã¤ãã¬ã¼ã¿)ã¨listã®éã
- generatorï¼ã¸ã§ãã¬ã¼ã¿ï¼
- list comprehensionï¼ãªã¹ãå æ¬è¡¨è¨ï¼
- ããã°ã©ãã®ããã®éåæ§æè¨æ³(Set-builder notation)
ã«ã¤ãã¦ã¾ã¨ããè¡ãããã¨æãã¾ãï¼ Think Complexity ã¯ç¡æã§èªãäºãã§ãã¾ãï¼
iteratorã¨ã¯
pythonã§ããiteratorã¨ã¯
__iter__
ã¡ã½ããã¨æ¬¡ã®è¦ç´ ãè¿ã
__next__
ã¡ã½ãããæã¤ãªãã¸ã§ã¯ãã®äºã§ãï¼
__next__
ã¡ã½ããã¯è¦ç´ ãç¡ããªãã¨
StopIteration
ä¾å¤ãéåºãã¾ãï¼
ä¸è¨ãiteratorã®å®è£
ä¾ã§ãï¼
counter.py
ã¨gen_counter.py
ã®
counter
ã¯ã©ã¹ã¯ç¡éã«1,2,3,4,5, . . . . .
ã¨ã«ã¦ã³ãããiteratorã§ãï¼
counter.py
ã®counter
ã¯ã©ã¹ã¯next
ã¡ã½ãããå¼ã°ããã¨
ã«ã¦ã³ããã¾ãï¼foræã§ã¯åãã«iter
ã¡ã½ãããå¼ã°ãï¼
次ã«next
ã¡ã½ãããå¼ã°ãã¾ãï¼
gen_counter.py
ã®counter
ã¯ã©ã¹ã¯iter
ãå¼ã°ããã¨
yield
æã使ã£ã¦ï¼ã«ã¦ã³ããè¡ãã¾ãï¼ãã®ããã«
yield
ã使ã£ã¦iteratorãç°¡åã«çæããé¢æ°ï¼ã¡ã½ããï¼ã
generatorï¼ã¸ã§ãã¬ã¼ã¿ã¼ï¼
ã¨å¼ã³ã¾ãï¼
ä»åã®ä¾ã§ã¯iter
ã¡ã½ãããã¸ã§ãã¬ã¼ã¿ã¼ã§ãï¼
ã¡ãªã¿ã«yield
ã¯é¢æ°ã»ã¡ã½ããå
ã§ã®ã¿ä½¿ç¨ããäºãå¯è½ã§ãï¼
iteratorã¨listã®éã
iteratorã¨listã®å¤§ããªéãã¯
for
æã使ãæã®éãã§ãï¼ãã¡ããå
ç¨èª¬æããéãï¼
iteratorã¯ã¤ãã¬ã¼ã¿ãããã³ã«ï¼__iter__
ã¨next
ã¡ã½ããï¼ã
å®è£
ãã¦ããå¿
è¦ãããããï¼ãªãã¸ã§ã¯ãçã«å
¨ãéãã¾ãï¼
ä¾ãã°ï¼è¾æ¸ãèµ°æ»ï¼traverseï¼ããã¨ãï¼ ä¸»ã«ï¼ã¤ã®æ¹æ³ãããã¾ãï¼
- ï¼ã¤ã¯
items
ã¡ã½ããã§ã¿ãã«ã®ãªã¹ããçæãã¦ï¼èµ°æ»ããæ¹æ³ï¼ ï¼ã¤ç®ã¯ï¼
iteritems
ã¡ã½ããã§ã¤ãã¬ã¼ã¿ãçæã㦠ã¤ã¿ã¬ã¼ã¿ãèµ°æ»ããæ¹æ³ï¼itemsã§ã®èµ°æ»
d = dict(a=1, b=2, c=3) for k, v in d.items(): print k, v
- iteritemsã§ã®èµ°æ»
d = dict(a=1, b=2, c=3) for k, v in d.iteritems(): print k, v
d.items()
ã¨ãããã¨ã§ï¼ã¿ãã«ã®ãªã¹ã[('a', 1), ('c', 3), ('b', 2)]
ãçæããã¾ãï¼ãã®ãªã¹ããçæããã®ã«O(n)
æããã¾ããï¼
d.iteritems()
ã§ã¤ãã¬ã¼ã¿ãªãã¸ã§ã¯ã
ãçæããã®ã«ã¯O(1)
ããæããã¾ããï¼
ã«ã¼ãã§èµ°æ»(O(n)
)ããããï¼d.items()
ã§ã¯O(n)+O(n)
ã§ãªã¼ãã¯O(n)
ï¼
d.iteritems()
ã§ã®èµ°æ»ã¯O(1)+O(n)
ã§ãªã¼ããO(n)
ã¨çµå±ãªã¼ãã¯å¤åãã¾ãããï¼
ä¿æ°ãå¤ã£ã¦ãããããªãã¼ããããæããäºãåºæ¥ã¾ãï¼
ã¾ãï¼ã¤ãã¬ã¼ã¿ã¯ãªã¹ãã«æ¯ã¹ã¦ã¡ã¢ãªãæ¶è²»ãã¾ããï¼
ãªã¹ãã¯nåã®è¦ç´ ãçæããã®ã«å¯¾ãã¦ï¼ã¤ãã¬ã¼ã¿ã¯1åã®ãªãã¸ã§ã¯ãã ãã§æ¸ãããããã§ãï¼
å¾ã£ã¦ï¼èµ°æ»ããæã¯iteratorã使ã£ã»ããè¯ãã§ãï¼ãã ãï¼
iteratorã¯èµ°æ»ä¸ã«iteratorã®ãµã¤ãºãå¤æ´ã§ããï¼indexæ¼ç®
(d['a']ã®ãããªæ¼ç®ã®äº)
ãåºæ¥ã¾ããï¼
ï¼æ³¨æ ãã®ä¾ã§ã¯print
æã®è¨ç®éãå«ãã§ãã¾ããï¼
list comprehension
ã¾ãã¯ãããªãä¾ãã
nums = [1,2,3,4,5] sqr = [] for num in nums: sqr.append(nums**2) print sqr # > [1, 4, 9, 16, 25]
ãã®ä¾ã§ã¯ï¼numsã®è¦ç´ ã2ä¹ãã¦æ°ãããªã¹ãsqr
ã«
追å ãã¦ãã¾ãï¼ããã¯ã¨ã¬ã¬ã³ãã¨ã¯è¨ãã¾ããããï¼
list comprehesionã使ãã¨ã¨ã¬ã¬ã³ãã§é«éã«ãªãã¾ãï¼
nums = [1,2,3,4,5] sqr = [num**2 for num in nums] print sqr
ãããlist comprehensionã§ãï¼list comprehension㯠ã¨ã¬ã¬ã³ãã«ãããã ãã§ãªãï¼ã«ã¼ããcè¨èªã§å®è¡ããã ããé«éã§ãï¼ã¾ããã¹ãããããã¨ãå¯è½ã§ãï¼
ãã¹ãã®ä¾:
tpl = [ (i,j) for i in range(10) for j in range(i) if i != i ]
ã¯æ¬¡ã®ã³ã¼ãã¨åãæå³ã«ãªãã¾ã
tpl = [] for i in range(10): for j in range(i): if i != j: tpl.append((i,j))
list comprehension
ã®ããã«generator
ãyiled
ã使ãã
ã«çæãããã¨ãã§ãã¾ãï¼ä»¥ä¸ã®ããã«()
ã§å²ããã¨ã§
generator
ãçæã§ãã¾ãï¼
nums = [1,2,3,4,5] sqr = (num**2 for num in nums) print sqr # <generator object <genexpr> at 0x10af870f0>
ããã°ã©ãã®ããã®éåæ§æè¨æ³ã®è§£é
Set-builder notation ï¼éåæ§æè¨æ³ï¼ ã¨ã¯æ°å¦ã§æ±ãéåã表ãè¨æ³ã§ï¼æ¡ä»¶ãå±æ§ã示ããããã¾ãï¼
ä¾ãã°ï¼
S = {2x| x â N, x^2 > 3}
ãããã¨ãã¾ãï¼ãããããã°ã©ã è¨æ³(list comprehension)ã§ç°¡åã«è¡¨ãäºãã§ãã¾ãï¼
ããã§ã®ï¼x
ã¯å¤æ°ã§ï¼x â N
ã¯å¤æ°xã®ç¯å²ã¨ãªãã¾ãï¼æ¬¡ã«x^2 > 3
ã¯æ¡ä»¶ã§ãï¼S
ã¯èªç¶æ°ã®ç¯å²ã§ï¼ãã¤x^2
ã3ãã大ããæ¡ä»¶ãæºããx
ã2
åããå¤ã®éåã¨ãªãã¾ãï¼
å¾ã£ã¦ï¼ãã®éåS
ã¯
N = range(10000) #Nã¯èªç¶æ°ã§ããï¼ããã§ã¯ç°¡æåã®ãã0 ~ 10000ã¨ãã¾ã S = [2*x for x in N if x**2 > 3]
ã¨è¡¨ç¾ãããã¨ãã§ãã¾ãï¼ ããã³ã³ãã¥ã¼ã¿ãµã¤ã¨ã³ã¹ãªã©ã®æ¬ã«åºã¦ãã éåã®è¨æ³ãããã°ã©ã 風ã«èããã°ãã£ãããããã¨ãããã¾ãï¼
ã¾ã¨ã
ä»æ¥ã¯Think Complexity
ã®chap2
ã¨chap3
ã§ç´¹ä»ããã¦ãã
iterator(ã¤ãã¬ã¼ã¿)ã¨listã®éãï¼generatorï¼ã¸ã§ãã¬ã¼ã¿ï¼ï¼
list comprehensionï¼ãªã¹ãå
æ¬è¡¨è¨ï¼
ã«ã¤ãã¦ã¾ã¨ãã¾ããï¼ãã¾ãã¨ãã¦Set-builder notation
ã«ã¤ãã¦ã
ç´¹ä»ãã¾ããï¼æ¬¡åã¯chap3
ã§åå¼·ããè¨ç®éï¼äºåæ¢ç´¢æ³ï¼ããã·ã¥ãã¼ãã«ã«ã¤ãã¦ç´¹ä»ãã¾ãï¼