åã ã¡ããRubyã¯ãããããªã¡ã½ããããã£ã¦æè»ã ããã ãã£ã¡ã«æ £ãã¡ããã¨Pythonæ¸ãã¨ãã¡ãã£ã¨å°ããã ãããã¨è¨ã£ã¦ãã¾ããã
ãããã«ãRubyã¯ãé åãããã·ã¥ã«å¯¾ãã¦ãããªæ©è½ãããã°ãããªãã¨æããã¨ãã ãã®ãã¼ã¿åã®ãªãã¡ã¬ã³ã¹ã調ã¹ãã°å¤§æµè¦ã¤ããã¾ãã
ããããPythonã§ããããããªä¾¿å©ãªé¢æ°ã使ã£ã¦åçã®æ©è½ãå®ç¾ãããã¨ãã§ãã¾ãã ãã ããRubyã«æ¯ã¹ã¦ãªãã¸ã§ã¯ãæåã£ã½ããªãã¨ãããããã ãªãã¸ã§ã¯ãããã¡ã½ãããå¼ã³åºãã®ã§ã¯ãªãé¢æ°ãã©ã ãå¼ãå©ç¨ããå¿ è¦ããã®ã§ã æåã¯å°ãæ¸æãããããã¾ããã
ããã§ããRubyã®ä¾¿å©ãªã¡ã½ããããPythonã®ã³ã¼ãã§èªç¶ã«æ¸ããªãããã ãããã¨ããè¨äºã ã¾ã¨ãã¦ã¿ããã¨ã«ãã¾ããã ã¨ãã£ã¦ãããªããªãã®ã§ãé åã¨ããã·ã¥ã®ã¡ã½ãããä¸å¿ã«ãã¾ãã
Rubyã¯2.2.2ãPythonã¯3.4.1ãå©ç¨ãã¾ãã ã¾ã便å®ä¸ããã®è¨äºã®ä¸ã§ã¯Pythonã®ããªã¹ããã¨ãè¾æ¸ããããããRubyã®å¯¾å¿ããåã§ãã ãé åããããã·ã¥ãã¨å¼ã¶ãã¨ã«ãã¾ãã
$ ruby -v ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14] $ python -V Python 3.4.3
ä½è«ã§ãããPythonã¯ãããã·ã¥ãã¼ãã«ãããè¾æ¸ãã¨å¼ã¶ãªã©ã ããã°ã©ã以å¤ã«ãç´æçã«åãããããååãã¤ããããããã§ãã ãªã¹ãã¨è¨ããã¦Lispã«ãããããªé£çµãªã¹ããæ³åããæ¹ãããããããã¾ãããã å®éã¯é åãªã®ã§ã注æãã ããã
æ¢ã«ç´ æ´ãããè¨äºãæ¸ãã¦ããæ¹ãããã£ãããã®ã§ãã¡ããã覧ãã ããã è»è¼ªã®åçºææã¯æãã¾ãããããã®è¨äºã§ã¯ããå°ãç´°ããã¨ããã¾ã§ã¾ã¨ããããã°ã¨æãã¾ãã
each
ããªãã©ã¢ã¤ãã«ãã¿ã¼ããã«ã«åæããããã°ã©ã ãæ¸ãããã¨ãã¾ãã
Rubyã§ãfor
ã®å¶å¾¡æ§æã¯ç¨æããã¦ãããã®ã®ã
å®éã®ããã°ã©ãã³ã°ã§ã¯each
ã¡ã½ãããå¼ã³åºãã»ãã好ã¾ãã¦ããããã§ãã
ãªãã¸ã§ã¯ãæåï¼ã£ã¦æãã§ããã
# Ruby idols = %w(laala mirei sophy) idols.each do |idol| puts idol end
ã¡ãªã¿ã«Rubyã®å ãã¿ã®ç´ç²ãªãã¸ã§ã¯ãæåè¨èªã®Smalltalkã§ã¯ããã«å¾¹åºãã¦ããã æ¡ä»¶åå²(if)ãçå½å¤ã®ã¡ã½ããã¨ãã¦å®ç¾©ããã¦ããã¨ãã 話ãèãããã¨ãããã¾ãã
ããã·ã¥ã®åè¦ç´ ãã«ã¼ãããããã¨ãã§ãã¾ãã
# Ruby idol_group_dict = { 'sorami smile' => %w(laala mirei sophy), 'dressing pafe' => %w(shion dorothy leona) } idol_group_dict.each do |group_name, members| members.each do |idol| puts "#{idol} is a member of #{group_name}" end end
Pythonã§ã¯for ~ in ~
ã®å¶å¾¡æ§æã使ã£ã¦ããæ¸ãã¾ãã
# Python idols = ['laala', 'mirei', 'sophy'] for idol in idols: print(idol)
ããã·ã¥ã®åè¦ç´ ãã«ã¼ããããã«ã¯ãè¾æ¸åã®.items()
ã¡ã½ãããå©ç¨ãã¾ãã
ãµã¤ãã«for
æã«æ¸¡ãã¨ããã·ã¥ã®ãã¼ããåãåºããªãã®ã§é¢é£ããããããã¾ããã
# Python idol_group_dict = { 'sorami smile': ['laala', 'mirei', 'sophy'], 'dressing pafe': ['shion', 'dorothy', 'leona'] } for group_name, members in idol_group_dict.items(): for idol in members: print('{} is a member of {}'.format(idol, group_name))
ã¡ãªã¿ã«Python3.6ããã¯f'{idol} is member of {group_name}')
ã¨ããå½¢ã§ã
æååãªãã©ã«ãã§ããããã«ãªãããã§ãã
Rubyã«æ¯ã¹ã¦åé·ã ã£ãã®ã§å¬ããã§ãã
each_with_index
ã¤ã³ããã¯ã¹ãå«ãã¦ã«ã¼ãããããå ´åãRubyã§ã¯each_with_index
ã使ãã¨ä¾¿å©ã§ãã
# Ruby school_rules = %w(çå¾ã¯èªåã«èªä¿¡ãæããªããã°ãªããªã) school_rules.each_with_index do |rule, i| puts "ãããªã«å¦åæ ¡å第#{i+1}æ¡ï¼ #{rule}ï¼" end
ã¤ã³ããã¯ã¹ã0以å¤ããå§ãããå ´åã¯ãeach.with_index
ã使ãã¾ãã
ï¼each
ã¡ã½ããã§Enumeratoråã«å¤æããå¾ãwith_index
ã¡ã½ãããå¼ã³åºãã¦ãã¾ããï¼
ã¡ãªã¿ã«each
ã ãã§ãªãmap
çã®ä»ã®ã¡ã½ããã¨ãçµã¿åããããã¾ãã
# Ruby school_rules = %w(çå¾ã¯èªåã«èªä¿¡ãæããªããã°ãªããªã) school_rules.each.with_index(1) do |rule, i| puts "ãããªã«å¦åæ ¡å第#{i}æ¡ï¼ #{rule}ï¼" end
Pythonã§ã¯enumerate
ã¨ããé¢æ°ãå©ç¨ãã¾ãã
# Python school_rules = ['çå¾ã¯èªåã«èªä¿¡ãæããªããã°ãªããªã'] for i, rule in enumerate(school_rules): print("ãããªã«å¦åæ ¡å第{}æ¡ï¼ {}ï¼".format(i+1, rule))
ã¤ã³ããã¯ã¹ã1ããå§ãããå ´åã¯ãenumerate
ã®å¼æ°ã«æå®ãã¦ãããã ãã§ãã
# Python school_rules = ['çå¾ã¯èªåã«èªä¿¡ãæããªããã°ãªããªã'] for i, rule in enumerate(school_rules, 1): print("ãããªã«å¦åæ ¡å第{}æ¡ï¼ {}ï¼".format(i, rule))
each_cons
åã©ãã®ããã«ããããããã¨ãã£ãããã®ã¨ã³ãã£ã³ã°ãã¼ããåºåããããç¶ããããã°ã©ãã¼ãå¤ããã¨ã§ãããã ï¼ä»ã§ããã®æãªãã§ããããï¼ï¼
ãããªã¨ããeach_cons
ã使ãã¨èªç¶ã«æ¸ãã¾ãã
# Ruby song_with_mam = %w(ã¯ã¸ã© ã©ãã³ ã³ã¢ã© ã©ã¤ãªã³ 女ã®å ç·ã®å) song_with_mam.each_cons(2) do |x, y| puts "#{x} ã«ãªããã #{y}âª" end # => ã¯ã¸ã©ã«ãªãããã©ãã³âª # => ã©ãã³ã«ãªãããã³ã¢ã©âª # ...
ãã®ãããªå ´åã«ã¯Pythonã§ã¯ã©ãæ¸ãã°èªç¶ãªã®ãããã¨æãã¤ãã¾ããâ¦ãzip
é¢æ°ã使ã£ã¦
# Python song_with_mam = ['ã¯ã¸ã©', 'ã©ãã³', 'ã³ã¢ã©', 'ã©ã¤ãªã³', '女ã®å', 'ç·ã®å'] for x, y in zip(song_with_mam, song_with_mam[1:]): print('{}ã«ãªããã{}âª'.format(x, y))
ã¨ã2ã¤ã«ã¼ããåãã¦
# Python song_with_mam = ['ã¯ã¸ã©', 'ã©ãã³', 'ã³ã¢ã©', 'ã©ã¤ãªã³', '女ã®å', 'ç·ã®å'] for i, x in enumerate(song_with_mam): for j, y in enumerate(song_with_mam): if j - i == 1: print('{}ã«ãªããã{}âª'.format(x, y))
ã¨ããä¸ã¤åã®å¤ãæ®ãã¦
# Python song_with_mam = ['ã¯ã¸ã©', 'ã©ãã³', 'ã³ã¢ã©', 'ã©ã¤ãªã³', '女ã®å', 'ç·ã®å'] prev = None for x in song_with_mam: if x is not None: print('{}ã«ãªããã{}âª'.format(prev, x)) prev = x
ã¨ãã«ãªããã§ããããï¼
ã¨æã£ãã®ã§ãããPythonã®ã¤ãã¬ã¼ã¿ãæ±ãæ¨æºã©ã¤ãã©ãªã§ããitertools
ã使ãã®ãèªç¶ããããã¾ããã
Itertools ã¬ã·ãã®ãµã³ãã«ã³ã¼ãã使ãã¨ãã®ããã«ãªãã¾ãã
# Python from itertools import tee def pairwise(iterable): a, b = tee(iterable) next(b, None) return zip(a, b) song_with_mam = ['ã¯ã¸ã©', 'ã©ãã³', 'ã³ã¢ã©', 'ã©ã¤ãªã³', '女ã®å', 'ç·ã®å'] for x, y in pairwise(song_with_mam): print('{}ã«ãªããã{}âª'.format(x, y))
è¦ç´ ã3ã¤ä»¥ä¸ã®å ´åã®ããã«æ¡å¼µãã§ãããã§ãããã ãè¤æ°åãã¤è¦ç´ ãåãåºãã ããªã®ã«ã大ãããªã³ã¼ãã«ãªã£ã¦ãã¾ã£ãæã¯ããã¾ãã
ã¨ããã§ãã¡ã½ããåã®each_cons
ã®cons
ã£ã¦Lispã®cons
)ãç±æ¥ãªãããããï¼
each_slice
æ¢ã«ç´ æ´ãããè¨äºãæ¸ãã¦ããæ¹ããã¾ããã
http://blog.livedoor.jp/dankogai/archives/51838970.html
ãã®ã¨ã³ããªãè¦ã¦ãPythonã§Rubyã®each_sliceãæ¸ãã¨ãããã©ããªãã ããã¨æã£ã¦ãã£ã¦ã¿ãã
ãã¡ããitertools
ã使ãã®ãèªç¶ã®ããã§ããã
Itertools ã¬ã·ãã«ããã³ã¼ãããã¡ãã§ãã
ï¼ä¸ã®è¨äºã§ã¯Python2ç³»ã®æ代ãªã®ã§izip_longest
ã¨ããé¢æ°åã«ãªã£ã¦ãã¾ããï¼
# Python from itertools import zip_longest def grouper(iterable, n, fillvalue=None): "Collect data into fixed-length chunks or blocks" # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx" args = [iter(iterable)] * n return zip_longest(*args, fillvalue=fillvalue)
length (size)
é åãããã·ã¥ãããã¦æååçã®é·ãã調ã¹ãã¡ã½ããã§ãã
# Ruby triangle = %(kanon pinon junon) puts "Triangle is a group of #{triangle.length} idols."
Pythonã§ã¯ãã¡ã½ããã§ã¯ãªãlen
ã¨ããé¢æ°ã使ãã¾ããPythonã§ã¯ããç¨åº¦ä¸è¬çãªæä½
ï¼Rubyã§ã¯Enumerable
ã®ã¡ã½ããã«ãããããªãã®ï¼ã¯
é¢æ°ãæ§æã¨ãã¦ç¨æããã¦ãããã¨ãå¤ãã§ãã
triangle = ['kanon', 'pinon', 'junon'] print('Triangle is a group of {} idols.'.format(len(triangle)))
sort, sort_by, sort!, sort_by!
é
åã®ä¸èº«ãã½ã¼ãããå ´åã
Rubyã§ã¯sort
ãå¼ã³åºãããsort_by
ã®ãããã¯ã«ã½ã¼ãåºæºã渡ããã¨ãå¤ãã¨æãã¾ãã
ï¼å人çã«ãsort
ã¡ã½ããã§ãããã¯ãå¼ã³åºãã¨ããå®å®è¹æ¼ç®å(<=>
)ãå®ç¾©ãããã
æ»ãå¤ã«-1 or 0 or 1ãè¿ããããã¯ãä½ãå¿
è¦ããã£ã¦ãé ã®ä¸ããã¡ããã¡ããã¦ãã¾ããã¨ãå¤ãã§ãâ¦ãï¼
# Ruby p [3, 0, 1].sort # => [0, 1, 3] # ååã®é·ãã§ã½ã¼ããã names = %w(laala fuwari dore shio) p names.sort_by { |n| n.size } # => ["shio", "dore", "laala", "fuwari"] p names.sort_by(&:size) # ç°¡ç¥åããæ¸ãæ¹
ä½è«ã§ãããsort_by
ãsort
ã¡ã½ããã¯å®å®ã½ã¼ãã§ãªãï¼æ¯è¼çµæãåãè¦ç´ ãå
ã®é åºéãã«ä¸¦ã°ãªããã¨ãããï¼
ãããªã®ã§æ³¨æãå¿
è¦ã§ãã
ã¾ããæ¢ã«ãåãã ã¨æãã¾ãããsort!
ãsort_by!
ãªãã¸ã§ã¯ãèªèº«ãå¤åãããç ´å£çã¡ã½ããã§ãã
ã³ãã¼ãä½ããªããããç ´å£çã¡ã½ããã®ã»ããããã©ã¼ãã³ã¹ãè¯ãå ´åãå¤ãããã§ãã
names = %w(laala fuwari dore shio) names.sort_by! { |n| n.size } p names # => ["shio", "dore", "laala", "fuwari"]
Pythonã§ã¯éç ´å£æä½ã¯sorted
é¢æ°ãããç ´å£çæä½ã¯é
åï¼ãªã¹ãåï¼ã®.sort
ã¡ã½ããã使ãã¾ãã
ã¡ãªã¿ã«ããããã®ã½ã¼ãã¯å®å®ã½ã¼ãã ããã§ãã
# Python print(sorted([3, 0, 1])) # => [0, 1, 3] # ååã®é·ãã§ã½ã¼ããã names = ['laala', 'fuwari', 'dore', 'shio'] print(sorted(names, key=len)) # => ["shio", "dore", "laala", "fuwari"]
ç ´å£çæä½ã®å ´åã®ã³ã¼ãã§ãã
# Python names = ['laala', 'fuwari', 'dore', 'shio'] names.sort(key=len) print(names) # => ["shio", "dore", "laala", "fuwari"]
ã¨ããã§ãRubyã®ãããã¯ã¨ããå¼·åãªæ©è½ã使ããªã代ããã«ã Pythonã§ã¯é«éé¢æ°ã§ï¼æ¯è¼åºæºã¨ãªãé¢æ°ãå¼æ°ã«ä¸ãã¦ï¼æ©è½ãå®ç¾ãã¦ãããã¨ãå¤ãã§ãã
ãã¾ã«Pythonã¯ãé
å.size
ãããªãã¦len(é
å)
ãªã®ã¯ãªãã¸ã§ã¯ãæåè¨èªã¨ãã¦ä¸è²«æ§ããªãããï¼ã
ã¨ããã³ã¾ãã¦ãã¾ãããã¡ã½ããã¨ãã¦å®ç¾©ãã¦ãã¾ãã¨names.sort(key=lambda x: x.size)
ã®ãããªåé·ãªæ¸ãæ¹ã«ãªã£ã¦ãã¾ãããã
ã¨ããçç±ãããããã«æãã¾ãã
ã¾ããRubyã®sort
ã¡ã½ããã«ãããã¯ã渡ããã¨ãã®ããã«ã2ã¤ãã¤ã®è¦ç´ ãåãåºãã¦æ¯è¼ãããå ´åãããããããã¾ããã
Python2.7ã§ã¯ãsorted
é¢æ°/.sort
ã¡ã½ããã«cmp
ã¨ããå¼æ°ãããã
çµã¿è¾¼ã¿é¢æ°ã«å®å®è¹æ¼ç®å(<=>
)ã¨åãæåã®cmp
é¢æ°ãç¨æããã¦ããã®ã§ããã
Python3.5ã§ã¯åé¤ããã¦ãã¾ãã
Python3.5ã§2.7ã®cmp
ã®ãããªæåï¼Rubyã®sort
ã¡ã½ãã+ãããã¯ã®ãããªæåï¼ã欲ããå ´åã
å
¬å¼ããã¥ã¡ã³ãã«ããã¨ã
æ¨æºã©ã¤ãã©ãªã®functools
ã®ä¸ã«cmp_to_key
ããããããã使ã£ã¦å¤æããé¢æ°ãkey
å¼æ°ã«æ¸¡ãã°ããããã§ãã
æ§å¼ã® cmp é¢æ°ã key é¢æ°ã«å¤æããã«ã¯ functools.cmp_to_key() ã使ç¨ãã¦ãã ããã
min, min_by, max, max_by
ãããsort
ã®å ´åã¨ä¼¼ã¦ãã¾ãã
# Ruby # ä¸çªå¤§ããªæ°åãæ¢ã tension = [1, 2, 3, 4, 5] p tension.max # => 5 idols = [ {name: 'mirei', gobi: 'puri'}, {name: 'ajimi', gobi: 'davinci'}, {name: 'cosmo', gobi: 'cosmic'} ] # èªå°¾ãä¸çªé·ããã£ã©ã¯ã¿ã¼ãæ¢ã p idols.max_by { |x| x[:gobi].size } # => {:name=>"ajimi", :gobi=>"davinci"}
Pythonã®å ´åãmax
ã®å¼æ°key
ã«é¢æ°ã渡ããã¨ã¨Rubyã®mix_by
ã®ãããªæåã«ãªãã¾ãã
# Python # ä¸çªå¤§ããªæ°åãæ¢ã tension = [1, 2, 3, 4, 5] print(max(tension)) idols = [ {'name': 'mirei', 'gobi': 'puri'}, {'name': 'ajimi', 'gobi': 'davinci'}, {'name': 'cosmo', 'gobi': 'cosmic'} ] # èªå°¾ãä¸çªé·ããã£ã©ã¯ã¿ã¼ãæ¢ã print(max(idols, key=lambda x: len(x['gobi']))) # => {'name': 'ajimi', 'gobi': 'davinci'}
reverse, reverse!
ãããã½ã¼ãã®å ´åã¨ããä¼¼ã¦ãããreversed
é¢æ°ã.reverse
ã¡ã½ãããå©ç¨ãã¾ãã
ãã ããreversed
é¢æ°ã®æ»ãå¤ã¯é
å(list
)ã§ã¯ãªãlistreverseiterator
ã¨ããã¤ãã¬ã¼ã¿ã¼ãªãã¸ã§ã¯ããè¿ã£ã¦ããã®ã§æ³¨æãã¾ãããã
# Python print(reversed([1, 2, 3])) # => <list_reverseiterator object at 0x103c03320> # liståã«å¤æãã¾ããã print(list(reversed([1, 2, 3])))
å ¬å¼ããã¥ã¡ã³ã(http://docs.python.jp/3.5/library/functions.html#reversed)ã«ã¯ä»¥ä¸ã®ãããªè¨è¿°ãããã¾ãã
è¦ç´ ãéé ã«åãåºãã¤ãã¬ã¼ã¿ (reverse iterator) ãè¿ãã¾ãã seq 㯠__reverse__() ã¡ã½ãããæã¤ããã·ã¼ã±ã³ã¹åãããã³ã« (__len__() ã¡ã½ãããããã³ã 0 以ä¸ã®æ´æ°ãå¼æ°ã¨ãã __getitem__() ã¡ã½ãã) ããµãã¼ããããªãã¸ã§ã¯ãã§ãªããã°ãªãã¾ããã
Pythonã¯ãRubyã«æ¯ã¹ã¦ã«ã¸ã¥ã¢ã«ã«é 延è©ä¾¡ãè¡ãè¨èªã ããããããã¾ããã
# Python # long_listã¯è¶ é·ãé å for x in reversed(long_list): function(x)
ãã®ãããªå ´åãreversed
é¢æ°ãé
åãè¿ãå®è£
ã ã£ãå ´åãreversed(long_list)
ã§ä¸åº¦é
åå
¨é¨ãã«ã¼ããã¦æä½ããå¾ã«
ããä¸åº¦for
æã®ã«ã¼ããåãã¾ãã
é
延è©ä¾¡ãããªãã¸ã§ã¯ããè¿ããã¨ã§ãã«ã¼ããä¸åº¦ã§æ¸ãããã«ãªã£ã¦ãã¾ãã
æ£ç´ãreverse
ã§ã¯ãããªã«é
延è©ä¾¡ã®ã¡ãªãããæãããã¾ããããmap
ãfilter
ã§ã¯å½¹ç«ã¡ã¾ãã
inject (reduce), each_with_object, join
Pythonã§ãreduce
ã¨ããç³ã¿è¾¼ã¿ã®é¢æ°ãç¨æããã¦ããã®ã§ããã
Effective Rubyã®ãé
ç®19 reduceã使ã£ã¦ã³ã¬ã¯ã·ã§ã³ãç³ã¿è¾¼ãæ¹æ³ã身ã«ä»ãããããèªãã§
ãã®ä¾¿å©ãã«é©ãããããã¨ãããã¾ãã
- ä½è : Peter J.Jones
- åºç社/ã¡ã¼ã«ã¼: ç¿æ³³ç¤¾
- çºå£²æ¥: 2015/01/19
- ã¡ãã£ã¢: Kindleç
- ãã®ååãå«ãããã° (4件) ãè¦ã
ä¾ãã°ãï¼ä»¥åã®è¨äºã®ãã®ã¾ã¾ã®ä¾ãªã®ã§ããï¼URLã®é åï¼ãªã¹ãï¼ãã{URL => ãã¼ã¸ã¿ã¤ãã«}ã¨ããããã·ã¥ãã¼ãã«ï¼è¾æ¸ï¼ã«å¤æãããã¨ãã¾ãã åæã«ããã¼ã¸ã®URLã«googleãå«ã¾ãã¦ãããã®ãé¤å¤ãããã¨ãã¾ãã
ãã®ãããªã¨ããinject
ã®åæå¤ãããã·ã¥ã«ãããã¨ã§ãããã·ã¥ã®ä¸ã«ç³ã¿è¾¼ãã§ãããã¨ãã§ãã¾ãã
# Ruby # ã«ã¼ãããçµæãããã·ã¥(ããã©ã«ãå¤ã§ã¯{})ã®ãã¼ã«ä»£å ¥ãã¦ãã def create_url_table(urls) urls.inject({}) do |hash, url| hash[url] = get_title(url) unless url =~ %r{google} hash end end
ããããç§ã¯ä»ã§ã¯each_with_object
ãå©ç¨ãã¦ã¾ãã
# Ruby def create_url_table(urls) urls.each_with_object({}) do |url, hash| hash[url] = get_title(url) unless url =~ %r{google} end end
Pythonã§ã¯åæ§ã®å¦çãè¾æ¸å å 表è¨ã使ã£ã¦æ¸ããã¨ãã§ãã¾ãã
# Python def create_url_table(urls): return {url: get_title(url) for url in urls if 'google' not in url}
ããå°ãæ¡ä»¶ãè¤éã«ãªã£ã¦ããã¨ãã¸ã§ãã¬ã¼ã¿ã¼ã«åãåºãããã«ãã¦ãã¾ãã ãã³ã¬ã¯ã·ã§ã³ãç³ã¿è¾¼ãæ¹æ³ã身ã«ä»ããããã§ç´¹ä»ããã¦ãããããªä¾ã ã¨ã å å 表è¨ãã¸ã§ãã¬ã¼ã¿ã¼ã使ããã¨ãå¤ãã§ãã
# Python def create_url_table(urls): return dict(_url_generator(urls)) def _url_generator(urls): for url in urls: if 'google' not in url: yield (url, get_title(url))
Rubyã§ã¯é
åã®ä¸èº«ã®æ°å¤ã®åè¨å¤ãè¨ç®ããéã«ãinject
ã使ããããinject
ãç»å ´ããæ©ä¼ã¯å¤ãã§ãã
# Ruby [1, 2, 3].inject(:+) # Ruby2.4ãã.sumãå©ç¨ã§ããããã«ãªãããã§ã [1, 2, 3].sum
ä¸æ¹ã§ãPythonã¯çµã¿è¾¼ã¿é¢æ°ã§ããsum
ãå©ç¨ãã¾ãã
ãã®ããããPython3.*ã«ãªã£ãéã«reduce
ã¯çµã¿è¾¼ã¿é¢æ°ã§ãªããªã£ã¦ãã¾ãã
functools
ããå¼ã³åºãå¿
è¦ãããã¾ãã
# Python sum([1, 2, 3])
åãããã«ãä¸ã¤ã®æååã«çµåããéã«ã¯ãRubyã§ã¯é
åããæååçµåã®ã¡ã½ãã.join
ãå¼ã³åºãã¾ããã
# Ruby puts %(laala mirei sophy).join(', ') # => "laala, mirei, sophy"
Pythonã§ã¯ãæåååã®.join
ã¡ã½ããã«é
åã渡ãããã«ãªã£ã¦ãã¾ãã
ãããlen
é¢æ°ã¨åããããªãã¸ã§ã¯ãæåè¨èªã®ããã°ã©ãã¼ãå«ãããã¨ã®ä¸ã¤ã§ãã
# Python print(', '.join(['laala', 'mirei', 'sophy'])) # => "laala, mirei, sophy"
ä½è«ã§ãããRubyã«inject
ã¨reduce
ã®å¥åã®ã¨ã¤ãªã¢ã¹ãç¨æããã¦ãããã¨ã«ã¤ãã¦ã¯ã
Rubyist Magazine - map と collect、reduce と inject ―― 名前の違いに見る発想の違いã®å
容ãé¢ç½ãã£ãã®ã§ãã²èªãã§ãã ããã
map (collect), select, reject
é
åå
¨ä½ã«é¢æ°ãã¡ã½ãããé©ç¨ããmap
ã¨ã
æ¡ä»¶ã«å½ã¦ã¯ã¾ãè¦ç´ ã ããæ®ãselect
ï¼ã¨çå½å¤ãéã®ãã¼ã¸ã§ã³ã®reject
ï¼ã§ãã
ãRubyã®é
åæä½ãPythonã§æ¸ãã¨ï¼ãã®è¨äºã«ããéãã§ãã
ãã ããPython3.*ã§ã¯map
ãfilter
ãé
延è©ä¾¡ãããããã«å¤æ´ããã¦ãã¾ãã
詳ããã¯ä»¥ä¸ã®è¨äºãèªãã§ãã ããã
map
ãfilter
ãªã©ã®å
¬éé¢æ°ã¨ãã¸ã§ãã¬ã¼ã¿ã¼å
å
表è¨ã§æ©è½ã被ã£ã¦ãã¾ãã
ã©ã¡ãã使ã£ã¦ãããã®ã§ãããç§ã¯å
å
表è¨ã®ã»ãããã使ãã¾ãã
é
åã®è¦ç´ ã®å¤æã¨ãã£ã«ã¿ãªã³ã°ãåæã«è¡ãããã
æ¬å¼§ã«ãã£ã¦æ»ãå¤ããªã¹ããã¸ã§ãã¬ã¼ã¿ã¼ãéå(set
)ãé¸ã¹ãããã§ãã
Rubyã«ã¯ç ´å£çæä½çã®map!
ãselect!
ãããã¾ãã
ããã¯é¢æ°ã®ä¸ã§ã¡ã½ãããã§ã¼ã³ããéã«ä½¿ã£ã¦ãã¾ãã
ãã以å¤ã§ä½¿ã£ããã¨ã¯ç§ã¯ã»ã¨ãã©ç¡ãã§ãã
# Ruby # ï¼ããã¾ãä¾ã¯è¯ããªãã§ããï¼ã¡ã½ãããã§ã¼ã³ã§æ¸ã代ããã« def count_group_mamber(idols, group_name) idols.filter { |x| x[:group_name] == group_name }. map { |x| x[:name] }. size end # é¢æ°ã®ä¸ã§ã¯ç ´å£çå¤æ´ã®ã¡ã½ããã§æ¸ãã¦ãã¾ã def count_group_mamber(idols, group_name) arr = idols.filter { |x| x[:group_name] == group_name } arr.map! { |x| x[:name] } arr.size end
Rubyã§ãPythonã¨åæ§ã«é
延è©ä¾¡ãè¡ããããªãã.lazy.map
ã®ããã«æ¸ãã¾ãã
詳ããã¯ã怠惰で短気で傲慢な君に贈るRubyの遅延評価ããèªã¿ã¾ãããã
empty?
Rubyã«ã¯ãé
åã空ãã©ãããå¤å®ãã.empty?
ã¨ããã¡ã½ãããããã¾ãã
ããã«ããæå¿«ãªã³ã¼ããæ¸ããããã«ãªãã¾ãã
ä¾ãã°ãååã®é
å(names
)ãåç°¿ã管çãã¦ããDBã«åãåããããããªå ´åã
ããnames
ã空ãªãDBã«åãåãããã¾ã§ããªã空ã®é
åãè¿ãã°ããã¨ãã¾ãã
# Ruby def search_ids(names) return [] if names.empty? # åç°¿DBã«ååããå¦ç # ... ids end
Pythonã§ã¯.empty?
ã®ãããªã¡ã½ããã¯ç¡ãããã空ã®é
åãå½ã§ãããã¨ãå©ç¨ãã¦ä»¥ä¸ã®ããã«æ¸ãã¨æãã¾ãã
def search_ids(names): if not names: # ããã㯠len(names) == 0 return [] # åç°¿DBã«ååããå¦ç # ... return ids
Pythonã¯ãããã¾ãç¹æ®ãªé¢æ°ãæ§æã追å ããã¨ããããããªããªãããæå°éã«ã¨ã©ããããã¨ãã èãæ¹ãå¼·ãããã«æãã¾ãã
ä¸æ¹ã§ãRubyã§ã¯åãã¡ã½ããã®å¥åã®ã¨ã¤ãªã¢ã¹ãç¨æããããif
ã¨unless
ãç¨æããã¦ãããã
æèã«ãã£ã¦ãã¡ãã¨ä½¿ãåããããã°ãã³ã¼ãã®æå³ãæå¿«ãªããã°ã©ãã³ã°ãè¡ããããã§ãã
include?
# Ruby solami_smile = %w(laala mirei sophy) puts solami_smile.include? 'sophy' # => True puts solami_smile.include? 'dorothy' # => False
Pythonã§ã¯in
ã¨ããæ¼ç®åãå©ç¨ãã¾ãã
# Python solami_smile = ['laala', 'mirei', 'sophy'] print('sophy' in solami_smile) # => true print('dorothy' in solami_smile) # => false
ã¾ããin
æ¼ç®åã¯ãããæåãæååã®ä¸ã«å«ã¾ãããããããã·ã¥ã®ãã¼ãåå¨ããã(Rubyã®.hash_key?
ã¡ã½ããã®ç¨é)ã«ã使ããã¾ãã
(Python2.*ã§ã¯è¾æ¸åã«has_key
ã¡ã½ãããåå¨ããã®ã§ããã
3ç³»ã§ã¯in
ã«çµ±ä¸ããã¾ããã)
# Python print('la' in 'laala') # => true print('do' in 'laala') # => False idol_group_dict = { 'sorami smile': ['laala', 'mirei', 'sophy'], 'dressing pafe': ['shion', 'dorothy', 'leona'] } print('sorami_smile' in idol_group_dict) # => true
any?, all?
é
åã®ä¸ã«ãããªãã®ããããã©ãããå
¨é¨ãããªã®ãã調ã¹ãã¨ããany?
ãall?
ã®ã¡ã½ããã¯ä¾¿å©ã§ãã
ä¾ãã°ããªãã©ã®åãã¼ã ã®ã¡ã³ãã¼ã®ä¸ã«ããããããã©ãã確èªãããã¨ãã以ä¸ã®ãããªã³ã¼ããæ¸ãã¨æãã¾ãã ï¼éåæ¼ç®ã使ãæ¹æ³ãããã¨æãã¾ããï¼
# Ruby solami_smile = %w(laala mirei sophy) solami_dressing = %w(laala mirei sophy shion dorothy) gaarmageddon = %(aroma mikan gaaruru) boys = %(meganii leona) def cute?(pripara_idol) true end # ã½ã©ãã¹ãã¤ã«ã®ä¸ã«ã¬ã¡ã«ãã²ãã³ã®ã¡ã³ãã¼ã1人ã§ãããã p solami_smile.any? { |x| gaarmageddon.include?(x) } # => false # ã½ã©ããã¬ãã·ã³ã°ã«ç·åããããã©ãã p sorami_dressing.any? { |x| boys.include?(x) } # => true # ã½ã©ãã¹ãã¤ã«ã«ã½ã©ãã¹ãã¤ã«ã®ã¡ã³ãã¼ãå ¨å¡å«ã¾ããã p solami_smile.all? { |x| solami_dressing.include?(x) } # => true # ã½ã©ãã¹ãã¤ã«ãå ¨å¡ã«ã¯ã¤ã¤ãã©ãã p solami_smile.all? { |x| cute?(x) } # => true
Rubyã®any?
ã¡ã½ããã§ã¯true
ã®å¤ãè¦ã¤ãã£ãæç¹ã§ï¼all?
ã¯false
ãè¦ã¤ãã£ãæç¹ã§ï¼ã
ãã以ä¸ã®ã«ã¼ããæã¡åãã¾ãã
Pythonã§ã¯any
ãall
ã¯é¢æ°ã¨ãã¦ç¨æããã¦ãã¾ãã
çå½å¤ã®ä¸è¦§ãè¿ãå
å
表è¨ã¨çµã¿åããã¦ä½¿ããã¨ãå¤ãã§ãã
ï¼ããã§ã¸ã§ãã¬ã¼ã¿ã¼ã使ããã¨ã§ãRubyã¨åæ§ã«any
é¢æ°ã§è©ä¾¡ãã¦ããä¸ã§True
ãè¦ã¤ãã£ãæç¹ã§ã«ã¼ããæã¡åããã¨ãã§ãã¾ãï¼
# Python def is_cute(pripara_idol): return True # ã½ã©ãã¹ãã¤ã«ã®ä¸ã«ã¬ã¡ã«ãã²ãã³ã®ã¡ã³ãã¼ã1人ã§ãããã print(all((x in gaarmageddon) for x in solami_smile)) # => False # ã½ã©ãã¹ãã¤ã«ãå ¨å¡ã«ã¯ã¤ã¤ãã©ãã print(any(is_cute(x) for x in solami_smile)) # => True
Rubyã«ã¯ä»ã«ãone?
ãnone?
ãªã©ã®ã¡ã½ãããããã¾ãã
uniq, uniq!
å²ã¨Pythonãæ±ãéã«é¬¼éãªã®ãå¤ãä¸æã«ããã¡ã½ããã§ãã
é çªãæ°ã«ããªãã¦ãã & é
åã®ä¸ã®å¤æ°ãã¤ãã¥ã¼ã¿ãã«ãªããä¸åº¦éåå(set
)ã«å¤æãã¦ãã¾ãã¾ãããã
ãã ããå¤æ°ã®é çªãä¿æãããå¿
è¦ãããã¾ãã
â» Python3.6ããRubyã¨åããããã·ã¥ã®é çªãä¿æãããããã«ãªããããã®ã§ã éååã§ãé çªãä¿æãããããã«ãªãããããã¾ããï¼åãªãæå¾ ã§ããï¼ã
# Python nums = [1, 2, 3, 4, 2, 3, 4] nums_set = set(nums) print(nums_set) # => {1, 2, 3, 4} # é åã«æ»ã print(list(nums_set)) # => [1, 2, 3, 4]
é çªãä¿æãããå ´åã¯ããPython Tipsï¼ãªã¹ãããéè¤ããè¦ç´ ãåé¤ããããã§åæ§ã®è°è«ãããã¦ãã¾ãã ã¾ããItertools ã¬ã·ãã®ä¸ã«ããããå°ãé£ããå ´åã®ãµã³ãã«ãããã¾ãã
# Python def f7(seq): seen = set() seen_add = seen.add return [ x for x in seq if x not in seen and not seen_add(x)] print(f7([1, 2, 3, 4, 2, 3, 4]))
ã¾ããsorted
é¢æ°ã§ã¤ã³ããã¯ã¹ãåºã«ã½ã¼ãããã¨ããæãããã¾ãã
# Python nums = [1, 2, 3, 4, 2, 3, 4] nums = sorted(set(nums), key=nums.index) # => [1, 2, 3, 4]
ãã ãããã¥ã¼ã¿ãã«ãªãªãã¸ã§ã¯ãï¼ä¾ãã°é
åãããã·ã¥ï¼ã¯ãset
ã®è¦ç´ ã«å«ãããã¨ãã§ãã¾ããã
ãã®ãããã¦ãã¼ã¯ã«ããéã«ã¨ã©ã¼ãåºã¦ãã¾ãã¾ãã
# Python idol_groups = [['laala', 'mirei', 'sophy'], ['shion', 'dorothy', 'leona'], ['laala', 'mirei', 'sophy']] set(idol_groups) # â»ä»¥ä¸ã®ãããªã¨ã©ã¼ãåºã¾ã # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # TypeError: unhashable type: 'list'
ãã®ãããªå ´åã«ã©ãããã°ããã®ã調ã¹ãã¨ããã ãPythonã®ãªã¹ãå å 表è¨ã§Rubyã®uniqã¡ã½ããã¨åãäºãããã ã¨ããè¨äºã®ä¸ã«ããä¾ãã¾ãèªããä¾ã ã¨æãã¾ããã
# Python idol_groups = [['laala', 'mirei', 'sophy'], ['shion', 'dorothy', 'leona'], ['laala', 'mirei', 'sophy']] print([x for i, x in enumerate(idol_groups) if i == idol_groups.index(x)]) # => [['laala', 'mirei', 'sophy'], ['shion', 'dorothy', 'leona']]
ã³ã¼ãã®ä¸ã«é »ç¹ã«ä½¿ããªãã
çµã¿è¾¼ã¿ã®list
åãç¶æ¿ãã¦UniqueList
ã¿ãããªååã§å°ç¨ã®ã¯ã©ã¹ãä½ã£ã¦ãã¾ã£ãã»ããããã®ããããã¾ããã
freeze
é åãããã·ã¥ã«éãã¾ãããããªãã¸ã§ã¯ãã«ç ´å£çå¤æ´ãå ããããªãããã«å¶éããã¡ã½ããã§ãã
Pythonã§ã¯ã¤ãã¥ã¼ã¿ãã«ãªé åã¨ãã¦ã¿ãã«ãã ã¤ãã¥ã¼ã¿ãã«ãªéååã¨ãã¦frozensetãããã¾ãã
å
ç¨uniq
ã®ã¨ããã§è¿°ã¹ãéããããã·ã¥ã®ãã¼ãéååã«ãã¥ã¼ã¿ãã«ãªãªãã¸ã§ã¯ãã§ããé
åãæå®ã§ãã¾ããã
ãã®ããããã®ãããªå ´åã«é
åãæå®ããå¿
è¦ãããã¨ãã«ã¿ãã«ãå©ç¨ãã¾ãã
ãããï¼ãããfrozendictã¯ç¡ãã®ï¼ãã¨æã£ããä¸åº¦ææ¡ããã¦ãªã¸ã§ã¯ãããã¦ããããã§ãã
ããã·ã¥ã®ãã¼ã«ããã·ã¥ãæå®ãããå ´åï¼ãããããï¼ã¯ã.items()
ã¡ã½ããã§å¤æãããã®ãã¿ãã«ã«ããã¨è¯ãã¨æãã¾ãã
# Python profile = {'name': 'mirei', 'gobi': 'puri'} tuple(profile.items()) # => (('name', 'mirei'), ('gobi', 'puri'))
slice
é åã®ä¸é¨ãåå¾ããã¡ã½ããã§ããPythonã«ããã¹ã©ã¤ã¹ã®ããã®å°ç¨ã®è¡¨è¨ãããã¾ãã
transpose
è¡åï¼ï¼æ¬¡å é åï¼ã®è»¢ç½®ãè¡ãå ´åãRubyã«ã¯å°ç¨ã®ã¡ã½ãããç¨æããã¦ãã¾ãã Pythonã§ã¯
# Python matrix = [[1, 2, 3], [4, 5, 6]] print(list(zip(*martix))) # => [(1, 4), (2, 5), (3, 6)]
ãã ãã転置è¡åãå¿
è¦ãªã¬ãã«ã®æ°å¤è¨ç®ãè¡ãå ´åãnumpy
ãå©ç¨ãããã¨ãå¤ãã¨æãã¾ãã
ãã¡ãã«ã¯transpose
ã®ã¡ã½ãããç¨æããã¦ãã¾ãã
flatten
ãã¡ãããPythonã«ã¯ç°¡åã«æä½ã§ããã¡ã½ããã¯ç¨æããã¦ãã¾ããã
group_by
ç§ã好ããªã¡ã½ããã®ä¸ã¤ã§ãã以åæ¸ããè¨äºã®ã¾ãã¾ã§ããã ã¡ã½ãããã§ã¼ã³ã§ãã¼ã¿ãã¼ã¹ã®ãã¼ãã«æä½ã¿ãããªãã¨ãç°¡åã«ã§ãã¾ãã
# Ruby # ã¹ã¿ã¼ã©ã¤ãå¦åã®ã¢ã¤ãã«ã®åç°¿ name_list = [ {'name' => 'ichigo', 'age' => 16, 'group' => 'soleil'}, {'name' => 'aoi', 'age' => 17, 'group' => 'soleil'}, {'name' => 'ran', 'age' => 17, 'group' => 'soleil'}, {'name' => 'akari', 'age' => 13, 'group' => 'luminas'}, {'name' => 'sumire', 'age' => 14, 'group' => 'luminas'}, {'name' => 'hinaki', 'age' => 14, 'group' => 'luminas'}, ] def col_mean(list, colname) list.reduce(0){|x, y| x + y[colname]} / list.size.to_f end # åã°ã«ã¼ãã®å¹´é½¢ã§åéè¨ p name_list. group_by{|raw| raw['group']}. map{|k, v| {k => col_mean(v, 'age')}} # => [{"soleil"=>16.666666666666668}, {"luminas"=>13.666666666666666}]
Pythonã§ã¯ãitertools
ã®groupby
é¢æ°ã§åæ§ã®æ©è½ã使ãã¾ãã
ãã ããå
¬å¼ããã¥ã¡ã³ãã«ããã¨ããã
- æ»ãå¤ãããã·ã¥ï¼è¾æ¸ï¼ã§ã¯ãªãã¸ã§ãã¬ã¼ã¿ã¼ã§ãããã¨ï¼ä¸åº¦ã«ã¼ããããã¨ä¸èº«ãæ¶ããï¼
- key é¢æ°ã®å¤ãå¤ãããã³ã«æ°ããã°ã«ã¼ããä½ããããã¨
ã«æ³¨æããå¿
è¦ãããã¾ããä¸ã®ãããªSQLã£ã½ãéç´ã®ããã«ä½¿ããªãããããããsorted
é¢æ°ã§ã½ã¼ããã¦ããã¾ãããã
ã¾ã¨ã
ä»ã«ãRubyã®ã¡ã½ããã¯ããããããã¾ãããRubyã«æ £ããããã°ã©ãã¼ãã¤ã¾ã¥ããããã¨ãããä¸å¿ã«ç´¹ä»ããã¤ããã§ãã ï¼ãã£ã¨è¯ãæ¹æ³ãããã°æãã¦ãã ããï¼ï¼
ãPythonãã¤ã±ã¦ããï¼ãã¨ããè¨äºã«ããã¤ãããªã®ã§ãããeach_cons
ãuniq
ãªã©ã¯æã£ãããåç¾ãé£ããæãã¾ããã
æ®æ®µä½¿ã£ã¦ãã便å©ãªã¡ã½ããããèªåã§åç¾ããã¨åå¼·ã«ãªãã¾ããâ¦ã
ã¾ããPythonã®itertools
ã«ã¯ã¾ã 便å©ãªæ©è½ãç ã£ã¦ããæ°ããã¦ãã¾ãã
ä¸åº¦èª¿ã¹ã¦ã¿ããã§ãã
ãã®ã¢ã¸ã¥ã¼ã«ã¯ ã¤ãã¬ã¼ã¿ ãæ§ç¯ããé¨åãå®è£ ãã¦ãã¾ããããã°ã©ã è¨èª APL, Haskell, SML ããã¢ã¤ãã¢ãå¾ã¦ãã¾ããã Python ã«é©ããå½¢ã«ä¿®æ£ããã¦ãã¾ãã
ãã£ã±ãHaskellãä¸åº¦ã¬ãããªè§¦ãã¹ããªã®ããªâ¦ãåã®ãã£ããããè¨èªããããããã
Effective Python âPythonããã°ã©ã ãæ¹è¯ãã59é ç®
- ä½è : Brett Slatkin,ç³æ¬æ¦å¤«,é»å·å©æ
- åºç社/ã¡ã¼ã«ã¼: ãªã©ã¤ãªã¼ã¸ã£ãã³
- çºå£²æ¥: 2016/01/23
- ã¡ãã£ã¢: 大åæ¬
- ãã®ååãå«ãããã° (4件) ãè¦ã