Pythonã§æ¸ãã便å©ãªæ¸ãæ¹ãç´¹ä»ãã¾ãã
çããããã«ã¡ã¯
ãå
æ°ã§ãããç§ã¯å
æ°ã§ãã
ä»æ¥ã¯Pythonã«ããã便å©è¨æ³ãæ¸ãã¦ã¿ã¾ãã
ä»ã®è¨èªãã使ã£ããã¨ç¡ã人ããã²
â»1/25ééããã¨æãããç®æã®å çä¿®æ£ãå®æ½
- ãªã¹ãã«é¢ããã¤ãã¬ã¼ã·ã§ã³
- Collections
- Sortï¼ã½ã¼ãï¼
- ifæã®æ¸ãæ¹
- Fileã«é¢ããå¦çï¼withï¼
- inï¼ãªãã¸ã§ã¯ãã«åå¨ãã¦ãããã©ããï¼
- é¢æ°ã«ã¤ãã¦
- åèæç®
ãªã¹ãã«é¢ããã¤ãã¬ã¼ã·ã§ã³
enumerateï¼ã¤ã³ããã¯ã¹ã¨ãªãã¸ã§ã¯ãã®ã«ã¼ãï¼
enumerateã¯iterateåæ°ã¨ãªãã¸ã§ã¯ããåæã«ã«ã¼ãã®è¨è¼ããããã¨ãã§ãã¾ãã
è¦ç´ ã®içªç®ãåå¾ãã¦ã«ã¼ããæ¸ãã¨ä»¥ä¸ã®è¨è¼æ¹æ³ã«ãªãã¾ãã
list = [1,2,3,4,5,6] for i in xrange(len(list)): print i,list[i]
enumerateé¢æ°ã使ãã¨ãããç´æçã«è¨è¼ãããã¨ãã§ãã¾ãã
for i,number in enumerate(list): print i,number
zipï¼åæã«ï¼ã¤ã®ãªãã¸ã§ã¯ãã§ã«ã¼ãï¼
zipã使ç¨ãã¦åºãã¦ã2ã¤ä»¥ä¸ã®é
åãiterationããã
ä¸ã¤ã ãã§ãªããã»ããã§åºãããã¨ãã§ãã¾ãã
list,list2 = [1,2,3,4],[3,4,5,6] for i in xrange(len(list)): print list[i],list2[i]
ãããzipã使ã£ã¦æ¸ãã¨æ¬¡ã®ããã«ãªãã¾ãã
list,list2 = [1,2,3,4],[3,4,5,6] for num1,num2 in zip(list,list2): print num1,num2
å ã¿ã«zipã¨enumerateãçµã¿åãããä½çªç®ãã¤ãzipã«ããåºãããã¨ãã§ãã¾ãã
list,list2 = [1,2,3,4],[3,4,5,6] for index,(num1,num2) in enumerate(zip(list,list2)): print index,num1,num2
yieldï¼éä¸ã§ä¸æ¦è¿ãï¼
yieldè¨æ³ã¯å人çã«ãããå¦ç¿ã®æã«ä½¿ããã¨ãå¤ãã§ãã
ç´æçã«ãããã«ããã®ã§ãããyieldã®æã«ä¸åå¤ãè¿ãã¨ãã£ããã¨ããã¾ãã
def iteration_list(list,batch=2): for i in xrange(0,len(list),batch): yield list[i:i+batch] for obj in iteration_list(): print obj #list[i:i+batch]ã表示ãããã
for-elseï¼forã§éä¸ã§åæ¢ããªãå ´åã®æ¡ä»¶ï¼
ç§ããããä½æãã¦ããæã«åãã¦ç¥ã£ãã®ã§ããã
forã«elseæãã¤ãããã¨ã§ãbreakãããªãã£ãæã®å¦çãè¨è¿°ãããã¨ãã§ãã¾ãã
for i in xrange(10): if i == 100: break else: print i #å®è¡ããã
ãªã¹ãå å 表è¨
ãªã¹ãã®å
é¨ã«foræãæ¸ããçãæ¸ããã¨ãã§ãã¾ãã
次ã«è¨è¼ããã®ã¯0~9ã¾ã§ã®æ°åãæã£ããªã¹ãã§ãã
print [i for i in xrange(10)]
ã¾ããæ¡ä»¶å¼ã使ãç¹å®ã®å¤ãåå¾ãããã¨ãã§ãã¾ãã
é常ã®ãªã¹ããæ§ç¯ããappendãå®æ½ããããé«éã«åä½ããã®ã§ãåç´ãªå¦çã§ããã°ãã¡ããè¨è¿°ãã¦ã¿ã¾ãããã
print [i for i in xrange(10) if i == 2]
Collections
Counter
import collections counter_list = ["a","b","c","d","e","a","a"] counter = collections.Counter(counter_list) print counter print counter["a"]
default dictï¼è¾æ¸ã®ããã©ã«ãå¤è¨å®ï¼
ä¸è¨ã§èª¬æããã¨è¾æ¸ã«åå¨ããªããã¼ã®åæå¤ã決ãããã¨ãã§ãã¾ãã
é常ã§ããã°ã以ä¸ã®è¨è¿°ããããã¨ã«ãªãã¨æãã¾ãã
dict = {} if "a" in dict: dict["a"] += 1 else: dict["a"] = 0
ããããcollectionsã使ããã¨ã§ç°¡åã«è¨è¿°ãã§ãã¾ãã
import collections dict = collections.defaultdict(int) dict["a"] += 1 print dict
Sortï¼ã½ã¼ãï¼
sorted
sortedã使ãã¨ãè²ã
ãªãªãã¸ã§ã¯ããã½ã¼ããããã¨ãã§ãã¾ãã
æé ãéé ã¯
list = [1,4,3,6,5,8] print sorted(list) print sorted(list,reverse=True)
åºå
[1, 3, 4, 5, 6, 8] [8, 6, 5, 4, 3, 1]
è¾æ¸ãã½ã¼ãããã³ã¼ãã¯ä»¥ä¸ã«ãªãã¾ãã
dict = {"a":1,"b":2,"c":1} print sorted(dict.items(),key=lambda x:x[1],reverse=True)
[('b', 2), ('a', 1), ('c', 1)]
å ã¿ã«ã½ã¼ãããããã¼ãkeyã«é¸æããã°è¯ãã®ã§ãåºæ¬çã«ã©ããª
ãªãã¸ã§ã¯ãã§ãã½ã¼ããããã¨ãã§ãã¾ãã
ifæã®æ¸ãæ¹
Pythonã§ifæã®æ¡ä»¶ã¯é£çµãã¦æ¸ããã¨ãã§ãã¾ãã
ã©ããããã¨ãã¨ããã¾ãã¨ã以ä¸ã®ä¾ãè¦ã¦ã¿ã¾ãã
ããã¯ãå¤æ°aã10ããé«ãããã¤ã100æªæºã®æã®ããã°ã©ã ã«ãããä¸è¬çãªè¨è¼æ¹æ³ã§ãã
a = 70 if a > 10 and a < 100: print "True" else: print "False"
ããããPythonã§ã¯ã以ä¸ã®ããã«æ¡ä»¶ãé£çµãã¦æ¸ããã¨ãã§ãã¾ãã
if 100 > a > 10: print "True" else: print "False"
Fileã«é¢ããå¦çï¼withï¼
fileã«é¢ããå¦çã¯é常以ä¸ã®ããã«æ¸ããèªåã®æã§ã¯ãã¼ãºããªããã°ãªãã¾ããã
f = open("test.txt") #ä½ããã®å¦ç f.close()
ããããwith statementã®å°å ¥ã«ãããèªåã§ã¯ãã¼ãºãããå¿ è¦ããªããªãã¾ãã
with open("test.txt") as f print f.read() #ä½ããã®å¦ç
inï¼ãªãã¸ã§ã¯ãã«åå¨ãã¦ãããã©ããï¼
æååããªã¹ãã«åå¨ãã¦ãããã©ããã調ã¹ãã«ã¯ã©ãããã°ããã§ãããï¼
èªåã§æ¸ãã«ã¯ã以ä¸ã®ããã«æ¸ããªããã°ãªãã¾ããã
searched_number = 4 for i in [1,2,3,4,5,6]: if i == searched_number: print "find!" break
ããããPythonã«ã¯inã¨å¼ã°ããã¹ãã¼ãã¡ã³ããããã以ä¸ã®ããã«è¨è¼ãããã¨ãã§ãã¾ãã
searched_number = 4 if searched_number in list: print "find!"
é¢æ°ã«ã¤ãã¦
è¿ãå¤ãè¤æ°è¿ãã
å®ã¯Pythonã¯è¿ãå¤ãè¤æ°è¿ããã¨ãã§ãã¾ãã
def return_function(a,b): return a,b
é¢æ°ã®ãªãã¸ã§ã¯ãå
é¢æ°ã¯ãªãã¸ã§ã¯ãã«ãããã¨ãã§ãã¾ãã
æ¡ä»¶åå²ã¨ãã§ã©ã®é¢æ°ãå®è¡ããããé¸æããå ´åã«ä¸»ã«å©ç¨ãã¦ãã¾ãã
def sum(a,b): return a + b def diff(a,b): return a - b func_obj = sum print func_obj(1,2)