ã°ã©ããšããªãŒã®å®è£
ããšãã°ãæçã«ãŒãã«æ²¿ã£ãŠãã€ã³ãã移åããåé¡ãªã©ãå€ãã®ã¿ã¹ã¯ã¯ã ã°ã©ãã䜿çšããŠæã匷åãªããŒã«ã®1ã€ã䜿çšããŠè§£æ±ºã§ããŸã ã å€ãã®å Žåãã°ã©ãã®åé¡ã解決ããŠãããšå€æã§ããã°ãå°ãªããšã解決çã®ååã«ãªã£ãŠããŸãã ãŸããããŒã¿ãäœããã®åœ¢ã§ããªãŒãšããŠè¡šçŸã§ããå Žåãçã«å¹æçãªãœãªã¥ãŒã·ã§ã³ãæ§ç¯ããããããæ©äŒããããŸãã
ã°ã©ãã¯ããã©ã³ã¹ããŒããããã¯ãŒã¯ããããŒã¿äŒéãããã¯ãŒã¯ãŸã§ã现èæ žå ã®ã¿ã³ãã¯è³ªã®çžäºäœçšããã€ã³ã¿ãŒãããäžã®äººã ã®ã€ãªãããŸã§ãããããæ§é ãŸãã¯ã·ã¹ãã ãè¡šãããšãã§ããŸãã
éã¿ãè·é¢ãªã©ã®ããŒã¿ãè¿œå ãããšãã°ã©ããããã«äŸ¿å©ã«ãªããŸããããã«ããããã§ã¹ãããããèœåã«å¿ããŠäººã®é©åãªä»äºã決å®ãããªã©ãããŸããŸãªåé¡ã説æã§ããŸãã
ããªãŒã¯ç¹å¥ãªçš®é¡ã®ã°ã©ãã§ãããããã»ãšãã©ã®ã°ã©ãã¢ã«ãŽãªãºã ãšè¡šçŸã¯ãããã«å¯ŸããŠãæ©èœããŸãã
ãã ããç¹æ®ãªç¹æ§ïŒæ¥ç¶æ§ãšãµã€ã¯ã«ã®äžè¶³ïŒã«ãããã¢ã«ãŽãªãºã ïŒããã³è¡šçŸïŒã®ç¹æ®ãªïŒéåžžã«åçŽãªïŒããŒãžã§ã³ã䜿çšããããšãå¯èœã§ãã
å®éã«ã¯ãå Žåã«ãã£ãŠã¯ãããªãŒåœ¢åŒã§è¡šçŸã§ããæ§é ïŒXMLããã¥ã¡ã³ãããã£ã¬ã¯ããªéå±€ãªã©ïŒããããŸãïŒIDREFå±æ§ãšã·ã³ããªãã¯ãªã³ã¯ãèæ ®ã«å ¥ãããšãXMLããã¥ã¡ã³ããšãã£ã¬ã¯ããªéå±€ã¯ã°ã©ãã«ãªããŸãïŒã å®éããããã®ãäžéšã®ãã±ãŒã¹ã¯éåžžã«äžè¬çã§ãã
ã°ã©ãã«é¢ããåé¡ã®èª¬æã¯ããªãæœè±¡çãªããããœãªã¥ãŒã·ã§ã³ãå®è£ ããå¿ èŠãããå Žåã¯ãã°ã©ããããŒã¿æ§é ãšããŠè¡šãå¿ èŠããããŸãã ïŒããã¯ãã¢ã«ãŽãªãºã ãèšèšãããšãã«ãè¡ãå¿ èŠããããŸããããã¯ãã°ã©ãè¡šçŸã§ããŸããŸãªæäœãå®è¡ãããæéãç¥ãå¿ èŠãããããã§ãïŒã å Žåã«ãã£ãŠã¯ãã°ã©ãã¯ãã§ã«ã³ãŒããŸãã¯ããŒã¿ã§äœæãããŠãããããå¥ã®æ§é ã¯å¿ èŠãããŸããã ããšãã°ããªã³ã¯ãä»ããŠãµã€ãã«é¢ããæ å ±ãåéããWebã¯ããŒã©ãŒãèšè¿°ããå Žåãã°ã©ãã¯ãããã¯ãŒã¯èªäœã«ãªããŸãã friendså±æ§ïŒ Personåã®ä»ã®ãªããžã§ã¯ãã®ãªã¹ãïŒãæã€Personã¯ã©ã¹ãããå Žåããªããžã§ã¯ãã¢ãã«ã¯æ¢ã«ããŸããŸãªã¢ã«ãŽãªãºã ã䜿çšã§ããã°ã©ãã«ãªããŸãã ãã ããã°ã©ããè¡šãç¹å¥ãªæ¹æ³ããããŸãã
äžè¬çã«ã¯ã N [v]ã vã«é£æ¥ããé ç¹ã®ã³ã¬ã¯ã·ã§ã³ïŒãŸãã¯å Žåã«ãã£ãŠã¯å埩åïŒã«ãªãããã«ãé£æ¥é¢æ°NïŒvïŒãå®è£ ããæ¹æ³ãæ¢ããŠããŸãã ä»ã®å€ãã®æ¬ãšåæ§ã«ãæãæçšã§äžè¬åãããŠããããã2ã€ã®æãããç¥ãããŠããè¡šçŸã é£æ¥ãªã¹ããšé£æ¥ãããªãã¯ã¹ã«çŠç¹ãåœãŠãŸã ã 代æ¿è¡šçŸã«ã€ããŠã¯ã以äžã®æåŸã®ã»ã¯ã·ã§ã³ã§èª¬æããŸãã
ãã©ãã¯ããã¯ã¹ïŒèŸæžãšã»ãã
, , Python, â <em></em>. ( ), . , , ( , ). Python hash: >>> hash(42) 42 >>> hash("Hello, world!") -1886531940 , -. . , ( -, ). , Î(1). ( Î(n), -. , ). , dict set , .
é£æ¥ãªã¹ããªã©
ã°ã©ããè¡šãæãæçœãªæ¹æ³ã®1ã€ã¯ãé£æ¥ãªã¹ãã§ãã ãã®æå³ã¯ãåé ç¹ã«ã€ããŠãããã«é£æ¥ããé ç¹ã®ãªã¹ãïŒãŸãã¯ã»ãããå¥ã®ã³ã³ãããã€ãã¬ãŒã¿ïŒãäœæããããšããããšã§ãã 0 ... n-1ãšçªå·ä»ããããnåã®é ç¹ããããšä»®å®ããŠããã®ãããªãªã¹ããå®è£ ããæãç°¡åãªæ¹æ³ãåæããŸãããã
, . 0⊠n-1 , .
ãããã£ãŠãåé£æ¥ãªã¹ãã¯ãã®ãããªçªå·ã®ãªã¹ãã§ããããããã®ãªã¹ãèªäœã¯ãé ç¹çªå·ã§ã€ã³ããã¯ã¹ä»ãããããµã€ãºnã®ã¡ã€ã³ãªã¹ãã«åéãããŸãã éåžžããã®ãããªãªã¹ãã®ãœãŒãã¯ã©ã³ãã ã§ãããããç§ãã¡ã話ããŠããã®ã¯ãé£æ¥ã»ãããå®è£ ããããã®ãªã¹ãã®äœ¿çšã§ãã çšèªãªã¹ãã¯ãåã«æŽå²çã«ç¢ºç«ããããã®ã§ãã 幞ããªããšã«ãPythonã«ã¯ã»ããçšã®å¥ã®ã¿ã€ãããããå€ãã®å Žåã䜿ããããã§ãã
ããŸããŸãªè¡šçŸã衚瀺ãããã°ã©ãã®äŸãå³ã«ç€ºããŸãã 1.ãŸãããã¹ãŠã®é ç¹ã«çªå·ãä»ããããŠãããšä»®å®ããŸãïŒ a = 0ãb = 1ã... ïŒã ããã念é ã«çœ®ããŠã次ã®ãªã¹ãã«ç€ºãããã«ãã°ã©ããæçœãªæ¹æ³ã§è¡šãããšãã§ããŸãã 䟿å®äžãå³ã®é ç¹ã©ãã«ã«åŸã£ãŠååãä»ããããå€æ°ã«é ç¹çªå·ãå²ãåœãŠãŸããã ãã ããæ°å€ãçŽæ¥æäœã§ããŸãã ã©ã®ããããã©ã®ãªã¹ãã«å±ãããã¯ãã³ã¡ã³ãã«ç€ºãããŠããŸãã å¿ èŠã«å¿ããŠããã¥ãŒãåçãšäžèŽããããšã確èªããããã«æ°åããããŸãã
a, b, c, d, e, f, g, h = range(8) N = [ {b, c, d, e, f}, # a {c, e}, # b {d}, # c {e}, # d {f}, # e {c, g, h}, # f {f, h}, # g {f, g} # h ]
Python 2.7 ( 3.0) set([1, 2, 3]) {1, 2, 3}. - set(), {} .
å³ 1.ããŸããŸãªçš®é¡ã®ãã¬ãŒã³ããŒã·ã§ã³ã瀺ãããããã®ã°ã©ã
ãªã¹ãã®ååNã¯ãäžèšã®é¢æ°Nã«é¢é£ä»ããããŠããŸãã ã°ã©ãçè«ã§ã¯ã NïŒvïŒã¯vã«é£æ¥ããé ç¹ã®ã»ãããè¡šããŸãã åæ§ã«ããã®ã³ãŒãã§ã¯ã N [v]ã¯vã«é£æ¥ããé ç¹ã®ã»ããã§ãã Nãäžèšã®äŸã®ããã«å®çŸ©ãããŠãããšä»®å®ãããšãPython察話ã¢ãŒãã§ãã®ãã¥ãŒãæ¢çŽ¢ã§ããŸãã
>>> b in N[a] # ? True >>> len(N[f]) # 3
: , , , , , python -i, : python -i listing_2_1.py , , , .
å¥ã®èãããããã¥ãŒã¯ãå Žåã«ãã£ãŠã¯ãªãŒããŒããããå°ãªããªããŸãããé£æ¥ãªã¹ãèªäœã§ãã ãã®ãããªãªã¹ãã®äŸãã以äžã®ãªã¹ãã«ç€ºããŸãã ãã¹ãŠåãæäœãå©çšå¯èœã§ãããé ç¹é£æ¥ãã§ãã¯ã¯ÎïŒnïŒã§å®è¡ãããŸãã ããã«ãããé床ãå€§å¹ ã«äœäžããŸããããã®ãã¬ãŒã³ããŒã·ã§ã³ãæ¬åœã«å¿ èŠãªå Žåã¯ãããã圌ã®å¯äžã®åé¡ã§ãã ïŒã¢ã«ãŽãªãºã ãè¡ãããšã¯ãã¹ãŠãé£æ¥ããé ç¹ããã€ãã¹ããããšã§ããå Žåãã¿ã€ãã»ããã®ãªããžã§ã¯ãã䜿çšããããšã¯ç¡æå³ã§ã¯ãããŸããïŒãªãŒããŒãããã¯ãå®è£ ã®æŒžè¿çåäœã«ãããäžå®ã®èŠå ãæªåãããå¯èœæ§ããããŸãïŒã
a, b, c, d, e, f, g, h = range(8) N = [ [b, c, d, e, f], # a [c, e], # b [d], # c [e], # d [f], # e [c, g, h], # f [f, h], # g [f, g] # h ]
ãã®è¡šçŸã¯å®éã«ã¯é£æ¥é åã®ã»ããã§ãããåŸæ¥ã®é£æ¥ãªã¹ãã§ã¯ãªãããšã䞻匵ã§ããŸã ã Pythonã®ãªã¹ãã¿ã€ãã¯ãå®éã«ã¯åçé åã§ãã å¿ èŠã«å¿ããŠããªã³ã¯ãªã¹ãã¿ã€ããå®è£ ããPythonã®ãªã¹ãã¿ã€ãã®ä»£ããã«äœ¿çšã§ããŸãã ããã«ãããããã©ãŒãã³ã¹ã®èŠ³ç¹ãããªã¹ããžã®ä»»æã®æ¿å ¥ãå®äŸ¡ã«ãªããŸãããããããåãæäœã§ãªã¹ãã®æåŸã«æ°ããé ç¹ãè¿œå ã§ããããããã®ãããªæäœã¯å¿ èŠãªãã§ãããã çµã¿èŸŒã¿ãªã¹ãã䜿çšããå©ç¹ã¯ã ãªã¹ããéåžžã«é«éã§ãããããã°ãããŠããããšã§ãïŒçŽç²ãªPythonã§å®è£ ã§ãããªã¹ãæ§é ãšã¯ç°ãªããŸãïŒã
ã°ã©ãã§äœæ¥ãããšããæè¯ã®ã¢ã€ãã¢ã¯ã°ã©ãã§äœãããå¿ èŠããããã«æ£ç¢ºã«äŸåãããšããã¢ã€ãã¢ãåžžã«çŸããŸãã ããšãã°ãé£æ¥ãªã¹ãïŒãŸãã¯é åïŒã䜿çšãããšããªãŒããŒããããå°ããä¿ã¡ãä»»æã®é ç¹vã«å¯ŸããŠNïŒvïŒã®å¹ççãªãã©ããŒãµã«ãæäŸã§ããŸãã ãã ãã uãšvãé£æ¥ããŠãããã©ããã確èªããã«ã¯ã ÎïŒNïŒvïŒïŒã®æéãããããŸããããã¯ãé«ãã°ã©ãå¯åºŠïŒã€ãŸããå€æ°ã®ãšããžïŒã§åé¡ã«ãªãããšããããŸãã ãããã®å Žåãå€ãã®é£æ¥é¢ä¿ãå©ãã«ãªããŸãã
ãã³ãïŒ
Pythonã®ãªã¹ãã®äžå€®ãããªããžã§ã¯ããåé€ããããšã¯ãéåžžã«ã³ã¹ãããããããšãç¥ãããŠããŸãã çµããããã®åé€ã¯äžå®ã®æéã§çºçããŸãã é ç¹ã®é åºãæ°ã«ããªãå Žåã¯ãé£æ¥ãªã¹ãã®æ«å°Ÿã«ããé ç¹ã§äžæžãããŠããpopã¡ãœãããåŒã³åºãããšã«ãããäžå®ã®æéã§ã©ã³ãã ãªé ç¹ãåé€ã§ããŸãã
ãã®ãã¥ãŒã®ãããã¯ã®å°ããªããªãšãŒã·ã§ã³ã¯ãé£æ¥ããé ç¹ã®ãœãŒãããããªã¹ãã§ãã ãªã¹ããé »ç¹ã«å€æŽãããªãå Žåã¯ããœãŒããç¶æãããã€ã»ã¯ã·ã§ã³ã䜿çšããŠé ç¹ã®é£æ¥é¢ä¿ã確èªã§ããŸããããã«ããããªãŒããŒãããããããã«æžå°ããŸãïŒã¡ã¢ãªäœ¿çšéãšå埩æéïŒããã ãããã§ãã¯ã®è€éãã¯ÎïŒlog 2 kïŒã«å¢å ããŸããã®é ç¹ã«é£æ¥ããŸãã ïŒããã¯ãŸã éåžžã«å°ããªå€ã§ãããã ããå®éã«ã¯ãçµã¿èŸŒã¿ã®ã»ããåã䜿çšããããšã¯ããã»ã©é¢åã§ã¯ãããŸããïŒã
å¥ã®ãã€ããŒãªèª¿æŽã¯ãã»ãããŸãã¯ãªã¹ãã®ä»£ããã«èŸæžã䜿çšããããšã§ãã é£æ¥ããé ç¹ã¯èŸæžããŒã«ããããšãã§ããè¿œå ã®ããŒã¿ïŒãšããžã®éã¿ãªã©ïŒãå€ãšããŠäœ¿çšã§ããŸãã ãããã©ã®ããã«èŠãããã¯ã以äžã®ãªã¹ãã§ç¢ºèªã§ããŸãïŒéã¿ã¯ã©ã³ãã ã«éžæãããŸãïŒã
a, b, c, d, e, f, g, h = range(8) N = [ {b:2, c:1, d:3, e:9, f:4}, # a {c:4, e:3}, # b {d:8}, # c {e:7}, # d {f:5}, # e {c:2, g:2, h:2}, # f {f:1, h:6}, # g {f:9, g:8} # h ]
é£æ¥èŸæžã¯ãéã¿ã«é¢ããè¿œå æ å ±ãèæ ®ããŠãä»ã®è¡šçŸãšåãæ¹æ³ã§äœ¿çšã§ããŸãã
>>> b in N[a] # True >>> len(N[f]) # 3 >>> N[a][b] # (a, b) 2
å¿ èŠã«å¿ããŠããšããžã®éã¿ïŒããŒã¿ã®ä»£ããã«NoneãŸãã¯å¥ã®å€ã䜿çšïŒãªã©ã®æçšãªããŒã¿ããªãå Žåã§ããé£æ¥èŸæžã䜿çšã§ããŸãã ããã«ãããé£æ¥ã»ããã®ãã¹ãŠã®å©ç¹ãåŸãããŸããã ã»ããããµããŒãããŠããªãïŒéåžžã«ïŒå€ãããŒãžã§ã³ã®PythonïŒã»ããã¯Python 2.3ã§ã»ããã¢ãžã¥ãŒã«ãšããŠå°å ¥ãããŸãããçµã¿èŸŒã¿ã»ããã¿ã€ãã¯Python 2.4以éã§äœ¿çšå¯èœïŒã§åäœããŸãã
ãããŸã§ãé£æ¥æ§é ïŒãªã¹ããã»ããããŸãã¯èŸæžïŒãæ ŒçŽãããšã³ãã£ãã£ã¯ãé ç¹çªå·ã§ã€ã³ããã¯ã¹ä»ãããããªã¹ãã§ããã ããæè»ãªãªãã·ã§ã³ïŒä»»æã®ããã·ã¥ãããé ç¹åã®äœ¿çšãèš±å¯ïŒã¯ãèŸæžã«åºã¥ããŠæ§ç¯ãããŸãïŒé£æ¥ãªã¹ããæã€èŸæžã¯ã www.python.orgã§å ¥æå¯èœãªèšäºãPython Patterns-Implementing Graphsãã§Guido van Rossumã«ãã£ãŠäœ¿çšãããŸããïŒ /doc/essays/graphs.html ïŒã 以äžã®ãªã¹ãã¯ãé£æ¥ã»ãããå«ãèŸæžã®äŸã瀺ããŠããŸãã ãã®äžã®é ç¹ã¯èšå·ã§ç€ºãããŠããããšã«æ³šæããŠãã ããã
N = { 'a': set('bcdef'), 'b': set('ce'), 'c': set('d'), 'd': set('e'), 'e': set('f'), 'f': set('cgh'), 'g': set('fh'), 'h': set('fg') }
ã»ããã³ã³ã¹ãã©ã¯ã¿ãŒãäžèšã®ãªã¹ãããåé€ããããšãæåã®é£æ¥ãªã¹ãïŒäžå€ïŒãšããŠæ©èœããé£æ¥ã¹ããªã³ã°ãæ®ããŸãïŒãããã«ãªãŒããŒããããå°ãªããªããŸãïŒã ããã¯æè¯ã®ã¢ã€ãã¢ãšã¯ã»ã©é ãããã«æããŸãããåè¿°ã®ããã«ããã¹ãŠã¯ããã°ã©ã ã«äŸåããŸãã ã°ã©ãã®ããŒã¿ã¯ã©ãããååŸããŸããïŒ ïŒãã¶ãããããã¯ãã§ã«ããã¹ãã®åœ¢ã«ãªã£ãŠããŸããïŒïŒã©ã®ããã«äœ¿çšããŸããïŒ
é£æ¥è¡å
ã°ã©ãè¡šçŸã®å¥ã®äžè¬çãªåœ¢åŒã¯ãé£æ¥è¡åã§ãã äž»ãªéãã¯æ¬¡ã®ãšããã§ããåé ç¹ã®ãã¹ãŠã®é£æ¥ããé ç¹ããªã¹ããã代ããã«ãå€ã®1è¡ïŒé åïŒãæžãçããŸããåè¡ã¯ãé£æ¥ããå¯èœæ§ã®ããé ç¹ã«å¯Ÿå¿ãïŒã°ã©ãã®åé ç¹ã«ãã®ãããªé ç¹ãå°ãªããšã1ã€ãããŸãïŒãå€ãä¿åããŸãïŒ True圢åŒãŸãã¯False ïŒé ç¹ãå®éã«é£æ¥ããŠãããã©ããã瀺ããŸãã ç¹°ãè¿ããŸãããæãåçŽãªå®è£ ã¯ã以äžã®ãªã¹ããããããããã«ããã¹ãããããªã¹ãã䜿çšããŠååŸã§ããŸãã ããã«ã¯ãé ç¹ã«0ããV-1ãŸã§ã®çªå·ãä»ããå¿ èŠãããããšã«æ³šæããŠãã ããã ççå€ã¯ããããªãã¯ã¹ãèªã¿åãå¯èœã«ããããã«1ãš0ïŒ Trueããã³Falseã®ä»£ããã«ïŒã§ãã
a, b, c, d, e, f, g, h = range(8) # abcdefgh N = [[0,1,1,1,1,1,0,0], # a [0,0,1,0,1,0,0,0], # b [0,0,0,1,0,0,0,0], # c [0,0,0,0,1,0,0,0], # d [0,0,0,0,0,1,0,0], # e [0,0,1,0,0,0,1,1], # f [0,0,0,0,0,1,0,1], # g [0,0,0,0,0,1,1,0]] # h
é£æ¥è¡åã®äœ¿çšæ¹æ³ã¯ããªã¹ãããã³é£æ¥ã»ãããšã¯å°ãç°ãªããŸãã bã N [a]ã«ãããã©ããããã§ãã¯ãã代ããã«ããããªãã¯ã¹N [a] [b]ã®ã»ã«å€ãçã§ãããã©ããããã§ãã¯ããŸãã ããã«ããã¹ãŠã®è¡ãåãé·ãã§ãããããé£æ¥ããé ç¹ã®æ°ãååŸããããã«lenïŒN [a]ïŒã䜿çšã§ããªããªããŸããã 代ããã«ã sumé¢æ°ã䜿çšã§ããŸãã
>>> N[a][b] 1 >>> sum(N[f]) 3
é£æ¥è¡åã«ã¯ãç¥ã£ãŠããã¹ãããã€ãã®äŸ¿å©ãªããããã£ããããŸãã ãŸããã«ãŒãã®ããã°ã©ããèæ ®ããªãããïŒã€ãŸããç䌌ã°ã©ãã䜿çšããªãïŒã察è§ç·äžã®ãã¹ãŠã®å€ã¯falseã§ãã ãŸããç¡åã°ã©ãã¯éåžžãäž¡æ¹åã®ãšããžã®ãã¢ã§èšè¿°ãããŸãã ããã¯ãç¡åã°ã©ãã®é£æ¥è¡åã察称ã«ãªãããšãæå³ããŸãã
é£æ¥è¡åãæ¡åŒµããŠéã¿ã䜿çšããã®ã¯ç°¡åã§ããè«çå€ãä¿åãã代ããã«ãéã¿ãä¿åããŸãã ãšããžïŒuãvïŒã®å Žåã N [u] [v]ã¯Trueã§ã¯ãªããšããžwïŒuãvïŒã®éã¿ã«ãªããŸã ã å€ãã®å Žåãå®éçãªç®çã®ããã«ãååšããªããªãã«ã¯ç¡éã®éã¿ãå²ãåœãŠãããŸãã ïŒããã«ãããæ¢åã®ãšããžã«æ²¿ã£ããã¹ãæ¢ããŠãããããããšãã°æçãã¹ã«å«ãŸããªãããã«ãªããŸãïŒã ç¡éãæ³åããæ¹æ³ã¯å¿ ãããæããã§ã¯ãããŸãããã確ãã«ããã€ãã®ç°ãªããªãã·ã§ã³ããããŸãã
ãããã®1ã€ã¯ããã¹ãŠã®éã¿ãéè² ã§ããããšãããã£ãŠããå Žåã«ã Noneã-1ãªã©ãéã¿ã«äžé©åãªå€ã䜿çšããããšã§ãã å Žåã«ãã£ãŠã¯ãæ¬åœã«å€§ããªæ°å€ã䜿çšãããšäŸ¿å©ã§ãã æŽæ°ã®éã¿ã®å Žåã sys.maxintã䜿çšã§ããŸããããã®å€ã¯å¿ ãããæ倧ã§ã¯ãããŸããïŒé·ãæŽæ°ã®æ¹ã倧ããå ŽåããããŸãïŒã ç¡éãåæ ããããã«å°å ¥ãããå€ïŒ infããããŸãã Pythonã§ã¯ååã§çŽæ¥å©çšã§ããã floatïŒ 'inf'ïŒãšããŠè¡šãããŸãïŒ Python 2.6以éã§åäœããããšãä¿èšŒãããŠããŸãã以åã®ããŒãžã§ã³ã§ã¯ããã®ãããªç¹å¥ãªå€ã¯ãã©ãããã©ãŒã åºæã§ãããã floatïŒ 'inf'ïŒãŸãã¯floatïŒ ' Inf 'ïŒã¯ã»ãšãã©ã®ãã©ãããã©ãŒã ã§åäœããã¯ãã§ãïŒã
以äžã®ãªã¹ãã¯ããã¹ãããããªã¹ãã«ãã£ãŠå®è£ ãããéã¿ãããªãã¯ã¹ãã©ã®ããã«èŠãããã瀺ããŠããŸãã äžèšã®ãªã¹ããšåãéã¿ã䜿çšãããŸãã
a, b, c, d, e, f, g, h = range(8) _ = float('inf') # abcdefgh W = [[0,2,1,3,9,4,_,_], # a [_,0,4,_,3,_,_,_], # b [_,_,0,8,_,_,_,_], # c [_,_,_,0,7,_,_,_], # d [_,_,_,_,0,5,_,_], # e [_,_,2,_,_,0,2,2], # f [_,_,_,_,_,1,0,6], # g [_,_,_,_,_,9,8,0]] # h
ç¡éã®å€ã¯çããèŠèŠçã«åºå¥ã§ãããããã¢ã³ããŒã¹ã³ã¢ïŒ _ ïŒãšããŠç€ºãããŸãã åœç¶ãä»»æã®ååã䜿çšã§ããŸãã 察è§ç·ã®å€ã¯ãŸã ãŒãã§ããããšã«æ³šæããŠãã ãããã«ãŒãããªããŠããéã¿ã¯ãã°ãã°è·é¢ãšããŠè§£éãããé ç¹ããããèªäœãŸã§ã®è·é¢ã¯ãŒãã§ãã
ãã¡ããããŠã§ã€ããããªãã¯ã¹ã䜿çšãããšããšããžã®ãŠã§ã€ããéåžžã«ç°¡åã«ååŸã§ããŸãããããšãã°ãé ç¹ã®é£æ¥åºŠã®ãã¹ããšæ±ºå®ããŸãã¯é£æ¥ãããã¹ãŠã®é ç¹ã®ç§»åã¯ç°ãªãæ¹æ³ã§è¡ãããŸãã ããã§ã¯ããã®ãããªç¡éã®å€ã䜿çšããå¿ èŠããããŸãïŒæ確ã«ããããã«ã inf = floatïŒ 'inf'ïŒãå®çŸ©ããŸãïŒïŒ
>>> W[a][b] < inf # True >>> W[c][e] < inf # False >>> sum(1 for w in W[a] if w < inf) - 1 # 5
察è§ç·äžã®å€ã¯ã«ãŠã³ãããªããããååŸãã次æ°ãã1ãæžç®ãããããšã«æ³šæããŠãã ããã ããã§æ¬¡æ°ãèšç®ããè€éãã¯ÎïŒnïŒã§ãããå¥ã®è¡šçŸã§ã¯ãé ç¹ã®é£æ¥ãšæ¬¡æ°ã®äž¡æ¹ãäžå®ã®æéã§æ±ºå®ã§ããŸãã ãããã£ãŠãã°ã©ãã®äœ¿çšæ¹æ³ãåžžã«æ£ç¢ºã«ç解ã ãé©åãªè¡šçŸãéžæããå¿ èŠããããŸãã
NumPyããã®ç¹å¥ãªé å
NumPy , . , NumPy , , . n , : >>> N = [[0]*10 for i in range(10)] NumPy zeros: >>> import numpy as np >>> N = np.zeros([10,10]) , : A[u,v]. , : A[u]. NumPy http://numpy.scipy.org. , NumPy, Python. NumPy Python, , . (, Subversion): svn co http://svn.scipy.org/svn/numpy/trunk numpy , NumPy, , .
ããªãŒã®å®è£
åœç¶ãããªãŒã¯ç¹å¥ãªçš®é¡ã®ã°ã©ãã§ãããããã°ã©ãã®ä»»æã®è¡šçŸã䜿çšããŠããªãŒãè¡šãããšãã§ããŸãã ãã ããããªãŒã¯ã¢ã«ãŽãªãºã ã§å€§ããªåœ¹å²ãæãããŠãããå€ãã®é©åãªæ§é ãšæ¹æ³ãéçºãããŠããŸãã ã»ãšãã©ã®ããªãŒããŒã¹ã®ã¢ã«ãŽãªãºã ïŒããªãŒæ€çŽ¢ãªã©ïŒã¯ã°ã©ãçè«ã®èŠ³ç¹ããèããããšãã§ããŸãããç¹å¥ãªããŒã¿æ§é ã«ããå®è£ ã容æã«ãªããŸãã
ã«ãŒãããããªãŒã®è¡šçŸãèšè¿°ããæãç°¡åãªæ¹æ³ã¯ãrib骚ãã«ãŒãããäžããããšã§ãã ãã®ãããªããªãŒã¯ãå€ãã®å Žåãéå±€ããŒã¿ã®åå²ã衚瀺ããŸããã«ãŒãã«ã¯ãã¹ãŠã®ãªããžã§ã¯ãïŒãªãŒãã«ä¿åãããïŒã衚瀺ãããåå éšããŒãã«ã¯ããã®ããŒããã«ãŒããšããããªãŒã«å«ãŸãããªããžã§ã¯ãã衚瀺ãããŸãã ãã®èª¬æã¯ããã¹ãŠã®åå«ãµãããªãŒãå«ããªã¹ããšããŠåãµãããªãŒãæ瀺ããããšã§äœ¿çšã§ããŸãã å³ã«ç€ºãããŠããåçŽãªããªãŒãæ€èšããŠãã ããã 2ã
ãã®ããªãŒããªã¹ãã®ãªã¹ããšããŠè¡šãããšãã§ããŸãã
>>> T = [["a", "b"], ["c"], ["d", ["e", "f"]]] >>> T[0][1] 'b' >>> T[2][1][0] 'e'
åãªã¹ãã¯åºæ¬çã«ãåå éšããŒãã®åå«ã®ãªã¹ãã§ãã 2çªç®ã®äŸã§ã¯ãã«ãŒãã®3çªç®ã®åå«ã次ã«ãã®2çªç®ã®åå«ãæåŸã«åã®ããŒãã®æåã®åå«ã«åãæ¿ããŸãïŒãã®ãã¹ã¯å³ã§ããŒã¯ãããŠããŸãïŒã
å³ 2.ã«ãŒããããªãŒããžã®ããŒã¯ããããã¹ãæã€ããªãŒã®äŸ
å Žåã«ãã£ãŠã¯ãåããŒãã®åå«ã®æ倧æ°ãäºåã«æ±ºå®ããããšãã§ããŸãã ïŒããšãã°ã ãã€ããªããªãŒã®åããŒãã«ã¯æ倧2ã€ã®åãå«ããããšãã§ããŸãïŒã ãããã£ãŠãä»ã®ãã¥ãŒãããšãã°ãäžã®ãªã¹ãã®ããã«ãåå«ããšã«åå¥ã®å±æ§ãæã€ãªããžã§ã¯ãã䜿çšã§ããŸãã
class Tree: def __init__(self, left, right): self.left = left self.right = right >>> t = Tree(Tree("a", "b"), Tree("c", "d")) >>> t.right.left 'c'
Noneã䜿çšããŠãåå«ãæ¬ èœããŠããããšã瀺ãããšãã§ããŸãïŒããŒãã«åå«ã1ã€ãããªãå ŽåïŒã ãã¡ãããããŸããŸãªæ¹æ³ãçµã¿åãããããšãã§ããŸãïŒããšãã°ãããŒãããšã«ãªã¹ããŸãã¯åå«ã®ã»ããã䜿çšããŸãïŒã
ç¹ã«çµã¿èŸŒã¿ãªã¹ãã®ãµããŒããæããªãèšèªã§ããªãŒãå®è£ ããäžè¬çãªæ¹æ³ã¯ããããããæåã®åå«ã次ã®å åŒãã®è¡šçŸã§ãã ãã®äžã®åããŒãã«ã¯ããã€ããªããªãŒã®ããã«ãä»ã®ããŒããæã2ã€ã®ããã€ã³ã¿ãŒããŸãã¯å±æ§ããããŸãã ãã ãããããã®å±æ§ã®æåã¯ããŒãã®æåã®åå«ãæãã2çªç®ã¯ãã®æ¬¡ã®å åŒïŒã€ãŸããåã芪ãæã€ãå³åŽã«ããããŒã- ãããRe ïŒãæããŸãã èšãæãããšãããªãŒå ã®åããŒãã«ã¯ããã®åå«ã®ãªã³ã¯ãªã¹ããžã®ãã€ã³ã¿ãããããããã®åå«ã¯ããããç¬èªã®é¡äŒŒãªã¹ããåç §ããŸãã ãããã£ãŠããã€ããªããªãŒãå°ãå€æŽãããšã以äžã®ãªã¹ãã«ç€ºããã«ããã¹ããªãŒãåŸãããŸãã
class Tree: def __init__(self, kids, next=None): self.kids = self.val = kids self.next = next
ããã§ã¯ãããŒããžã®ãªã³ã¯ã®ä»£ããã«å€ïŒããšãã°ããcãïŒãæå®ãããšãã«æ確ãªåºåãååŸããããã«ãå¥åã®valå±æ§ãå°å ¥ãããŠããŸãã åœç¶ãããã¯ãã¹ãŠå€æŽã§ããŸãã ãã®æ§é ãåŠçããæ¹æ³ã®äŸã次ã«ç€ºããŸãã
>>> t = Tree(Tree("a", Tree("b", Tree("c", Tree("d"))))) >>> t.kids.next.next.val 'c'
ãããŠããã®ããªãŒã¯æ¬¡ã®ããã«ãªããŸãã
åèŠçŽ ãšæ¬¡ã®å±æ§ã¯ç Žç·ã§ç€ºãããŠãããããªãŒã®ç«¯ã¯å®ç·ã§ãã ç§ã¯å°ãã ãŸããŠãè¡ãaãããbããªã©ã«å¥ã ã®ããŒãã衚瀺ããŸããã§ããã 代ããã«ãããããããããã®èŠªããŒãã®ã©ãã«ãšããŠæ±ããŸãã ããè€éãªããªãŒã§ã¯ãåäžã®å±æ§ã䜿çšããŠããŒãå€ãæ ŒçŽããåå«ã®ãªã¹ããåç §ãã代ããã«ãäž¡æ¹ã®ç®çã§å¥åã®å±æ§ãå¿ èŠã«ãªãå ŽåããããŸãã éåžžãããŒãã³ãŒãããããã¹ã§ããã§æå®ããããã®ãããè€éãªã³ãŒãïŒã«ãŒããšååž°ãå«ãïŒã䜿çšããŠããªãŒãèµ°æ»ããŸãã
ãã¶ã€ã³ã»ãããã³ãã¬ãŒã
( ) , . «» ( «Python Cookbook»). , : class Bunch(dict): def __init__(self, *args, **kwds): super(Bunch, self).__init__(*args, **kwds) self.__dict__ = self . -, , : >>> x = Bunch(name="Jayne Cobb", position="PR") >>> x.name 'Jayne Cobb' -, dict , () . : >>> T = Bunch >>> t = T(left=T(left="a", right="b"), right=T(left="c")) >>> t.left {'right': 'b', 'left': 'a'} >>> t.left.right 'b' >>> t['left']['right'] 'b' >>> "left" in t.right True >>> "right" in t.right False . , , .
å€ãã®ç°ãªããã¥ãŒ
ã°ã©ãã«ã¯å€ãã®è¡šçŸããããšããäºå®ã«ããããããããããã®ã»ãšãã©ã¯ããã®ç« ã§ãã§ã«èª¬æãã2ã€ã®ã¿ã€ãïŒå€æŽããïŒã®ã¿ãç 究ããŠäœ¿çšããŸãã Jeremy Spinredã¯èæžãEffective Graph Representationãã§ãã°ã©ãã®ã³ã³ãã¥ãŒã¿ãŒè¡šçŸã®ç 究è ãšããŠãã»ãšãã©ã®å ¥éèšäºã§ãç¹ã«ã€ã©ã€ã©ããããšæžããŠããŸãã ããç¥ãããŠããè¡šçŸïŒãªã¹ããšé£æ¥è¡åïŒã®èª¬æã¯éåžžé©åã§ãããããäžè¬çãªèª¬æã¯ãã°ãã°èª€ãã§ãã , :
« : . , , , , , » (. 9).
, . -, , . , ( ), ( -), , , ( ). , ( ) ( ). , , - .
-, : , , , , .
, , , , . , , , . , (u, v) Î(1) , v â Î(n) , Î(d(v)) , .. .
ã¢ã«ãŽãªãºã ã®æŒžè¿çãªè€éããè¡šçŸã®ã¿ã€ãã«äŸåããªãå Žåããã®ç« ã§åè¿°ããçµéšçãªãã¹ããå®è¡ã§ããŸãããŸãã¯ãããããããšã§ãããã³ãŒãã§æé©ã«æ©èœããä¿å®ãããããã®ãéžæã§ããŸãã
ãŸã 觊ããããŠããªãã°ã©ãã䜿çšããéèŠãªæ¹æ³ã¯ããã¬ãŒã³ããŒã·ã§ã³ã«é¢ä¿ãããŸãããå®éãå€ãã®åé¡ã§ããŒã¿ã¯ãã§ã«ã°ã©ããŸãã¯ããªãŒã®æ§é ãæã£ãŠãããããç¹å¥ãªè¡šçŸãäœæããããšãªããã°ã©ããšããªãŒã«å¯Ÿå¿ããã¢ã«ãŽãªãºã ã䜿çšã§ããŸããå Žåã«ãã£ãŠã¯ãã°ã©ãè¡šçŸãããã°ã©ã ã®å€éšã§ã³ã³ãã€ã«ãããŠããå Žåã«çºçããŸããããšãã°ãXMLããã¥ã¡ã³ãã解æãããããã¡ã€ã«ã·ã¹ãã ãã£ã¬ã¯ããªãèµ°æ»ããããããšããããªãŒæ§é ã¯ãã§ã«äœæãããŠãããç¹å®ã®APIããããŸããã€ãŸããã°ã©ããæé»çã«èšå®ããŸããããšãã°ãã«ãŒããã¯ãã¥ãŒãã®ç¹å®ã®æ§æã«å¯ŸããŠæãå¹æçãªãœãªã¥ãŒã·ã§ã³ãèŠã€ããããã«ããã¥ãŒãã®ç¶æ ãšå€æŽæ¹æ³ã決å®ã§ããŸãããã ããåæ§æãæ瀺çã«èª¬æããŠä¿åããªããŠãã ( ) â . , A* , . N(v) « », , -.
, , . , , . , : , . , (.. ) . ( ), « » .
, , , . , , , . , C, . , . , :
NetworkX: http://networkx.lanl.gov
python-graph: http://code.google.com/p/python-graph
Graphine: http://gitorious.org/projects/graphine/pages/Home
, Pygr, ( http://bioinfo.mbi.ucla.edu/pygr ), Gato, ( http://gato.sourceforge.net ) PADS, ( http://www.ics.uci.edu/~eppstein/PADS ).