HOME > é»ç® > Python ãããããã
Python ãããããã
Python ã®åºæ¬çãªéšåã«ã€ããŠèŠ³å¯ãããšããé¢çœãããªããšæã£ãŠã¡ã¢ãããã®ãªãã
å šè¬çã« http://www.python.jp/doc/release/tut/tut.html ãåèã«ããããè±èªç http://docs.python.org/tutorial/index.html ã®ã»ããæ°ããã®ã§ããã¡ããèŠãªããŠã¯ãªããªãå Žé¢ããã£ããããããŠãè±èªã®ããã¥ã¡ã³ãã®ã»ããå å®ããŠããã¯ã©ãããŠã ãããããã®ã»ãåå¥ã«åèã«ãããã®ã¯ããããã®ç®æã«èšããŠãããã®ã ã
ç§ã®ç¡ç解ããããããã¯ãåçŽãã¿ã€ããã¹ãããééããæžããŠããå¯èœæ§ã¯äœããªãããçºå¿µã
ååãš =
以äžãå€æ°ããšããèšèã䜿ããªããå€æ°ãšããèšèããååããããã®ããšãããã®ãããååãæã瀺ããªããžã§ã¯ããã®ããšãããã®ãæ··ä¹±ããªãããã«ããã€ãããïŒæããšããã®ã¯ be assigned to ã®ããšïŒã
Python ã§ã¯ã= ã®å·ŠèŸºã« x ã眮ããŠ
x=1
ãšããããšã«ãããx ãªãååãã1ããæã瀺ãããã«ãªãã
äžæ¹ã= ã®å·ŠèŸºã«çœ®ãããªãå Žå x ã¯ãx ãæã瀺ããšããã®ãã®ãããããããããšãã°ã
x=1 print(x)
ãšãããšããx ãæã瀺ããšããã®ãã®ããã€ãŸã 1 ãåºåãããã
ãããã£ãŠãx ãš y ãšããååããã£ããšãã«ã
y=x
ãšããããšã«ãããy ããx ãæã瀺ããšããã®ãã®ããæã瀺ãããã«ãªãã
x=1 y=x print(y) ==> 1
ã®åŠãã
ãšããã§ã1+1 ã®ããšã㯠Python ã§ã¯åŒã§ãããã ãããeval('1+1')㯠2 ãè¿ãããšããããx=1 ã®ããšãã¯ãåŒã§ã¯ãªããŠæã§ãããã ãããeval('x=1') ã¯ãšã©ãŒã«ãªããããã¯ãexec('x=1') ãšããªããã°ãªããªããïŒPython ã®ååãšãããã®ããLISP ã®ã·ã³ãã«ãšéããäžäººåã®ãªããžã§ã¯ãã§ã¯ãªãããšãšé¢ä¿ãããã®ããããããªãããã人ã¯èšããã·ã³ãã«åã¯æªãèãã§ã¯ãªãããã€ã³ã¿ãŒã³ãããæååãããã°ã»ãšãã©å©ç¹ã¯ãªãããhttp://practical-scheme.net/trans/IsPythonLisp-j.htmlïŒ
æååãšæ°å€
ååã¯ãã©ããªãªããžã§ã¯ãã§ãæã瀺ãããšãã§ããããããã£ãŠãååã«åã¯ãªãããããããªããžã§ã¯ãã®ã»ãã«ã¯ããšãããåãšãããã®ããããæååãšããæŽæ°ãšãâŠâŠ
æåå
Python ã®æåŒãèªããšããŸããPython ã®æååã¯å€æŽã§ãã¬ããšè ãããããã¹ã€ã«ã
>>> a="Hello" >>> a="Bye" >>> print a Bye
ãšããããšãã§ããªãããã§ã¯ãªããããã¯ãæååãã®ãã®ãå€æŽããŠããã®ã§ã¯ãªããŠãa ãšããååãå¥ã®æååãæã瀺ãããã«å€æŽããŠããã®ã ãååã®æååãžã®å²ãåœãŠããããšãäœãèµ·ããã®ãæ³åããŠã¿ãã
æååã®çµå
>>> print 'a' 'bc' 'd' # ããã¯ããã abcd >>> s='bc' >>> print 'a' s 'd' # ããã¯ãšã©ãŒ >>> print 'a' + s + 'd' # ãããªãã°è¯ã abcd
æååããã 䞊ã¹ãå Žåãçµåã¯ã¹ã¯ãªããã®å®è¡ä»¥åã«è¡ãããã®ã§ãååã®æãæååãããã«å±éããããšãã§ããªããããã«å¯ŸããŠã+ ã䜿ã£ãçµåã¯ãå®è¡æã«è¡ãããã®ã§ãååãå±éããããšãã§ããã
æååã®åäžæ§
is ã¯ãªããžã§ã¯ããåäžããããšã瀺ãã== ã¯å€ãåäžããããšã瀺ãã
å¥ã ã«ã€ãã£ãæååã¯ãåãåé¢ã§ãå¥ãªããžã§ã¯ããã ãšäºæ³ããŠã¿ãã
>>> a="dog and cat" >>> b="dog and cat" >>> a==b True >>> a is b False
ãã®éãã«èŠããâŠâŠ
>>> c="dog and cat" >>> d=c >>> c is d True
âŠâŠãã¡ããããããããã ããã
äžã¯ãããå¥åŠã«æããããäŸã
>>> a="dog" >>> b="dog" >>> a is b True
a ãšããååã 'dog' ã«å²ãåœãŠããšãã«ãPython ã¯ãããååãªã¹ãã«ç»é²ïŒinternïŒãããååã®ããã«äœ¿ãããæååã§ãªããŠããååã«äœ¿ãããããªæååã¯ãããããã€ã³ã¿ãŒã³ããŠããããã®åŸ b ã« 'dog' ãå²ãåœãŠããšãã«ã¯ãb ã a ãæãã®ãšåäžã®ãªããžã§ã¯ããæããããªåœ¢ã§åå©çšããã
æååã¯ãå€æŽã§ããªããããããããæ··ä¹±ãåŒãèµ·ããããšã¯ãªããäœè«ãªããâŠâŠ
>>> a=[1,2] >>> b=[1,2] >>> a is b False
ãªã¹ãïŒåŸè¿°ïŒã¯ãå€æŽã§ããããããå¥éäœæããããªã¹ãã¯åžžã«å¥ãªããžã§ã¯ãããããããªãã倧混乱ãèµ·ããã
æ°å€
æŽæ°ãšå°æ°ã§æ±ããéãããšã¯ãèŠãŠã®éãã
>>> 1 / 3 0 >>> 1.0 / 3 0.33333333333333331
åãå€ã®æŽæ°ã¯ãäžçã«ã²ãšã€ããååšããªãã
>>> a=1 >>> b=1 >>> a==b True >>> a is b True
ããå°æ°ã¯ãã®éãã«éã
>>> a=1.0 >>> b=1.0 >>> a==b True >>> a is b False
ã ããäœãªãã ã£ãŠèšããããã ããäžå¿è©Šãã次第ã
â»None, False, '', 0, 0.0 ã¯ããããã«å¥ã®ãªããžã§ã¯ãã§ããããã¹ãŠç°ãªãåã«å±ããããè«çæŒç®ã§ã¯ã¿ãªãåœãã
ãªã¹ã
ãªã¹ã㯠ãé çªããã£ãŠäžŠãã ãã®ã§ãããå ã¯ããããã«äœããã®ãªããžã§ã¯ããæã瀺ãââãšèããããšã«ãããããã§äºãå ãæã瀺ããªããžã§ã¯ãã¯ããªã¹ãã®èŠçŽ ããšåŒã°ããã
ãåå=ãªã¹ãããšããã°ãååããªã¹ããæãããã«ãªããããã¯ååã = ã®å·ŠèŸºã«çšããå Žåã®ååéãã§ãåãç«ãŠãŠãªã¹ãã«å¯ŸããŠã ãã«èšããããšã§ã¯ãªãã
ååã = ã®å·ŠèŸºä»¥å€ã®å Žæã«äœ¿ãããå Žåããã®ååã¯ããã®ååãæããªããžã§ã¯ãããè¡šçŸãããšåã«è¿°ã¹ãããã¡ããããã¯ããã®ååããªã¹ããæããŠããå Žåã«ãåœãŠã¯ãŸãã
ãªã¹ããšæ·»å
ãªã¹ã n ã«å¯ŸããŠãæ·»å n (N=0,1,2,...)ãçšã㊠a[n] ãšããè¡šçŸãããããšãã§ãããa[n] ã¯ã= ã®å·ŠèŸºã«ãªãéãã¯ãããªã¹ãã® n+1 çªç®ã® ãæããšããã®ãªããžã§ã¯ãããå¥ã®èšãæ¹ãããã° n+1 çªç®ã®èŠçŽ ãããããã
>>> a=['hello', 'bye'] >>> a[0] 'hello'
ãããã£ãŠã
äžæ¹ã= ã®å·ŠèŸºã« a[n] ã眮ããå Žåãããªã¹ãã® n+1 çªç®ã® ãã = ã®å³èŸºã«ãããªããžã§ã¯ããæãããã«ãªãããããçšããŠãªã¹ãã®èŠçŽ ãå€æŽããïŒåãæ¿ããïŒããšãã§ããã
次ã¯å ¥ãåãªã¹ããäœã£ãŠããæ§åã
ãªã¹ãã®äŒžåŒµ
ãªã¹ãã¯äŒžåŒµããããšãã§ããã䌞匵ããŠããåŸåã®ãªããžã§ã¯ããšåäžãªããžã§ã¯ãã§ããã
>>> a=[1,2] >>> b=a >>> a.append(3) >>> a [1,2,3] >>> b [1,2,3]
ã¹ã©ã€ã¹ã䜿ããšãããã«è€éãªããšãå¯èœãšãªãã
ãªãããªã¹ãã®çµå㯠+ ãªãã¬ãŒã¿ã«ãã£ãŠãæ°ããªãªã¹ãã»ãªããžã§ã¯ããäœæããããšã«ãã£ãŠãè¡ããã
â»ãã°ãã°ãPython ã®ãªã¹ãã¯å€æ°ãšéãå€æŽã§ãããšèšãããããããã¯ãã®ãããªæ§è³ªãèšã£ãŠããã®ã§ãããã
â»Python ã®ãªã¹ãã®èª¬æãšããŠã¯ãLISP ã® cdr ã次ã®ã³ã³ã¹ã»ã«ãæããŠãããããªãã®ãã€ããå³ãæãããããªãããã®ã»ããäžå±€ããªã¹ãã䌞匵ã§ããããšãããè¡šãã«éããªããã ããå®è£ ã¯ãããªã£ãŠããªããšããããã®èšŒæ ã«ãLISP ã®ãªã¹ããšç°ãªããåŸãã®ã»ãã®èŠçŽ ã«ã¢ã¯ã»ã¹ããããã«ããé·ãæéãå¿ èŠã«ãªãããšããããšã¯ãªãããã ããããŠãPython ã®ãªã¹ãã®éäžã«èŠçŽ ãæ¿å ¥ãããããªå Žåã¯ããªã¹ãã®åŸãã®ã»ãã«æ¿å ¥ããã»ããåã®ã»ãã«æ¿å ¥ããããæéãçããŠæžãããã§ããããã®ãããããããããªãå®è£ ããªãããŠãããæ³åããããšã¯ã§ããããå®éã«ã³ãŒãã確ãããããã§ã¯ãªãã
åäžãªããžã§ã¯ããèŠçŽ ãšããŠæã€ãå¥ã®ãªã¹ãã®äœæ
åã é ã®å¿çšã§ããªã¹ããè€è£œã§ãããããã¯ãæµ ãè€è£œãšåŒã°ããã
ãã®å Žåãã¹ã©ã€ã¹ïŒåŸè¿°ïŒã䜿ã£ãŠããã°ããã䟿å©ã§ããã
å¥ã®ååãåäžã®ãªã¹ããæãããã«ãã
ååã = ã®å·ŠèŸºä»¥å€ã®å Žæã«çœ®ããå Žåã¯ããã®ååã¯ããã®ååãæããªããžã§ã¯ãããããããã®ã§ããããã以äžã®ããã«ããŠãå¥ã®ååãåäžãªã¹ããæãããã«ããããšãã§ãããããããã°ãäžã€ã®ååãçšããŠããŠãªã¹ãã®èŠçŽ ãå€æŽããã°ãã¹ã€ã®ååã䜿ã£ãŠãªã¹ãã®èŠçŽ ãåç §ããå Žåã«ããåã«è¡ã£ãå€æŽãåæ ãããŠããããšã«ãªããC ã§ãããªãã¡ã¬ã³ã¹çãªæåãšãèšããã
ãªã¹ããïŒæ·±ãïŒã³ããŒãã
ãªã¹ãã®æµ ãã³ããŒã¯åã«èŠããããã«å¯ŸããŠãæ°ããªãªã¹ããšããŠããã£ããã§ã¯ãããå¥ã®ã¢ã€ãã³ãã£ãã£ããã€èŠçŽ ããæããªã¹ããäœæããå Žåããããæ·±ãã³ããŒãšåŒã¶ãããã¯ãã©ã€ãã©ãªãã€ã³ããŒãããã°ç°¡åã«è¡ããããšããããšã§ãçç¥ã
ã¹ã©ã€ã¹
ãªã¹ã L ã«å¯Ÿã㊠L[n:m] ãšã L[:] ãªã©ãšããè¡šçŸããããã¹ã©ã€ã¹èšæ³ãšåŒã°ãããm, n 㯠0 ãŸãã¯èªç¶æ°ã§ããªã¹ãäžã®äœçœ®ãè¡šçŸãããããšãã° L=[a,b,c] ã«ãããŠ
| | | | | a | b | c | | | | | 0 1 2 3
ãŠãªäœçœ®ã瀺ããŠããããªããL[:] 㯠L[0:ãªã¹ããµã€ãº] ã®ããšã§ããã
ãŸããã¹ã©ã€ã¹èšæ³ã§è¡šçŸããããã®ã = ã®å·ŠèŸºã«ç»å ŽãããšããèŠãã
L=['a', 'b', 'c', 'd']
ã«ãããŠã
L[1:3]=['j', 'k', 'l'] ã¯ã
ãªã¹ã L ã®éšåãªã¹ã L[1:3] ãã['j', 'k', 'l'] ãèŠçŽ ãšãããªã¹ãã«çœ®ãæããã
è£è¶³ã巊蟺ãã¹ã©ã€ã¹ãšãã= ã®å³èŸºã¯ãªã¹ããã¿ãã«ïŒåŸè¿°ïŒãªã©ïŒã® iterable ãªãã®ïŒã§ãªããŠã¯ãªãããL[0:1]=1 ã¯ãšã©ãŒã«ãªãã
çºå¿µãL=['a', 'b', 'c'] ã§ãããšããŠãæ·»åã䜿ã£ã L[1]=['p','q'] ã«ãã L ã®æããšããã®ãã®ã¯ ['a',['p','q'],'c',] ã«ãªãããã¹ã©ã€ã¹ã䜿ã£ã L[0:1]=['p','q'] ã§ã¯ L ã®æããšããã®ãã®ã¯ ['a','p','q','c'] ã«ãªãã
ã¹ã©ã€ã¹èšæ³ã§è¡šçŸããããã®ãã= ã®å·ŠèŸºä»¥å€ã«çŸããå Žåãããã¯ãããšã®ãªã¹ãã®éšåãªã¹ãããïŒæµ ãïŒè€è£œããŠäœæãããæ°ããªãªã¹ãããããããïŒLISP æ奜è ã®æ³šæãã¹ãç¹ã§ããïŒ
é¢æ°
æ°ããèšèªãåŠã¶ãšããé¢æ°ã®åŒæ°ãå€æž¡ããªã®ãåç §æž¡ããªã®ããšããã®ããæ°ã«ãªããã®ã ãPython ã§ã¯ãæååããªã¹ãã®æ§è³ªãèããŠã¿ããšããã®ãžãã®äºæ ã¯å®¹æã«æ³åãã€ãã
ãŸããæååãåŒæ°ã«ããé¢æ°ãè©ŠããŠã¿ãã
>>> def alterstring (s): ... s='aho' ... >>> x='rikou' >>> alterstring(x) >>> print x rikou
é¢æ°ã¯ãããŒã«ã«ãªåå空éã«ãx ãæãæååããæãåå s ãç»é²ãã
次㫠s ãæ°ããªæåå 'aho' ãæãããã«å€æŽããã
ãããã£ãŠãããšã® x ã¯çžå€ããã 'rikou' ãæããŠããã
次ã«ããªã¹ããåãåãé¢æ°ãè©ŠããŠã¿ãã
>>> def alterlist(t): ... t[0]='aho' ... >>> y=['rikou'] >>> alterlist(y) >>> print y[0] aho
é¢æ°ã¯ãããŒã«ã«ãªåå空éã«ãy ãæããªã¹ãããæãåå t ãç»é²ãã
次㫠ããt ãæããªã¹ããã®ç¬¬äžã® ããæåå 'aho' ãæãããã«å€æŽããã
ãããã£ãŠãy[0] 㯠'aho' ã«å€ãã£ãŠããã
åŒæ°ã®åãåã
é¢æ°ãåãåãåŒæ°ã®æ°ã¯ãäžå®ã§ãããããããã ã*ãã€ããä»®åŒæ°ã䜿ããšãå®çŸ©äžã§ããããã¿ãã«ãšããŠäœ¿ãããšãã§ããã
>>> def r(a, *b): # ä»®åŒæ° b ã¯äœã£ãåŒæ°ãåŒãåããã¿ãã« ... print a ... for x in b: ... print x ... >>> r(1,2,3) 1 2 3
ã·ã§ã«ã®ãã³ã°ãªãã·ã§ã³ã£ãœããããŒã¯ãŒãåŒæ°ã䜿ã£ãåŒæ°ã®æž¡ãæ¹ãããã
>>> def f(a, b): ... print a ... print b ... >>> f(b=2, a=1) 1 2
äžå®ã®æ°ã®ããŒã¯ãŒãåŒæ°ãäžãã** ã€ãã®ä»®åŒæ°ã§åãåããšãé¢æ°å®çŸ©ã®äžã§ã¯ãããèŸæžãšããŠäœ¿ããã
>>> def f(**t): ... for k in t: ... print k ... print t[k] ... >>> f(name='Kabi', addr='Aichi') name Kabi addr Aichi
* ãš ** ã®æ··çšãªã©ã§ããããããã©ããã
http://www.python.jp/doc/release/tut/node6.html#SECTION006720000000000000000
åç
§ã
ã¯ã©ã¹
Python ã®ã¯ã©ã¹ã¯ãããªã©ãã¯ã¹ãã䜿ãæ¹ãã§ãããã®ã ã
# äžèº«ããªãã¯ã©ã¹ >>> class c: # äžèº«ã空ã®ã¯ã©ã¹ ... pass ... >>> c.a=1 # ã¯ã©ã¹å±æ§ãã»ãã >>> i=c() # ã€ã³ã¹ã¿ã³ã¹äœæ >>> i.a2=2 # ã€ã³ã¹ã¿ã³ã¹å±æ§ãã»ãã
ã¯ã©ã¹äžã§ãªãããé¢æ°å®çŸ©ã¯ã第äžåŒæ°ãšããŠå¿ ãã€ã³ã¹ã¿ã³ã¹èªèº«ãåãåãããã®ããããã®ç¬¬äžåŒæ°ã«ã¯ self ãšããååãäžããã®ãç¿æ £ãšãªã£ãŠããã
# ã¯ã©ã¹ã®èšèšïŒå±æ§ãšã¡ãœããïŒ >>> class c: ... a=1 # ã¯ã©ã¹å±æ§å®çŸ© ... def m(self,x): # ã¡ãœããå®çŸ© ... return(self.a+x) ... >>> i=c() # ã€ã³ã¹ã¿ã³ã¹äœæ >>> i.m(2) # æé»ã®ãã¡ã«äžã€ç®ã®åŒæ°ãäžããããããã¯èªåèªèº« 3
__init__ ã¯ã¯ã©ã¹å ã§é¢æ°ãšããŠå®çŸ©ãããã€ã³ã¹ã¿ã³ã¹ãäœæããããšãã«èªåçã«å®è¡ãããã¡ãœãããšãªãã
>>> class c: ... def __init__(self): # é¢æ° ___init___ ã¯ã ... print(1) ... >>> i=c() # ã€ã³ã¹ã¿ã³ã¹äœææã«èªåçã«å®è¡ããã 1
åçŽãªç¶æ¿
>>> class p: ... a=1 ... >>> class c(p): ... b=2 ... >>> i=c() >>> i.a 1 >>> i.b 2
å€éç¶æ¿
>>> class p1: ... a=1 ... >>> class p2: ... b=2 ... >>> class c(p1,p2): ... c=3 ... >>> i=c() >>> i.a 1 >>> i.b 2 >>> i.c 3
ïŒhttp://www2.lib.uchicago.edu/keith/courses/python/class/5/ åèïŒ
äŸå€
æååãæŽæ°ã«åãæšãŠãããšãããšãåœç¶ãšã©ãŒã«ãªãããšã©ãŒã®çš®é¡ã¯ããã®å Žå ValueError ã§ããã
>>> int("p") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: 'p'
ããã°ã©ã ãå®è¡äžã«ç¹å®ã®çš®é¡ã®ãšã©ãŒãçºçãããšããäœããã®åäœããããããªããtry, except ã䜿ããããããšã©ãŒãæ§æãšã©ãŒãªãããšåºå¥ããŠãäŸå€ãšåŒã¶ããã finally æããããšãããã¯äŸå€ãåºãŠãåºãªããŠãæåŸã«å®è¡ãããã
>>> try: ... int("p") ... except ValueError: ... print "oops" ... finally: ... print "bye" oops bye
raise æãçšããŠã匷å¶çã«äŸå€ãçºçãããããšãã§ããã
>>> raise NameError Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError
ãªã except æããã£ããã§ããªãã£ãçš®é¡ã®äŸå€ã¯ããã®å€åŽã«ãã¹ãããã
>>> try: ... try: ... raise NameError ... except ValueError: ... print "I caught the value error" ... except NameError: ... print "I caught the NameError" ... I caught the NameError
ããè¿ã
while ãš for
while ã¯ä»ã®å€ãã®èšèªããé¡æšããããšããã®åããããããfor 㯠C ã® for ã§ã¯ãªããŠããããã·ã§ã«ã® for ã®ããã«æ¯ãèãã次é åç §ã
ã€ãã¬ãŒã¿
ã€ãã¬ãŒã¿ãšããã®ã¯ãã²ã©ã倧éæã«èšããšãnext() ã¡ãœãããæã¡ããããŸããŸã§ãããš StopIteration äŸå€ãæãããããªãªããžã§ã¯ããã§ããã
ãªã¹ãã®åèŠçŽ ãæååäžã®åæåãé ãããã¹ãŠèª¿ã¹ããããªå Žåããªã¹ããæååãããã€ãã¬ãŒã¿ãäœæããããã«å¯Ÿã㊠next() ã¡ãœãããé ã é©çšããã°ãããããæåŸãŸã§ãããš StopItaration ãšããäŸå€ãæããããã
>>> it=iter([1,2,3]) >>> it.next() 1 >>> it.next() 2 >>> it.next() 3 >>> it.next() Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration
[ã€ãã¬ãŒã¿ãªããžã§ã¯ãã¯ãããšã®ãªããžã§ã¯ããžã® "ãã€ã³ã¿" ãä¿æããŠããã®ã§ããã®æç¶ãã¯ããããŠã¡ã¢ãªãé£ããªãããã®åé¢ãfor ã«ãŒãäžã§ããšã®ãªããžã§ã¯ããå€æŽãããšããããïŒã€ãã¬ãŒã¿ãéããŠïŒæ¬¡å以åŸã®ç¹°ãè¿ãã«åœ±é¿ãåãŒãããšã«ãªãã]
for æã¯ãäžãããããªããžã§ã¯ãããã€ãã¬ãŒã¿ãªããžã§ã¯ããåŸãŠããã®ãªããžã§ã¯ãã® next ã¡ãœãããå®è¡ããn ããããé ã ã«æãããã«ããŠé²ã¿ãæåŸã« StopIteration äŸå€ãæãããããšåæ¢ããã
>>> for n in [1,2,3] >>> print n ... 1 2 3
ãããèªäœã¯ã©ã¹ã __iter__ ã¡ãœãããæã¡ããããã€ãã¬ãŒã¿ã€ãŸããnext() ã¡ãœãããæã¡ããããŸããŸã§ãããš StopIteration äŸå€ãæãããããªãªããžã§ã¯ãããè¿ããªãã°ããã㯠for ã«ãŒãã§äœ¿ããããã®ãšãè¿ãããã€ãã¬ãŒã¿ã¯èªåèªèº«ã§ãããŸããªãã
ãªã¹ãã«å¯Ÿããã€ãã¬ãŒã·ã§ã³ã䟿å©ã«
ãªã¹ãã«å¯Ÿããã€ãã¬ãŒã·ã§ã³äœæ¥ã«äœ¿ãã䟿å©ãªæ段ãããŸããŸã«çšæãããŠãããmap, zip, enumerate(list), reduce, å å è¡šçŸ, çã ã
ã²ãšã€ã ãããªã¹ããçµåããæŒç®å + ã®äžè¬åãäŸãšããŠåŒããšãããæé«ã«é«éãªæ¹æ³ãšã¯èšããªãã ããããåŠã¶ãšãããããã
>>> import operator >>> a=[[1,2],[4,5,6]] >>> reduce(operator.add,a) [1,2,3,4,5,6]
äœè«ãªãããå ¥ãåãªã¹ãã®è§£æ¶ïŒflatten=å¹³ãã«ããïŒã¯ããã°ãã°ç·Žç¿åé¡ã«ãªãããã ãäžã®äŸãšã¯éãã解æ¶ãã¹ãå ¥ãåã®æ·±ãã«å¶éããªããã®ãããé¡ã«ãªãã®ãç®ã«ããããããèããŠã¿ãã
def flatten(a): for n in reversed(range(len(a))): if type(a[n])==type([]): flatten(a[n]) a[n:n+1]=a[n]
ãããªæãâŠâŠã
>>> a=[1,[2,[3,[4]]]] >>> flatten(a) >>> a [1, 2, 3, 4]
ããšã®ãªã¹ããç Žå£ãã¡ãããã ãã©ãâŠâŠ
ãªã¹ãã«äŒŒãïŒãã®ãã¡
ã¿ãã«
ãªã¹ãã«äŒŒãŠããããäžèº«ãå€æŽã§ããªãã
äžžã«ãã³ã¯å ¥ãåã«ãããšã以å€ã¯äœ¿ããªããŠãããé·ã 1 ã®ã¿ãã«ãäœããšãã«æ³šæã
# é·ã 2 ã®ã¿ãã« t=1,2 t=(1,2) # é·ã 1 ã®ã¿ãã« t=1, t=(1,) # t=(1) ã¯æ°å€ã«ãªã£ãŠããŸã # é·ã 0 ã®ã¿ãã« t=() # t= ã¯æ§æãšã©ãŒ
ã¿ãã«ã«ãªã¹ããå ¥ãããšããã®ã¿ãã«ã¯ãã£ãšãã®ãªã¹ããæãç¶ãããããªã¹ãã®èŠçŽ ãå€ããããšã¯å¯èœã
>>> a=1 >>> a=[1] >>> t=(a) >>> t [1] >>> a[0]=2 >>> t [2]
èŸæž
èŸæžãšããçš®é¡ã®ãªããžã§ã¯ããããã®ã ãããã¯ãããŒãšå€ã®çµãããããäœã§ãããŒã«ã§ãããããªã¹ãã¯é§ç®ãå€ã®ã»ãã¯äœã§ãããã
>>> t={'a':1, 'b':2} # æååãããŒã«ããŠã¿ã >>> t['a'] # ãããã£ãŠå€ãåãåºã 1 >>> t={[1,2]:'a', [3,4]:'b'} # ãªã¹ããããŒã«ãããšæããã >>> t={(1,2):'a', (3,4):'b'} # ã¿ãã«ã¯ OK >>> t[(1,2)] # ãããã£ãŠå€ãåãåºã 'a' >>> t={([1],):'a', ([2],):'b'} # ãªã¹ããçŽããã¿ãã«ãé§ç®
ã¿ãã«ã¯ããŒã«ã§ããã
>>> t={(1,2):'a', (1,2):'b'} >>> t[(2-1,1+1)] 'b'
èŸæžããäœã£ãã€ãã¬ãŒã¿ã® next ã¡ãœããã¯ããŒãè¿ãã
>>> it=iter({'a':1, 'b':2}) >>> it.next() 'a' >>> it.next() 'b'
ã ãããfor æã䜿ã£ãŠã
>>> a={'a':1, 'b':2} >>> for x in a: ... print x ... print a[x] ... a 1 b 2
ã®ããã«äœ¿ãããšãã§ããã
é¢æ°ã®åŒæ°ãšããŠèŸæžãæž¡ãããšãã§ããã
>>> def f(d): ... for k in d: ... print k ... print d[k] ... >>> f({'a':1, 'b':2}) a 1 b 2
é å
é åãšããã®ã¯ããŸãã«ããããé åã§ããããªã¹ãã§ãã¿ãã«ã§ããªããããã¯ãé åããµããŒãããã¢ãžã¥ãŒã«ïŒNumeric ãšããnumarrayïŒãèªã¿èŸŒãŸãªããšäœ¿ããªããããããªã¹ããã¿ãã«ãšéããäœæãããšãã«ãã®èŠçŽ ã®åãå®ãŸããã¿ãã«ãšéããèŠçŽ ãæžãæããããšã¯ã§ãããããªã¹ããšéã䌞匵ãããããããšã¯ã§ããªãã
ãªã¹ããã¿ãã«ãããèšç®ãæ©ãã§ãããšããã¡ãªãããããããããããäœãã䟿å©ãªã®ã¯ããã¯ãã«ã®åãšãå·®ãã§ããããšããªã
ããã¹ãã®åŠç
æååã®çœ®æ
æååã¯å€æŽã§ããªãããããããªãããã¡ãœãããæã€ãåçŽãªçœ®æã¯ã
>>> "xccxccxcc".replace('cc','xx',2) 'xxxxxxxcc'
ãã®ä»æååã«å¯Ÿããã¡ãœãã㯠http://www.python.jp/doc/release/lib/string-methods.html ã«æžããŠããã
æ£èŠè¡šçŸã䜿ã£ã眮æã¯ãå¥é åç §ã
æ£èŠè¡šçŸ
Python ã«ã¯æ£èŠè¡šçŸã¯ã©ã¹ãšãããã®ããããæ£èŠè¡šçŸã䜿ã£ãæååã®çœ®æã¯ã
ãæååãªããžã§ã¯ã.眮æã¡ãœãã(æ£èŠè¡šçŸ, 眮ææåå)ã
ã§æååãå€æŽããã®ã§ã¯ãªãã
ãæ£èŠè¡šçŸãªããžã§ã¯ã.眮æã¡ãœãã(眮ææåå, 察象æåå)ã
ã§æ°ããªæååãè¿ãããããšã«ãã£ãŠè¡ããå
·äœçã«ã¯ã
import re # re ã¢ãžã¥ãŒã«ãã€ã³ããŒããã r = re.compile('a') # æ£èŠè¡šçŸãã³ã³ãã€ã«ã㊠RegexObject ã€ã³ã¹ã¿ã³ã¹ãäœã r.sub('d', 'abc') # ãã®ã€ã³ã¹ã¿ã³ã¹ã® sub ã¡ãœããã䜿ãã眮æçµæãè¿ãããã r.subn('d', 'abc', 1) # sub ã¯ãã¹ãŠçœ®æãsubn ã¯çœ®æåæ°ãå¶éã§ãã
ããã¯ã¹ã©ãã·ã¥ã«ãããšã¹ã±ãŒã
ããã¯ã¹ã©ãã·ã¥äžã€ãè¡šçŸããæ£èŠè¡šçŸãªããžã§ã¯ãã Python ã§ã€ãããšããre.compile ã¡ãœããã®åŒæ°ã '\\\\' ãšè¡šèšããªããŠã¯ãªããªããããã¯é¢åã«èŠããã
Perl 㧠$variable =~ /regexp/ ãšãããšãã® regexp ã«ã¯ãæ£èŠè¡šçŸãŸãã¯æ£èŠè¡šçŸã§ããæååãäžããã\\ ã¯ãæ£èŠè¡šçŸããã®ãã®ã§ãããããPerl ã§ããã¯ã¹ã©ãã·ã¥ã®è¡šçŸãšããŠäœ¿ãããšãã§ãããã'\\\\' ã¯ã\\ ãšããæååãã§ã\\ ã¯æ£èŠè¡šçŸã ãããããã䜿ããã
äžæ¹ãPython ã§ã¯ãre.compile(regexp) ãšãããšãã® regexp ã«ã¯ãæ£èŠè¡šçŸãã®ãã®ãäžããããšã¯ã§ãããæ£èŠè¡šçŸã§ããæååã ããäžããããšãã§ãããããã§ã'\\\\' ã®ããã«ããŠããã¯ã¹ã©ãã·ã¥ 2 ã€ã¶ãã®æååãã€ããã ãããããåŒæ°ãšããŠäžããã»ãã¯ãªãã
ãã ããr'\\' ãšããèšæ³ãããããã㯠'\\\\' ãšåçãªã®ã§ãæ£èŠè¡šçŸããæååãäœæãããšãã«ã¯éœåãããã
Python ãšæåã³ãŒã
é·ããªã£ãã®ã§å¥é
ãããã³ã°ãªã©
æ¥æ¬èªã®ãããã³ã°ãããŠãã³ãŒãåã«å¯ŸããŠãªãåé¡ããªãã
ïŒhttp://osksn2.hep.sci.osaka-u.ac.jp/~taku/osx/python/encoding.htmlãåèïŒ
>>> r=re.compile(u'ã') >>> m=r.match(u'ããã') >>> print m.span() (0,1)
ã¡ãããšãããããŠããããšããããã
æšæºåºåã«ãã®æååãããŸã衚瀺ãããŠããã®ã¯ããã®ããšãšã¯å¥ã®è©±ã§ããã
import sys import codecs sys.stdout = codecs.getwriter('utf_8')(sys.stdout) sys.stdin = codecs.getwriter('utf_8')(sys.stdout)
ãšãããšã以åŸæšæºåºå ¥åã«æ¥æ¬èªã䜿ããã
ãã¡ã€ã«ããã®åºå ¥å
open ã¯ãã¡ã€ã«ãªããžã§ã¯ããè¿ããïŒWindows ã Macintosh ã®å Žåãrbãwb ã®ããã«ãããšãã€ããªã¢ãŒãã«ãªãããã€ããªã»ããŒã¿ã®å Žåã«ã¯ããã䜿ããªããšããºãããšã«ãªãããã ïŒ
f=open('/home/baka/a.txt', 'r')
â»èªã¿åºãå°çš rãæžã蟌ã¿å°çš wãèªã¿æžã r+ãè¿œèš a
ã³ãŒãã£ã³ã°ãæå®ããŠéãã«ã¯ã
import codecs f=codecs.open('a.txt', 'r', 'utf_8')
â»utf_8, euc_jp, shift_jis, etc.
>>> f.read() # æå®ãµã€ãºã¶ãèªãããµã€ãºçç¥ãè² æ°ã®å Žåå šéšèªã >>> f.readline() # äžè¡èªã >>> f.readlines() # æå®ãµã€ãºãã€èªã¿ãäžè¡ãå®æãããåè¡ããªã¹ãã«ãã
ããããŠèªã¿åºãããæååã¯ãunicode åãªããžã§ã¯ãã§ãããutf ã EUC ãšãã£ãã³ãŒãã£ã³ã°ã·ã¹ãã ã§ãšã³ã³ãŒãããããã€ã䞊ã³ïŒstr åãªããžã§ã¯ãïŒã§ã¯ãªãã詳ããã¯å¥ããŒãžãPython ãšæåã³ãŒãã
äžè¡ãã€èªãå¥ã®èªã¿åºãæ¹ãããã
>>> for line in f: print line,
ãã¡ã€ã«ã«æžãã«ã¯ã
f.write('abc\n')
ã³ãŒãã£ã³ã°ãæå®ããŠæžãã«ã¯ã
import codecs f=codecs.open('a.txt', 'w', 'utf_8')
ãã¡ã€ã«ãéãã
f.close()
现ããªããš
ã³ãã³ãã©ã€ã³åŒæ°ã
import sys arg = sys.argv[1]
ãªãã·ã§ã³ã®åŠçã¯ãã¡ã£ãšè€éãhttp://www.python.jp/doc/release/lib/optparse-tutorial.html åèã
ãœãŒã¹ãã¡ã€ã«ã«æååå®æ°ãæžãå Žåããã¡ã€ã«é ã«
# coding: utf-8
ãšãæžããŠãããã°ãªãã¬ã
ã§ã¯è©Šãã«äœ¿ã£ãŠã¿ã
ãã¡ã€ã«äžã®ãé¿åãããã¢ããã«å€æããŠæšæºåºåã«åºããŠã¿ãã®ã ã
# coding: utf-8 import sys, codecs, re regexp = u'é¿å' replace = u'ã¢ã' infile=sys.argv[1] sys.stdout = codecs.getwriter('utf_8')(sys.stdout) r=re.compile(regexp) f=codecs.open(infile, 'r', 'utf_8') b=f.readlines() for l in b: print r.sub(replace, l), f.close()
[ãªãããã¡ã€ã«ãžã®ãã¹ã¯ããã£ã¬ã¯ããªåºåãèšå·ã«ãšããããªãã§ããããã«ãos.path ã¢ãžã¥ãŒã«ã䜿ãã®ãæ£åŒã ããã§ããã]
å®è¡çµæã
$ cat infile.txt ãã» é¿å ãã» é¿å $ python test.py infile.txt ãã» ã¢ã ãã» ã¢ã
ã¢ãžã¥ãŒã«ã£ãœãã¢ãžã¥ãŒã«
re ãšãã¯ãã§ã«èŠããä»ã®æšæºã®ã¢ãžã¥ãŒã«ãèŠãã®ã ã
sqlite3 ã䜿ã£ãŠã¿ã
Python ã®ã¢ãžã¥ãŒã«ãšããã®ã¯æããå å®ããŠããŠãsqlite3 çšã®ã¢ãžã¥ãŒã«ãæšæºã§ã€ããŠãã(Python 2.5 以åŸ)ããããç°¡åã«äœ¿ãããã ããhttp://docs.python.org/library/sqlite3.html åèã
>>> import sqlite3 # ã©ã€ãã©ãªãã€ã³ããŒã >>> conn = sqlite3.connect('test.sqlite') # ããŒã¿ããŒã¹ãéã >>> cur = conn.cursor() # ã«ãŒãœã«äœæ >>> cur.execute("select * from mytable") # SQL æãçºè¡ <sqlite3.Cursor object at 0xb7d03bc0> >>> print cur.fetchone() # çµæã 1 è¡ã¶ãåŸã (u'Mr. Holms', u'111-1111') >>> print cur.fetchone() # ãã 1 è¡ã¶ã (u'Dr. Watson', u'222-2222') >>> print cur.fetchone() # ãã 1 è¡ã¶ã None conn.close() # ããŒã¿ããŒã¹ãéãã
ãã㯠sqlite ã®æãé£ãã§ããã¯ãæãããç°¡åã ã泚æïŒããŒã¿ããŒã¹ãéããšãã«ã該åœãããã¡ã€ã«ããªããšèªåçã«ãã®ãã¡ã€ã«ãäœæãããã
SQL æäžã«ãååãåã蟌ãå Žåãç¹å¥ãªæ¹æ³ã䜿ããªããã°ãªããªããplaceholder ãšã㊠? ã䜿ãããã®ä»çµã¿ã¯ SQL ã€ã³ãžã§ã¯ã·ã§ã³ãé²ãããã«ããããŸããåã蟌ãŸããåŽã®ãªããžã§ã¯ãã¯ãã¿ãã«ã§ãªããšãããªãã£ããšæããã€ãŸãã1 ã®ãããã«ã(1,) ã§ãªããŠã¯ãªããªãããããªãã£ãããªã
>>> cur.execute("insert into mytable (name, phone) values(?, ?)", (name,phone)) <sqlite3.Cursor object at 0xb7d03c20> >>> conn.commit()
æåã³ãŒãã®åé¡ã«ã€ããŠã¯ãå¥ããŒãžãPython ãšæåã³ãŒãã
éå€ãªããš
å
type ã§åãããããšããã
>>> type('a') <type 'str'>
ãšããå ·åã ãããããããã¯ããã«åã衚瀺ããã ãã§ã¯ãªãã
>>> c=type('a') >>> c() ''
ãªããŠããšãã§ãããããtype ãè¿ãã®ã¯ãåãäœæããèœåãæã€ã¯ã©ã¹ãããã®ã ã