ãã®èšäºã§ã¯ãPythonã®åºæ¬ã«ã€ããŠèª¬æããŸãã äžè¬çã«ãç®æšã«è¿ã¥ãã€ã€ãããŸããäžè¬ã«ãããŒã¿ãµã€ãšã³ã¹ã®ã¡ã€ã³ã©ã€ãã©ãªã§ã®äœæ¥ãããã«éå§ããTensorFlowã䜿çšããŸãïŒãã¥ãŒã©ã«ãããã¯ãŒã¯ã®èšè¿°ãšå±éã«ã€ããŠã¯ããã£ãŒãã©ãŒãã³ã°ãç解ã§ããŸãïŒã
èšçœ®
Pythonã¯python.orgããããŠã³ããŒãã§ããŸãã ãã ãããŸã ã€ã³ã¹ããŒã«ãããŠããªãå Žåã¯ã代ããã«
Anacondaé
åžããã±ãŒãžããå§ãããŸããããã«ã¯ãããŒã¿ãµã€ãšã³ã¹ã®åéã§ã®äœæ¥ã«å¿
èŠãªã»ãšãã©ã®ã©ã€ãã©ãªãæ¢ã«å«ãŸããŠããŸãã
Anacondaãã£ã¹ããªãã¥ãŒã·ã§ã³ã䜿çšããŠããªãå Žåã¯ãå¿
ãpipããã±ãŒãžãããŒãžã£ãŒãã€ã³ã¹ããŒã«ããŠãã ãããããã«ããããµãŒãããŒãã£ã®ããã±ãŒãžãç°¡åã«ã€ã³ã¹ããŒã«ã§ããããã«ãªããŸãã ãŸããã¯ããã«ãŠãŒã¶ãŒãã¬ã³ããªãŒãªã€ã³ã¿ã©ã¯ãã£ãIPythonã·ã§ã«ãã€ã³ã¹ããŒã«ãã䟡å€ããããŸãã Anacondaãã£ã¹ããªãã¥ãŒã·ã§ã³ã«ã¯pipãšIPythonãä»å±ããŠããããšã«æ³šæããŠãã ããã
空çœ
å€ãã®ããã°ã©ãã³ã°èšèªã¯ãå·®å¥åã³ãŒããããã¯ã䜿çšããŸãã
äžãã£ãã ã€ã³ãã³ãã¯Pythonã§äœ¿çšãããŸãïŒ
ããã«ãããã³ãŒããèªã¿ããããªããŸãããåæã«æžåŒèšå®ã«åŸãããã«ãªããŸãã æ¬åŒ§ãšè§æ¬åŒ§å
ã®ã¹ããŒã¹ã¯ç¡èŠãããããã詳现ãªåŒãç°¡åã«èšè¿°ã§ããŸãã
èªã¿ãããã³ãŒãïŒ
ã¹ããŒãã¡ã³ããç¶è¡ããã«ã¯ã次ã®è¡ã§ããã¯ã¹ã©ãã·ã¥ã䜿çšãããŸããããã®ãããªã¬ã³ãŒãã¯ã»ãšãã©äœ¿çšãããŸããã
two_plus_three = 2 + \ 3
ã³ãŒãã空çœã§ãã©ãŒããããããšãã³ãŒããPythonã·ã§ã«ã«ã³ããŒããŠè²Œãä»ããããšãé£ãããªããŸãã ããšãã°ã次ã®ã³ãŒããã³ããŒããããšããŠããŸãïŒ
for i in [ 1, 2, 3, 4, 5] :
æšæºã®Pythonã·ã§ã«ã«è¿œå ãããšããšã©ãŒãã¹ããŒãããŸãã
ã€ã³ã¿ãŒããªã¿ãŒã«ãšã£ãŠã空ã®æååã¯forã«ãŒãã§ã³ãŒããããã¯ã®çµããã瀺ãããã§ãã
IPythonã©ãããŒã«ã¯ã空çœãå«ããã¹ãŠãã¯ãªããããŒãã«æ£ãã貌ãä»ãããããžãã¯ãïŒ
貌ãä»ãæ©èœããããŸãã
ã¢ãžã¥ãŒã«ïŒã©ã€ãã©ãªã®ã€ã³ããŒãïŒ
äžéšã®PythonããŒã¹ã®ããã°ã©ãã³ã°ç°å¢ã©ã€ãã©ãªã¯ãããã©ã«ãã§ã¯ããŒããããŸããã ãããã®ããŒã«ã䜿çšããã«ã¯ãããããå«ãã¢ãžã¥ãŒã«ãã€ã³ããŒãããå¿
èŠããããŸãã
1ã€ã®æ¹æ³ã¯ãã¢ãžã¥ãŒã«èªäœãåçŽã«ã€ã³ããŒãããããšã§ãã
import re my_regex = re.compile ("[0-9]+",re.I)
ããã§
reã¯ãæ£èŠè¡šçŸãæäœããããã®é¢æ°ãšå®æ°ãå«ãã¢ãžã¥ãŒã«ã®ååã§ãã ãã®æ¹æ³ã§ã¢ãžã¥ãŒã«å
šäœãã€ã³ããŒãããããšã«ãããreãåã«ä»ããŠæ©èœã«ã¢ã¯ã»ã¹ã§ããŸãã
ã³ãŒãã«reãšããååã®å€æ°ãæ¢ã«ããå Žåã¯ãã¢ãžã¥ãŒã«ãšã€ãªã¢ã¹ã䜿çšã§ããŸãã
import re as regex my_regex = regex.compile("[0-9)+",regex.I)
ãšã€ãªã¢ã¹ã¯ãã€ã³ããŒããããã¢ãžã¥ãŒã«ã«æ±ãã«ããååãããå Žåããã¢ãžã¥ãŒã«ãé »ç¹ã«ã¢ãžã¥ãŒã«ã«ã¢ã¯ã»ã¹ããå Žåã«ã䜿çšãããŸãã
ããšãã°ã
matplotlibã¢ãžã¥ãŒã«ã«åºã¥ããŠããŒã¿ãèŠèŠåããå Žåãéåžžã¯
次ã®æšæºãšã€ãªã¢ã¹ã䜿çšããŸãã
import matplotlib.pyplot as plt
ã¢ãžã¥ãŒã«ããããã€ãã®ç¹å®ã®å€ãååŸããå¿
èŠãããå Žåãããããæ瀺çã«ã€ã³ããŒãããå¶éãªãã§äœ¿çšã§ããŸãã
from collections import defaultdict , Counter lookup = defaultdict(int) my_counter = Counter()
æ©èœ
é¢æ°ã¯ã0å以äžã®å
¥ååŒæ°ãåãã察å¿ããçµæãè¿ãã«ãŒã«ã§ãã Pythonã§ã¯ãéåžžãé¢æ°ã¯defã¹ããŒãã¡ã³ãã䜿çšããŠå®çŸ©ãããŸãã
def double() : """, , docstring, , . , 2""" return * 2
Pythonã®é¢æ°ã¯ããã¡ãŒã¹ãã¯ã©ã¹ãªããžã§ã¯ããšããŠæ±ãããŸãã ããã¯ãä»ã®åŒæ°ãšåæ§ã«ãå€æ°ã«å€æ°ãå²ãåœãŠãŠä»ã®é¢æ°ã«æž¡ãããšãã§ããããšãæå³ããŸãã
ããã«ãçãå¿åé¢æ°ãŸãã¯ã©ã ãåŒãç°¡åã«äœæã§ããŸãã
= apply_to_one(lambda : + 4)
ã©ã ãåŒãå€æ°ã«å²ãåœãŠãããšãã§ããŸãã ãã ããdefæŒç®åã䜿çšããããšããå§ãããŸãã
another double = lmbd : 2 *
ããã«ãããã©ã«ãåŒæ°ãé¢æ°ãã©ã¡ãŒã¿ãŒã«æž¡ãããšãã§ããŸããããã¯ãããã©ã«ã以å€ã®å€ãäºæ³ãããå Žåã«ã®ã¿æå®ããå¿
èŠããããŸãã
def my_print (message="oe " ): print (message ) my_print ( "pe")
åŒæ°ãååã§æå®ããããšããå§ãããŸãã
å°æ¥ãé¢æ°ã¯éåžžã«é »ç¹ã«äœ¿çšãããŸãã
è¡
äž¡åŽã®æååïŒãŸãã¯æåã®ã·ãŒã±ã³ã¹ïŒã¯ãäžéåŒçšç¬ŠãŸãã¯äºéåŒçšç¬Šã«å¶éãããŸãïŒäžèŽããå¿
èŠããããŸãïŒã
single_quoted_string = ' '
ããã¯ã¹ã©ãã·ã¥ã¯ãç¹æ®æåããšã³ã³ãŒãããããã«äœ¿çšãããŸãã äŸïŒ
tab_string = "\t"
çŽæ¥ééããããã¯ã¹ã©ãã·ã¥èªäœãå¿
èŠãªå Žå
Windowsãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®ãã£ã¬ã¯ããªåã«r '"'ã䜿çšãããšã
æžåŒãªãæååãäœæã§ããŸãã
not_tab_string = r"\t"
è€æ°è¡ã®ããã¹ããããã¯ã¯ãããªãã«ã·ã³ã°ã«ïŒãŸãã¯
ããã«ïŒåŒçšç¬ŠïŒ
multi_line_string = """ . """
äŸå€
äœãåé¡ãçºçãããšãPythonã¯äŸå€ãã¹ããŒããŸãã æªåŠçã®äŸå€ã«ãããããã°ã©ã ãäºæããåæ¢ããŸãã äŸå€ã¯
tryããã³
except ã¹ããŒãã¡ã³ãã䜿çšããŠåŠçãããŸãïŒ
try: print (0 / 0) except ZeroDivisionError : rint ( " ")
å€ãã®ããã°ã©ãã³ã°èšèªã§ã¯ãäŸå€ã®äœ¿çšã¯æªãããã°ã©ãã³ã°ã¹ã¿ã€ã«ãšèŠãªãããŸãããPythonã§ã¯ãã³ãŒããããã¯ãªãŒã³ã«ããããã«äœ¿çšããããã©ãããå¿é
ããå¿
èŠã¯ãããŸããã
ãªã¹ã
ããããPythonã§æãéèŠãªããŒã¿æ§é ã¯ãªã¹ãã§ãã ããã¯ãä»ã®ããã°ã©ãã³ã°èšèªã®é
åã«äŒŒãŠããŸãããæ©èœãè¿œå ãããåãªãé åºä»ãã³ã¬ã¯ã·ã§ã³ïŒãŸãã¯ã³ã¬ã¯ã·ã§ã³ïŒã§ãã
integer_list = [1, 2, ]
å€ãèšå®ããè§æ¬åŒ§ã䜿çšããŠãªã¹ãã®nçªç®ã®èŠçŽ ã«ã¢ã¯ã»ã¹ã§ããŸãã
= list(range (10))
ããã«ããªã¹ãããã«ãããããããã«è§æ¬åŒ§ã䜿çšãããŸãã
first_three = [:]
Pythonã«ã¯ãèŠçŽ ããªã¹ãã«å±ããŠãããã©ããã確èªããlnã¹ããŒãã¡ã³ãããããŸãã
1 ln [1, 2, 3]
ãã§ãã¯ã¯ãã¹ãŠã®èŠçŽ ãäžåºŠã«1ã€ãã€èŠãããšã«ããããããªã¹ããå°ããããšã確èªãããŠãããããã§ãã¯ã«ãããæéã«é¢ä¿ãªãå Žåã«ã®ã¿äœ¿çšããŠãã ããã
ãªã¹ãã¯ç°¡åã«é£çµã§ããŸãïŒ
= [1, 2, 3] . extend ( [ 4, 5, 6] )
ãªã¹ãxãå€æŽããã«æ®ãããå Žåã¯ããªã¹ãã®è¿œå ã䜿çšã§ããŸãã
= [1, 2, 3] = + [4, 5, 6]
éåžžã1ã€ã®æäœã§1ã€ã®èŠçŽ ããªã¹ãã«è¿œå ãããŸãã
= [1, 2, 3] x.append (0)
ãªã¹ãã«å«ãŸããèŠçŽ ã®æ°ãããã£ãŠããå Žåããªã¹ãã解åãããšäŸ¿å©ãªããšããããããŸãã
, = [1, 2]
åŒã®äž¡åŽã§èŠçŽ ã®æ°ãåãã§ãªãå ŽåãValueErrorãšã©ãŒã¡ãã»ãŒãžã衚瀺ãããŸãã
ç Žæ£ãããå€ã«ã¯ãéåžžãã¢ã³ããŒã¹ã³ã¢ã䜿çšãããŸãã
_, = [1, 2]
ã¿ãã«
ã¿ãã«ã¯ãªã¹ãã®äžå€ïŒãŸãã¯äžå€ïŒã®ããšãã§ãã
ãªã¹ããå€æŽããã«ãªã¹ãã§å®è¡ã§ããã»ãšãã©ãã¹ãŠã®åŠçã¯ãã¿ãã«ã§å®è¡ã§ããŸãã è§æ¬åŒ§ã®ä»£ããã«ãäžžæ¬åŒ§ã§ã¿ãã«ãäœæããããè§æ¬åŒ§ãªãã§ã¿ãã«ãäœæã§ããŸãã
my_list = [1, 2]
ã¿ãã«ã¯ãé¢æ°ããè€æ°ã®å€ãè¿ã䟿å©ãªæ¹æ³ãæäŸããŸãã
ã¿ãã«ïŒããã³ãªã¹ãïŒã¯ãè€æ°ã®å²ãåœãŠã§ã䜿çšãããŸãã
, = 1, 2
èŸæž
èŸæžãŸãã¯é£æ³ãªã¹ãã¯ãå¥ã®åºæ¬çãªããŒã¿æ§é ã§ãã
ãã®äžã§ãå€ã¯ããŒã«é¢é£ä»ããããŠãããããç¹å®ã®ããŒã«å¯Ÿå¿ããå€ããã°ããååŸã§ããŸãã
empty_dict = {}
ããŒå€ã«ã¯ãè§æ¬åŒ§ã䜿çšããŠã¢ã¯ã»ã¹ã§ããŸãã
rigory_aleksee = grades[ "Grigoriy"]
èŸæžã«ãªãå€ãèŠæ±ããããšãããšãKeyErrorãšã©ãŒã¡ãã»ãŒãžã衚瀺ãããŸãã
try: kates_grade = grades [ "Kate "] except eyError: rint ( " ! " )
inæŒç®åã䜿çšããŠããŒã確èªã§ããŸãã
grigoriy_has_grade = "Grigoriy" in grades
èŸæžã«ã¯getïŒïŒã¡ãœããããããæ¬ èœããŠããããŒãæ€çŽ¢ãããšãã«ãäŸå€ãã¹ããŒãã代ããã«ããã©ã«ãå€ãè¿ããŸãã
grigoriy_grade = grades. get ( "Grigoriy ", 0)
ããŒå€ã®å²ãåœãŠã¯ãåãè§æ¬åŒ§ã䜿çšããŠå®è¡ãããŸãã
grades [ "Tim" ] = 99
èŸæžã¯ãæ§é ãè¡šãç°¡åãªæ¹æ³ãšããŠãã䜿çšãããŸã
ããŒã¿ïŒ
tweet = { "user" : " grinaleks", "text" : " - ", " retweet_count" : 100, "hashtags " : [ "# data", " #science", " #datascience " , " #awesome", "#yolo" ] }
åã
ã®ããŒã®æ€çŽ¢ã«å ããŠãäžåºŠã«å
šå¡ã«é£çµ¡ã§ããŸãã
tweet_keys = tweet.keys()
ããŒã¯äžå€ã§ãªããã°ãªããŸããã ç¹ã«ããªã¹ããããŒãšããŠäœ¿çšããããšã¯ã§ããŸããã è€åããŒãå¿
èŠãªå Žåã¯ãã¿ãã«ã䜿çšããããããŒãæååã«å€æããæ¹æ³ãèŠã€ããããšããå§ãããŸãã
DefaultdictèŸæž
ããã¥ã¡ã³ãã§åèªãæ°ããå¿
èŠããããšããŸãã ãã®åé¡ã®æãããªè§£æ±ºçã¯ãããŒãåèªã§å€ãåèªã®é »åºŠïŒãŸãã¯ããã¹ãå
ã®åèªã®åºçŸæ°ïŒã§ããèŸæžãäœæããããšã§ãã åèªããã§ãã¯ãããšãã«ãçŸåšã®åèªãæ¢ã«èŸæžã«ããå Žåããã®é »åºŠã¯å¢å ããååšããªãå Žåã¯ãèŸæžã«è¿œå ãããŸãã
ããã«ããèš±å¯ãããèš±ããæ±ããæ¹ãè¯ãããšåŒã°ããæ¹æ³ãå©çšããŠãæ¬ èœããŠããããŒã«ã¢ã¯ã»ã¹ããããšãããšãã«ãšã©ãŒããã£ããã§ããŸãã
word_ counts = { } for word in document : try: word_counts [word] += 1 except eyError : word_counts [word] = 1
3çªç®ã®ããªãã¯ã¯ãgetïŒïŒã¡ãœããã䜿çšããããšã§ããããã¯ãããŒãæ¬ èœããŠããç¶æ³ãããšã¬ã¬ã³ãã«æãåºããŸãã
word_counts = { } for word in document : previous_count = word_counts.get (word, 0) word_counts [word] = previous_count + 1
ãããã®ææ³ã¯ãã¹ãŠå°ãé¢åã§ãããã®ãããdefaultdictèŸæžïŒããã©ã«ãå€ãæã€èŸæžãšãåŒã°ããŸãïŒã䜿çšããããšããå§ãããŸãã 1ã€ã®æ©èœãé€ããŠãéåžžã®èŸæžã®ããã«èŠããŸãããã®äžã«ãªãããŒã«ã¢ã¯ã»ã¹ããããšãããšãæåã«åŒæ°ãªãã®é¢æ°ã䜿çšããŠå€ãè¿œå ããŸãã defaultdictèŸæžã䜿çšããã«ã¯ãããããã³ã¬ã¯ã·ã§ã³ã¢ãžã¥ãŒã«ããã€ã³ããŒãããå¿
èŠããããŸãã
from collections import defaultdict word_counts = defaultdict(int)
ããã«ãdefaultdictèŸæžã®äœ¿çšã¯ããªã¹ããèŸæžãããã«ã¯ãŠãŒã¶ãŒå®çŸ©é¢æ°ãæäœããå Žåã«å®çšçã§ãã
dd_list = defaultdict (list)
ãããã®æ©èœã¯ãèŸæžããã³ã¬ã¯ã·ã§ã³ãã«äœ¿çšãããšãã«å¿
èŠã«ãªããŸã
ç¹å®ã®ããŒã®çµæãããã³éè€ãé¿ããå¿
èŠãããå Žå
èŸæžå
ã®ããŒã®ååšããã§ãã¯ããŸãã
ã«ãŠã³ã¿ãŒèŸæž
ã«ãŠã³ã¿ãŒãã£ã¯ã·ã§ããªã®ãµãã¯ã©ã¹ã¯ãå€ã®ã·ãŒã±ã³ã¹ãdefaultdictïŒintïŒã®ãããªãªããžã§ã¯ãã«å€æããŸãããã®ãªããžã§ã¯ãã§ã¯ãããŒãåšæ³¢æ°ã«ããããããããæ£ç¢ºã«ã¯ãããŒãåšæ³¢æ°ã§è¡šç€ºïŒãããïŒãããŸãã
äž»ã«ãã¹ãã°ã©ã ã®äœææã«äœ¿çšãããŸãïŒ
from collections import Counter = Counter([0,1,2,0])
ãã®æ©èœã«ãããåèªã®é »åºŠãæ°ããåé¡ãç°¡åã«è§£æ±ºã§ããŸãã
ã«ãŠã³ã¿ãŒèŸæžã«ã¯most_commonïŒïŒã¡ãœããããããããã¯å€ãã®å Žå䟿å©ã§ãã
å€ãã®
ã»ãããŸãã¯ã»ããã®ããŒã¿æ§é ã¯ãç¹°ãè¿ãã®ãªãé åºä»ããããŠããªãèŠçŽ ã®ã³ã¬ã¯ã·ã§ã³ã§ãã
s = set ()
å€ãã¯2ã€ã®çç±ã§äœ¿çšãããŸãã ãŸããã»ããã®inæäœã¯éåžžã«é«éã§ãã ç¹å®ã®ã·ãŒã±ã³ã¹ã«å±ããèŠçŽ ã®å€§ããªã»ããããã§ãã¯ããå¿
èŠãããå Žåããªã¹ããããã»ããããŒã¿æ§é ã®æ¹ãé©ããŠããŸãã
2çªç®ã®çç±ã¯ãããŒã¿ã»ããå
ã®äžæã®èŠçŽ ãååŸããããšã§ãã
item_list = [1, 2, 3, 1, 2, 3]
å€ãã¯ãèŸæžããªã¹ããããã¯ããã«å°ãªãé »åºŠã§äœ¿çšãããŸãã
å¶åŸ¡æ§é
ä»ã®ã»ãšãã©ã®ããã°ã©ãã³ã°èšèªãšåæ§ã«ãifã¹ããŒãã¡ã³ãã䜿çšããŠæ¡ä»¶ããšã«ã¢ã¯ã·ã§ã³ãå®è¡ã§ããŸãã
if 1 > 2: message " 1 2 . . . " elif 1 > 3: message "elif 'else if '" else: message = " , else "
ããã«ãå°æ¥äœ¿çšãããå¯èœæ§ã®ããåäžè¡ã®ããªãã«if-then-elseæŒç®åã䜿çšã§ããŸãã
parity = "" if % 2 === else " "
Pythonã«ã¯ã«ãŒãããããŸãïŒ
= 0 while < 10: print (x, " 10") += 1
ãã ããforã«ãŒãã¯inæŒç®åã§ããé »ç¹ã«äœ¿çšãããŸãã
for in range (lO) : print (x, " 10" ) 51
ããè€éãªã«ãŒãå¶åŸ¡ããžãã¯ãå¿
èŠãªå Žåã¯ãæŒç®åã䜿çšã§ããŸã
continue break: for 1n range (10) : 1f == 3: continue
ãã®çµæã0ã1ã2ãããã³4ãå°å·ãããŸãã
çå®
Pythonã®ããŒã«å€æ°ã¯ãä»ã®ã»ãšãã©ã®ããã°ã©ãã³ã°èšèªãšåãããã«æ©èœããŸããã1ã€ã ãäŸå€ããããŸã-倧æåã§ãïŒ
one_is_less_than_two = 1 < 2
ååšããªãå€ã瀺ãããã«ãä»ã®èšèªã®nullã«å¯Ÿå¿ããç¹å¥ãªNoneãªããžã§ã¯ãã䜿çšãããŸãã
= None print (x == None )
Pythonã¯ãããŒã«åãæåŸ
ãããä»»æã®å€ã䜿çšã§ããŸãã 次ã®ãã¹ãŠã®èŠçŽ ã®ããŒã«å€ã¯Falseã§ãã
- åœ ã
- ãªã
- ã»ããïŒïŒïŒã»ããïŒïŒ
- []ïŒç©ºã®ãªã¹ãïŒ;
- {}ïŒç©ºã®èŸæžïŒ;
ä»ã®ã»ãšãã©ãã¹ãŠãçå®ãšèŠãªãããŸãã ããã«ããã空ã®ãªã¹ãããã§ãã¯ããifã¹ããŒãã¡ã³ããç°¡åã«äœ¿çšã§ããŸãã 空è¡ã空ã®èŸæžãªã©ããã ãã次ã®ããšãèæ
®ããªããšãèªèãã«ãããšã©ãŒãçºçããå ŽåããããŸãã
s = some_function_that_returns_a_string ()
åãããšãããç°¡åãªæ¹æ³ã次ã«ç€ºããŸãã
first_char = s and s [0]
è«çæŒç®åã¯ãæåã®å€ãtrueã®å Žåã¯2çªç®ã®å€ãè¿ããfalseã®å Žåã¯æåã®å€ãè¿ãããã§ãã åæ§ã«ã次ã®åŒã®xãæ°å€ããŸãã¯å Žåã«ãã£ãŠã¯Noneã®å Žåãçµæã¯äœããã®åœ¢ã§æ°å€ã«ãªããŸãã
safe = or 0
Pythonã®ãã¹ãŠã®çµã¿èŸŒã¿é¢æ°ã¯ãªã¹ããåãåããåãªã¹ãé
ç®ãtrueã®å Žåã«ã®ã¿Trueãè¿ããŸããå°ãªããšã1ã€ã®èŠçŽ ãtrueã®å Žåãçµã¿èŸŒã¿é¢æ°ã¯trueãè¿ããŸãã
all ( [True, 1, { 3 }])